in src/main/java/org/apache/commons/imaging/common/BasicCParser.java [405:468]
public String nextToken() throws IOException, ImagingException {
// I don't know how complete the C parsing in an XPM file
// is meant to be, this is just the very basics...
boolean inString = false;
boolean inIdentifier = false;
boolean hadBackSlash = false;
final StringBuilder token = new StringBuilder();
for (int c = is.read(); c != -1; c = is.read()) {
if (inString) {
switch (c) {
case '\\':
token.append('\\');
hadBackSlash = !hadBackSlash;
break;
case '"':
token.append('"');
if (!hadBackSlash) {
return token.toString();
}
hadBackSlash = false;
break;
case '\r':
case '\n':
throw new ImagingException(
"Unterminated string in XPM file");
default:
token.append((char) c);
hadBackSlash = false;
break;
}
} else if (inIdentifier) {
if (!Character.isLetterOrDigit(c) && (c != '_')) {
is.unread(c);
return token.toString();
}
token.append((char) c);
} else if (c == '"') {
token.append('"');
inString = true;
} else if (Character.isLetterOrDigit(c) || c == '_') {
token.append((char) c);
inIdentifier = true;
} else if (c == '{' || c == '}' || c == '[' || c == ']'
|| c == '*' || c == ';' || c == '=' || c == ',') {
token.append((char) c);
return token.toString();
} else if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
// ignore
} else {
throw new ImagingException(
"Unhandled/invalid character '" + ((char) c)
+ "' found in XPM file");
}
}
if (inIdentifier) {
return token.toString();
}
if (inString) {
throw new ImagingException("Unterminated string ends XMP file");
}
return null;
}