@Override public void setMapForKey(final Map<String, String> value, final String key) { final NSDictionary list = new NSDictionary(); for(Map.Entry<String, String> entry : value.entrySet()) { list.put(entry.getKey(), entry.getValue()); } dict.put(key, list); }
public static Optional<NSObject> optional(NSDictionary dictionary, String key) { return dictionary.containsKey(key) ? Optional.of(dictionary.get(key)) : Optional.empty(); }
@Override public Map<String, String> mapForKey(final String key) { final NSObject value = dict.objectForKey(key); if(null == value) { return null; } if(value instanceof NSDictionary) { final NSDictionary dict = (NSDictionary) value; final Map<String, String> map = new HashMap<>(); for(String k : dict.allKeys()) { map.put(k, dict.objectForKey(k).toString()); } return map; } log.warn(String.format("Unexpected value type for serialized key %s", key)); return null; }
@Override public void setStringForKey(final String value, final String key) { dict.put(key, value); }
@Override protected void toASCII(StringBuilder ascii, int level) { this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.DICTIONARY_BEGIN_TOKEN); ascii.append(NEWLINE); String[] keys = this.allKeys(); for (String key : keys) { NSObject val = this.objectForKey(key); this.indent(ascii, level + 1); ascii.append('"'); ascii.append(NSString.escapeStringForASCII(key)); ascii.append("\" ="); Class<?> objClass = val.getClass(); if (objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) { ascii.append(NEWLINE); val.toASCII(ascii, level + 2); } else { ascii.append(' '); val.toASCII(ascii, 0); } ascii.append(ASCIIPropertyListParser.DICTIONARY_ITEM_DELIMITER_TOKEN); ascii.append(NEWLINE); } this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.DICTIONARY_END_TOKEN); }
public PlistSerializer() { this(new NSDictionary()); }
@Override public String stringForKey(final String key) { final NSObject value = dict.objectForKey(key); if(null == value) { return null; } return value.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(NSDictionary 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 } } }
static String string(NSDictionary dictionary, String key, String defaultValue) { NSObject object = dictionary.get(key); return object == null ? defaultValue : object.toString(); } }
public static NSDictionary records(HttpClient httpClient, EscrowProxyRequestFactory requests) throws IOException { /* EscrowService SRP-6a exchanges: GETRECORDS */ HttpUriRequest recordsRequest = requests.getRecords(); NSDictionary dictionary = httpClient.execute(recordsRequest, RESPONSE_HANDLER); logger.debug("-- records() - GETRECORDS: {}", dictionary.toXMLPropertyList()); return dictionary; } }
@Override void toXML(StringBuilder xml, int level) { this.indent(xml, level); xml.append("<dict>"); xml.append(NSObject.NEWLINE); for (String key : this.dict.keySet()) { NSObject val = this.objectForKey(key); this.indent(xml, level + 1); xml.append("<key>"); //According to http://www.w3.org/TR/REC-xml/#syntax node values must not //contain the characters < or &. Also the > character should be escaped. if (key.contains("&") || key.contains("<") || key.contains(">")) { xml.append("<![CDATA["); xml.append(key.replaceAll("]]>", "]]]]><![CDATA[>")); xml.append("]]>"); } else { xml.append(key); } xml.append("</key>"); xml.append(NSObject.NEWLINE); val.toXML(xml, level + 1); xml.append(NSObject.NEWLINE); } this.indent(xml, level); xml.append("</dict>"); }
throw new InvalidLicenseException(); final NSData signature = (NSData) dictionary.objectForKey("Signature"); if(null == signature) { log.warn(String.format("Missing key 'Signature' in dictionary %s", dictionary)); final ArrayList<String> keys = new ArrayList<>(dictionary.keySet()); continue; values.append(dictionary.objectForKey(key).toString());
@Deprecated public static <T extends NSObject> T fetchOrDefault(NSDictionary dictionary, String key, T defaultValue) throws BadDataException { return dictionary.containsKey(key) ? cast(key, dictionary) : defaultValue; }
public void putAll(Map<? extends String, ? extends NSObject> values) { for (Object object : values.entrySet()) { @SuppressWarnings("unchecked") Map.Entry<String, NSObject> entry = (Map.Entry<String, NSObject>) object; this.put(entry.getKey(), entry.getValue()); } }
@Override public NSDictionary clone() { NSDictionary clone = new NSDictionary(); for(Entry<String, NSObject> entry : this.dict.entrySet()) { clone.dict.put(entry.getKey(), entry.getValue() != null ? entry.getValue().clone() : null); } return clone; }
@Override public NSDictionary objectForKey(final String key) { final NSObject value = dict.objectForKey(key); if(null == value) { return null; } if(value instanceof NSDictionary) { return (NSDictionary) value; } log.warn(String.format("Unexpected value type for serialized key %s", key)); return null; }
public static Auth from(HttpClient client, String id, String password) throws BadDataException, IOException { logger.trace("<< from() < id: {} password: {}", id, password); byte[] data = authClient.get(client, id, password); NSDictionary authentication = parse(data); logger.debug("-- from() > authentication: {}", authentication.toASCIIPropertyList()); NSDictionary appleAccountInfo = get(authentication, "appleAccountInfo"); String dsPrsID = string(appleAccountInfo, "dsPrsID"); NSDictionary tokens = get(authentication, "tokens"); String mmeAuthToken = string(tokens, "mmeAuthToken"); logger.debug("-- from() > dsPrsID: {}", dsPrsID); logger.debug("-- from() > mmeAuthToken: {}", mmeAuthToken); Auth auth = new Auth(dsPrsID, mmeAuthToken); logger.trace(">> from > auth: {}", auth); return auth; }
static <T extends NSObject> T cast(String key, NSDictionary dictionary) throws BadDataException { try { return (T) dictionary.get(key); } catch (ClassCastException ex) { throw new BadDataException("Bad type for key:" + key, ex); } }
static NSDictionary srpInit(HttpClient httpClient, EscrowProxyRequestFactory requests, SRPClient srpClient) throws IOException { /* SRP-6a SRP_INIT Randomly generated ephemeral key A presented to escrow server along with id (mmeAuthToken header). Server returns amongst other things a salt and an ephemeral key B. */ byte[] ephemeralKeyA = srpClient.generateClientCredentials(); HttpUriRequest srpInitRequest = requests.srpInit(ephemeralKeyA); NSDictionary dictionary = httpClient.execute(srpInitRequest, RESPONSE_HANDLER); logger.debug("-- srpInit() - SRP_INIT: {}", dictionary.toXMLPropertyList()); return dictionary; }
@Override protected void toASCIIGnuStep(StringBuilder ascii, int level) { this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.DICTIONARY_BEGIN_TOKEN); ascii.append(NEWLINE); String[] keys = this.dict.keySet().toArray(new String[this.dict.size()]); for (String key : keys) { NSObject val = this.objectForKey(key); this.indent(ascii, level + 1); ascii.append('"'); ascii.append(NSString.escapeStringForASCII(key)); ascii.append("\" ="); Class<?> objClass = val.getClass(); if (objClass.equals(NSDictionary.class) || objClass.equals(NSArray.class) || objClass.equals(NSData.class)) { ascii.append(NEWLINE); val.toASCIIGnuStep(ascii, level + 2); } else { ascii.append(' '); val.toASCIIGnuStep(ascii, 0); } ascii.append(ASCIIPropertyListParser.DICTIONARY_ITEM_DELIMITER_TOKEN); ascii.append(NEWLINE); } this.indent(ascii, level); ascii.append(ASCIIPropertyListParser.DICTIONARY_END_TOKEN); } }