@Override public <T extends Serializable> void setListForKey(final List<T> value, final String key) { final NSArray list = new NSArray(value.size()); int i = 0; for(Serializable serializable : value) { list.setValue(i, serializable.<NSDictionary>serialize(SerializerFactory.get())); i++; } dict.put(key, list); }
@Override public boolean equals(Object obj) { if(obj == null) return false; if(obj.getClass().equals(NSArray.class)) { return Arrays.equals(((NSArray) obj).getArray(), this.array); } else { NSObject nso = NSObject.fromJavaObject(obj); if(nso.getClass().equals(NSArray.class)) { return Arrays.equals(((NSArray) nso).getArray(), this.array); } } return false; }
@Override public <T> List<T> listForKey(final String key) { final NSObject value = dict.objectForKey(key); if(null == value) { return null; } if(value instanceof NSArray) { final NSArray array = (NSArray) value; final List<T> list = new ArrayList<T>(); for(int i = 0; i < array.count(); i++) { final NSObject next = array.objectAtIndex(i); if(next instanceof NSDictionary) { list.add((T) next); } else if(next instanceof NSString) { list.add((T) next.toString()); } else { log.warn(String.format("Ignore content of type %s", next)); } } return list; } log.warn(String.format("Unexpected value type for serialized key %s", key)); return null; }
@Override public void write(final Collection<S> collection, final Local file) throws AccessDeniedException { final NSArray list = new NSArray(collection.size()); int i = 0; for(S bookmark : collection) { list.setValue(i, bookmark.<NSDictionary>serialize(SerializerFactory.get())); i++; } final String content = list.toXMLPropertyList(); final OutputStream out = file.getOutputStream(false); try { IOUtils.write(content, out, Charset.forName("UTF-8")); } catch(IOException e) { throw new AccessDeniedException(String.format("Cannot create file %s", file.getAbsolute()), e); } finally { IOUtils.closeQuietly(out); } }
@Override public NSArray clone() { NSObject[] clonedArray = new NSObject[this.array.length]; for(int i = 0; i < this.array.length; i++) { clonedArray[i] = this.array[i] != null ? this.array[i].clone() : null; } return new NSArray(clonedArray); }
/** * Generates a valid ASCII property list which has this NSArray as its * root object. The generated property list complies with the format as * described in <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html"> * Property List Programming Guide - Old-Style ASCII Property Lists</a>. * * @return ASCII representation of this object. */ public String toASCIIPropertyList() { StringBuilder ascii = new StringBuilder(); this.toASCII(ascii, 0); ascii.append(NEWLINE); return ascii.toString(); }
/** * Generates a valid ASCII property list in GnuStep format which has this * NSArray as its root object. The generated property list complies with * the format as described in <a href="http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSPropertyList.html"> * GnuStep - NSPropertyListSerialization class documentation * </a> * * @return GnuStep ASCII representation of this object. */ public String toGnuStepASCIIPropertyList() { StringBuilder ascii = new StringBuilder(); this.toASCIIGnuStep(ascii, 0); ascii.append(NEWLINE); return ascii.toString(); }
/** * Saves a property list with the given object as root into a ASCII file. * * @param root The root object. * @param out The output file. * @throws IOException If an error occurs during the writing process. */ public static void saveAsASCII(NSArray root, File out) throws IOException { File parent = out.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { throw new IOException("The output directory does not exist and could not be created."); } OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(out), "ASCII"); try { w.write(root.toASCIIPropertyList()); } finally { try { w.close(); } catch (IOException ex) { // ignore } } }
private static NSArray fromCollection(Collection<?> collection) { List<NSObject> payload = new ArrayList<NSObject>(collection.size()); for (Object elem : collection) { payload.add(fromJavaObject(elem)); } return new NSArray(payload.toArray(new NSObject[payload.size()])); }
@Override protected void toASCII(StringBuilder ascii, int level) { this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); int indexOfLastNewLine = ascii.lastIndexOf(NEWLINE); for(int i = 0; i < this.array.length; i++) { Class<?> objClass = this.array[i].getClass(); if((objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) && indexOfLastNewLine != ascii.length()) { ascii.append(NEWLINE); indexOfLastNewLine = ascii.length(); this.array[i].toASCII(ascii, level + 1); } else { if(i != 0) ascii.append(' '); this.array[i].toASCII(ascii, 0); } if(i != this.array.length - 1) ascii.append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); if(ascii.length() - indexOfLastNewLine > ASCII_LINE_LENGTH) { ascii.append(NEWLINE); indexOfLastNewLine = ascii.length(); } } ascii.append(ASCIIPropertyListParser.ARRAY_END_TOKEN); }
private static NSObject fromArray(Object object, Class<?> objClass) { Class<?> elementClass = objClass.getComponentType(); if(elementClass == byte.class || elementClass == Byte.class) { return fromData(object); } int size = Array.getLength(object); NSObject[] array = new NSObject[size]; for (int i = 0; i < size; i++) { array[i] = fromJavaObject(Array.get(object, i)); } return new NSArray(array); }
static NSDictionary pcsRecord(NSDictionary records) { NSArray metadataList = PListsLegacy.getAs(records, "metadataList", NSArray.class); return Stream.of(metadataList.getArray()) .filter(EscrowedKeys::isPCSRecord) .map(nsObject -> (NSDictionary) nsObject) .findFirst() .orElseThrow(() -> new IllegalArgumentException("no escrow PCS record found: InflatableDonkey doesn't support iOS8 or earlier backups")); }
@Override public Collection<S> readCollection(final Local file) throws AccessDeniedException { final Collection<S> c = new Collection<S>(); final NSArray list = (NSArray) this.parse(file.getInputStream()); if(null == list) { log.error(String.format("Invalid bookmark file %s", file)); return c; } for(int i = 0; i < list.count(); i++) { NSObject next = list.objectAtIndex(i); if(next instanceof NSDictionary) { final NSDictionary dict = (NSDictionary) next; final S object = this.deserialize(dict); if(null == object) { continue; } c.add(object); } } return c; }
@Override protected void toASCIIGnuStep(StringBuilder ascii, int level) { this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); int indexOfLastNewLine = ascii.lastIndexOf(NEWLINE); for(int i = 0; i < this.array.length; i++) { Class<?> objClass = this.array[i].getClass(); if((objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) && indexOfLastNewLine != ascii.length()) { ascii.append(NEWLINE); indexOfLastNewLine = ascii.length(); this.array[i].toASCIIGnuStep(ascii, level + 1); } else { if(i != 0) ascii.append(' '); this.array[i].toASCIIGnuStep(ascii, 0); } if(i != this.array.length - 1) ascii.append(ASCIIPropertyListParser.ARRAY_ITEM_DELIMITER_TOKEN); if(ascii.length() - indexOfLastNewLine > ASCII_LINE_LENGTH) { ascii.append(NEWLINE); indexOfLastNewLine = ascii.length(); } } ascii.append(ASCIIPropertyListParser.ARRAY_END_TOKEN); } }
public static NSObject toNsObject(JsonArray a) { NSArray array = new NSArray(a.size()); int index=0; for(JsonElement v: a) { NSObject value; if(v.isObject()) { value=toNsObject(v.asObject()); } else if(v.isArray()) { value = toNsObject(v.asArray()); } else { value=toNsObject(v.asPrimitive()); } array.setValue(index++, value); } return array; }
/** * Parses an array from the current parsing position. * The prerequisite for calling this method is, that an array begin token has been read. * * @return The array found at the parsing position. */ private NSArray parseArray() throws ParseException { //Skip begin token this.skip(); this.skipWhitespacesAndComments(); List<NSObject> objects = new LinkedList<NSObject>(); while (!this.accept(ARRAY_END_TOKEN)) { objects.add(this.parseObject()); this.skipWhitespacesAndComments(); if (this.accept(ARRAY_ITEM_DELIMITER_TOKEN)) { this.skip(); } else { break; //must have reached end of array } this.skipWhitespacesAndComments(); } //parse end token this.read(ARRAY_END_TOKEN); return new NSArray(objects.toArray(new NSObject[objects.size()])); }
private Object[] deserializeArray() { NSObject[] originalArray = ((NSArray)this).getArray(); Object[] clonedArray = new Object[originalArray.length]; for(int i = 0; i < originalArray.length; i++) { clonedArray[i] = originalArray[i].toJavaObject(); } return clonedArray; }
int arrayOffset = lengthAndOffset[1]; NSArray array = new NSArray(length); for (int i = 0; i < length; i++) { int objRef = (int) parseUnsignedInt(this.bytes, offset + arrayOffset + i * this.objectRefSize, offset + arrayOffset + (i + 1) * this.objectRefSize); array.setValue(i, this.parseObject(objRef));