private char consumeWhiteSpace(CharacterIterator ci) { char c = ci.next(); // Consume white space; // TODO use correct c while (c == ' ') c = ci.next(); return c; }
/** * This method returns an instance of <code>CollationElementIterator</code> for the <code>String * </code> represented by the specified <code>CharacterIterator</code>. * * @param source The <code>CharacterIterator</code> with the desired <code>String</code>. * @return A <code>CollationElementIterator</code> for the specified <code>String</code>. */ public CollationElementIterator getCollationElementIterator(CharacterIterator source) { StringBuffer expand = new StringBuffer(""); // Right now we assume that we will read from the beginning of the string. for (char c = source.first(); c != CharacterIterator.DONE; c = source.next()) decomposeCharacter(c, expand); return getCollationElementIterator(expand.toString()); }
private String parseRegex(CharacterIterator ci) { StringBuffer regexBuffer = new StringBuffer(); int braceCount = 1; while (true) { char c = ci.next(); if (c == '{') { braceCount++; } else if (c == '}') { braceCount--; if (braceCount == 0) break; } regexBuffer.append(c); } return regexBuffer.toString().trim(); }
private void parse(CharacterIterator ci) { try { while (ci.hasNext()) { char c = ci.next(); if (c == '{') { processLiteralCharacters(); parseName(ci); } else { literalCharactersBuffer.append(c); } } processLiteralCharacters(); } catch (NoSuchElementException ex) { throw new IllegalArgumentException( "Invalid syntax for the template, \"" + template + "\". Check if a path parameter is terminated with a '}'.", ex); } }
public World() { // Parse foobar float x = 0, y = 0; CharacterIterator it = new StringCharacterIterator(foobar); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next(), x++) { switch (ch) { case '+': walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f))); case '-': walls.add(new Wall(new Vector2f(x, y + 0.5f), new Vector2f(x + 1.0f, y + 0.5f))); break; case '|': walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f))); break; case '\n': x = -1; y++; break; } } singleton = this; }
private void parseName(CharacterIterator ci) { char c = consumeWhiteSpace(ci); StringBuffer nameBuffer = new StringBuffer(); if (Character.isLetterOrDigit(c) || c == '_') { // Template name character nameBuffer.append(c); } else { throw new IllegalArgumentException( "Illegal character '" + c + "' at position " + ci.pos() + " is not as the start of a name"); } String nameRegexString = ""; while (true) { c = ci.next(); // "\\{(\\w[-\\w\\.]*) if (Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.') { // Template name character nameBuffer.append(c); } else if (c == ':') { nameRegexString = parseRegex(ci); break; } else if (c == '}') { break; } else if (c == ' ') { c = consumeWhiteSpace(ci); if (c == ':') { nameRegexString = parseRegex(ci); break; } else if (c == '}') { break; } else { // Error throw new IllegalArgumentException( "Illegal character '" + c + "' at position " + ci.pos() + " is not allowed after a name"); } } else { throw new IllegalArgumentException( "Illegal character '" + c + "' at position " + ci.pos() + " is not allowed as part of a name"); } } String name = nameBuffer.toString(); names.add(name); try { if (nameRegexString.length() > 0) numOfExplicitRegexes++; Pattern namePattern = (nameRegexString.length() == 0) ? TEMPLATE_VALUE_PATTERN : Pattern.compile(nameRegexString); if (nameToPattern.containsKey(name)) { if (!nameToPattern.get(name).equals(namePattern)) { throw new IllegalArgumentException( "The name '" + name + "' is declared " + "more than once with different regular expressions"); } } else { nameToPattern.put(name, namePattern); } // Determine group count of pattern Matcher m = namePattern.matcher(""); int g = m.groupCount(); groupCounts.add(g + 1); regex.append('(').append(namePattern).append(')'); normalizedTemplate.append('{').append(name).append('}'); } catch (PatternSyntaxException ex) { throw new IllegalArgumentException( "Invalid syntax for the expression '" + nameRegexString + "' associated with the name '" + name + "'", ex); } }