/** * Parse strings of format: * * <p>ENTRY (, ENTRY)* * * @param d Directive being parsed * @param s String to parse * @return A sorted ArrayList with enumeration or null if enumeration string was null. * @exception IllegalArgumentException If syntax error in input string. */ public static ArrayList parseEnumeration(String d, String s) { ArrayList result = new ArrayList(); if (s != null) { AttributeTokenizer at = new AttributeTokenizer(s); do { String key = at.getKey(); if (key == null) { throw new IllegalArgumentException( "Directive " + d + ", unexpected character at: " + at.getRest()); } if (!at.getEntryEnd()) { throw new IllegalArgumentException( "Directive " + d + ", expected end of entry at: " + at.getRest()); } int i = Math.abs(binarySearch(result, strComp, key) + 1); result.add(i, key); } while (!at.getEnd()); return result; } else { return null; } }
/** * Parse strings of format: * * <p>ENTRY (, ENTRY)* ENTRY = key (; key)* (; PARAM)* PARAM = attribute '=' value PARAM = * directive ':=' value * * @param a Attribute being parsed, the sole purpose of this param seems to be to spread * confusion... * @param s String to parse * @param single If true, only allow one key per ENTRY * @param unique Only allow unique parameters for each ENTRY. * @param single_entry If true, only allow one ENTRY is allowed. * @return Iterator(Map(param -> value)) or null if input string is null. * @exception IllegalArgumentException If syntax error in input string. */ public static Iterator parseEntries( String a, String s, boolean single, boolean unique, boolean single_entry) { ArrayList result = new ArrayList(); if (s != null) { AttributeTokenizer at = new AttributeTokenizer(s); do { ArrayList keys = new ArrayList(); HashMap params = new HashMap(); String key = at.getKey(); if (key == null) { throw new IllegalArgumentException( "Definition, " + a + ", expected key at: " + at.getRest()); } if (!single) { keys.add(key); while ((key = at.getKey()) != null) { keys.add(key); } } String param; while ((param = at.getParam()) != null) { List old = (List) params.get(param); boolean is_directive = at.isDirective(); if (old != null && unique) { throw new IllegalArgumentException( "Definition, " + a + ", duplicate " + (is_directive ? "directive" : "attribute") + ": " + param); } String value = at.getValue(); if (value == null) { throw new IllegalArgumentException( "Definition, " + a + ", expected value at: " + at.getRest()); } if (is_directive) { // NYI Handle directives and check them // This method has become very ugly, please rewrite. } if (unique) { params.put(param, value); } else { if (old == null) { old = new ArrayList(); params.put(param, old); } old.add(value); } } if (at.getEntryEnd()) { if (single) { params.put("key", key); } else { params.put("keys", keys); } result.add(params); } else { throw new IllegalArgumentException( "Definition, " + a + ", expected end of entry at: " + at.getRest()); } if (single_entry && !at.getEnd()) { throw new IllegalArgumentException( "Definition, " + a + ", expected end of single entry at: " + at.getRest()); } } while (!at.getEnd()); } return result.iterator(); }