diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-11-04 14:02:20 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2010-11-04 14:02:20 -0700 |
commit | ccd79e2bb784cdbd89e90a0e4301a707091d446d (patch) | |
tree | 3eb0e619b9394bf16c6ad7781f67945004468d7f /xml/src/main/java/org/kxml2 | |
parent | 700ec1f739329bd2deede32cc72affa473d737cc (diff) | |
download | libcore-ccd79e2bb784cdbd89e90a0e4301a707091d446d.zip libcore-ccd79e2bb784cdbd89e90a0e4301a707091d446d.tar.gz libcore-ccd79e2bb784cdbd89e90a0e4301a707091d446d.tar.bz2 |
Style cleanup of some XML code.
Motivation: in preparation to refactor the Kxml code I'd like
to bring this code to a style consistent with the rest of
Android. This code style currently disagrees with my toolchain.
Change-Id: Ibd24570c131e792532e46f7f44c64abac3a6979a
http://b/3090550
Diffstat (limited to 'xml/src/main/java/org/kxml2')
-rw-r--r-- | xml/src/main/java/org/kxml2/io/KXmlParser.java | 799 |
1 files changed, 352 insertions, 447 deletions
diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java index dd73f0b..3ee5e43 100644 --- a/xml/src/main/java/org/kxml2/io/KXmlParser.java +++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java @@ -22,14 +22,18 @@ package org.kxml2.io; -import java.io.*; -import java.util.*; - -import org.xmlpull.v1.*; - -/** A simple, pull based XML parser. This classe replaces the kXML 1 - XmlParser class and the corresponding event classes. */ - +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.HashMap; +import java.util.Map; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +/** + * A pull based XML parser. + */ public class KXmlParser implements XmlPullParser { private Object location; @@ -45,8 +49,8 @@ public class KXmlParser implements XmlPullParser { private boolean processNsp; private boolean relaxed; - private boolean keepNamespaceAttributes; // android-added - private Hashtable entityMap; + private boolean keepNamespaceAttributes; + private Map<String, String> entityMap; private int depth; private String[] elementStack = new String[16]; private String[] nspStack = new String[8]; @@ -68,7 +72,7 @@ public class KXmlParser implements XmlPullParser { /** Target buffer for storing incoming text (including aggregated resolved entities) */ private char[] txtBuf = new char[128]; - /** Write position */ + /** Write position */ private int txtPos; // Event-related @@ -90,13 +94,13 @@ public class KXmlParser implements XmlPullParser { * i + 3 = attribute value */ private String[] attributes = new String[16]; -// private int stackMismatch = 0; + private String error; /** - * A separate peek buffer seems simpler than managing - * wrap around in the first level read buffer */ - + * A separate peek buffer seems simpler than managing wrap around in the first level read + * buffer + */ private int[] peek = new int[2]; private int peekCount; private boolean wasCR; @@ -105,39 +109,34 @@ public class KXmlParser implements XmlPullParser { private boolean token; public KXmlParser() { - srcBuf = - new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128]; + srcBuf = new char[8192]; } - // BEGIN android-added /** - * Retains namespace attributes like {@code xmlns="http://foo"} or {@code - * xmlns:foo="http:foo"} in pulled elements. Most applications will only be - * interested in the effective namespaces of their elements, so these - * attributes aren't useful. But for structure preserving wrappers like DOM, - * it is necessary to keep the namespace data around. + * Retains namespace attributes like {@code xmlns="http://foo"} or {@code xmlns:foo="http:foo"} + * in pulled elements. Most applications will only be interested in the effective namespaces of + * their elements, so these attributes aren't useful. But for structure preserving wrappers like + * DOM, it is necessary to keep the namespace data around. */ public void keepNamespaceAttributes() { this.keepNamespaceAttributes = true; } - // END android-added - private final boolean isProp(String n1, boolean prop, String n2) { - if (!n1.startsWith("http://xmlpull.org/v1/doc/")) + private boolean isProp(String n1, boolean prop, String n2) { + if (!n1.startsWith("http://xmlpull.org/v1/doc/")) { return false; - if (prop) + } + if (prop) { return n1.substring(42).equals(n2); - else + } else { return n1.substring(40).equals(n2); + } } - private final boolean adjustNsp() throws XmlPullParserException { - + private boolean adjustNsp() throws XmlPullParserException { boolean any = false; for (int i = 0; i < attributeCount << 2; i += 4) { - // * 4 - 4; i >= 0; i -= 4) { - String attrName = attributes[i + 2]; int cut = attrName.indexOf(':'); String prefix; @@ -145,32 +144,26 @@ public class KXmlParser implements XmlPullParser { if (cut != -1) { prefix = attrName.substring(0, cut); attrName = attrName.substring(cut + 1); - } - else if (attrName.equals("xmlns")) { + } else if (attrName.equals("xmlns")) { prefix = attrName; attrName = null; - } - else + } else { continue; + } if (!prefix.equals("xmlns")) { any = true; - } - else { + } else { int j = (nspCounts[depth]++) << 1; nspStack = ensureCapacity(nspStack, j + 2); nspStack[j] = attrName; nspStack[j + 1] = attributes[i + 3]; - if (attrName != null && attributes[i + 3].isEmpty()) + if (attrName != null && attributes[i + 3].isEmpty()) { error("illegal empty namespace"); + } - // prefixMap = new PrefixMap (prefixMap, attrName, attr.getValue ()); - - //System.out.println (prefixMap); - - // BEGIN android-changed if (keepNamespaceAttributes) { // explicitly set the namespace for unprefixed attributes // such as xmlns="http://foo" @@ -186,7 +179,6 @@ public class KXmlParser implements XmlPullParser { i -= 4; } - // END android-changed } } @@ -196,45 +188,33 @@ public class KXmlParser implements XmlPullParser { String attrName = attributes[i + 2]; int cut = attrName.indexOf(':'); - if (cut == 0 && !relaxed) + if (cut == 0 && !relaxed) { throw new RuntimeException( - "illegal attribute name: " + attrName + " at " + this); - - else if (cut != -1) { + "illegal attribute name: " + attrName + " at " + this); + } else if (cut != -1) { String attrPrefix = attrName.substring(0, cut); attrName = attrName.substring(cut + 1); String attrNs = getNamespace(attrPrefix); - if (attrNs == null && !relaxed) + if (attrNs == null && !relaxed) { throw new RuntimeException( - "Undefined Prefix: " + attrPrefix + " in " + this); + "Undefined Prefix: " + attrPrefix + " in " + this); + } attributes[i] = attrNs; attributes[i + 1] = attrPrefix; attributes[i + 2] = attrName; - - /* - if (!relaxed) { - for (int j = (attributeCount << 2) - 4; j > i; j -= 4) - if (attrName.equals(attributes[j + 2]) - && attrNs.equals(attributes[j])) - exception( - "Duplicate Attribute: {" - + attrNs - + "}" - + attrName); - } - */ } } } int cut = name.indexOf(':'); - if (cut == 0) + if (cut == 0) { error("illegal tag name: " + name); + } if (cut != -1) { prefix = name.substring(0, cut); @@ -244,49 +224,53 @@ public class KXmlParser implements XmlPullParser { this.namespace = getNamespace(prefix); if (this.namespace == null) { - if (prefix != null) + if (prefix != null) { error("undefined prefix: " + prefix); + } this.namespace = NO_NAMESPACE; } return any; } - private final String[] ensureCapacity(String[] arr, int required) { - if (arr.length >= required) + private String[] ensureCapacity(String[] arr, int required) { + if (arr.length >= required) { return arr; + } String[] bigger = new String[required + 16]; System.arraycopy(arr, 0, bigger, 0, arr.length); return bigger; } - private final void error(String desc) throws XmlPullParserException { + private void error(String desc) throws XmlPullParserException { if (relaxed) { - if (error == null) + if (error == null) { error = "ERR: " + desc; - } - else + } + } else { exception(desc); + } } - private final void exception(String desc) throws XmlPullParserException { + private void exception(String desc) throws XmlPullParserException { throw new XmlPullParserException( - desc.length() < 100 ? desc : desc.substring(0, 100) + "\n", - this, - null); + desc.length() < 100 ? desc : desc.substring(0, 100) + "\n", + this, + null); } /** - * common base for next and nextToken. Clears the state, except from - * txtPos and whitespace. Does not set the type variable */ - - private final void nextImpl() throws IOException, XmlPullParserException { - - if (reader == null) + * Common base for next() and nextToken(). Clears the state, except from txtPos and whitespace. + * Does not set the type variable. + */ + private void nextImpl() throws IOException, XmlPullParserException { + if (reader == null) { exception("No Input specified"); + } - if (type == END_TAG) + if (type == END_TAG) { depth--; + } while (true) { attributeCount = -1; @@ -300,79 +284,57 @@ public class KXmlParser implements XmlPullParser { return; } - if (error != null) { - for (int i = 0; i < error.length(); i++) + for (int i = 0; i < error.length(); i++) { push(error.charAt(i)); - //text = error; + } error = null; type = COMMENT; return; } - -// if (relaxed -// && (stackMismatch > 0 || (peek(0) == -1 && depth > 0))) { -// int sp = (depth - 1) << 2; -// type = END_TAG; -// namespace = elementStack[sp]; -// prefix = elementStack[sp + 1]; -// name = elementStack[sp + 2]; -// if (stackMismatch != 1) -// error = "missing end tag /" + name + " inserted"; -// if (stackMismatch > 0) -// stackMismatch--; -// return; -// } - prefix = null; name = null; namespace = null; - // text = null; type = peekType(); switch (type) { - case ENTITY_REF : + case ENTITY_REF: pushEntity(); return; - case START_TAG : + case START_TAG: parseStartTag(false); return; - case END_TAG : + case END_TAG: parseEndTag(); return; - case END_DOCUMENT : + case END_DOCUMENT: return; - case TEXT : - // BEGIN android-changed: distinguish attribute values from normal text. + case TEXT: pushText('<', !token, false); - // END android-changed if (depth == 0) { - if (isWhitespace) + if (isWhitespace) { type = IGNORABLE_WHITESPACE; - // make exception switchable for instances.chg... !!!! - // else - // exception ("text '"+getText ()+"' not allowed outside root element"); + } } return; - default : + default: type = parseLegacy(token); - if (type != XML_DECL) + if (type != XML_DECL) { return; + } } } } - private final int parseLegacy(boolean push) - throws IOException, XmlPullParserException { - + private int parseLegacy(boolean push) throws IOException, XmlPullParserException { String req = ""; int term; int result; @@ -383,7 +345,7 @@ public class KXmlParser implements XmlPullParser { if (c == '?') { if ((peek(0) == 'x' || peek(0) == 'X') - && (peek(1) == 'm' || peek(1) == 'M')) { + && (peek(1) == 'm' || peek(1) == 'M')) { if (push) { push(peek(0)); @@ -394,38 +356,42 @@ public class KXmlParser implements XmlPullParser { if ((peek(0) == 'l' || peek(0) == 'L') && peek(1) <= ' ') { - if (line != 1 || column > 4) + if (line != 1 || column > 4) { error("PI must not start with xml"); + } parseStartTag(true); - if (attributeCount < 1 || !"version".equals(attributes[2])) + if (attributeCount < 1 || !"version".equals(attributes[2])) { error("version expected"); + } version = attributes[3]; int pos = 1; if (pos < attributeCount - && "encoding".equals(attributes[2 + 4])) { + && "encoding".equals(attributes[2 + 4])) { encoding = attributes[3 + 4]; pos++; } if (pos < attributeCount - && "standalone".equals(attributes[4 * pos + 2])) { + && "standalone".equals(attributes[4 * pos + 2])) { String st = attributes[3 + 4 * pos]; - if ("yes".equals(st)) + if ("yes".equals(st)) { standalone = new Boolean(true); - else if ("no".equals(st)) + } else if ("no".equals(st)) { standalone = new Boolean(false); - else + } else { error("illegal standalone value: " + st); + } pos++; } - if (pos != attributeCount) + if (pos != attributeCount) { error("illegal xmldecl"); + } isWhitespace = true; txtPos = 0; @@ -434,117 +400,113 @@ public class KXmlParser implements XmlPullParser { } } - /* int c0 = read (); - int c1 = read (); - int */ - term = '?'; result = PROCESSING_INSTRUCTION; - } - else if (c == '!') { + } else if (c == '!') { if (peek(0) == '-') { result = COMMENT; req = "--"; term = '-'; - } - else if (peek(0) == '[') { + } else if (peek(0) == '[') { result = CDSECT; req = "[CDATA["; term = ']'; push = true; - } - else { + } else { result = DOCDECL; req = "DOCTYPE"; term = -1; } - } - else { + } else { error("illegal: <" + c); return COMMENT; } - for (int i = 0; i < req.length(); i++) + for (int i = 0; i < req.length(); i++) { read(req.charAt(i)); + } - if (result == DOCDECL) + if (result == DOCDECL) { parseDoctype(push); - else { + } else { while (true) { c = read(); - if (c == -1){ + if (c == -1) { error(UNEXPECTED_EOF); return COMMENT; } - if (push) + if (push) { push(c); + } if ((term == '?' || c == term) - && peek(0) == term - && peek(1) == '>') + && peek(0) == term + && peek(1) == '>') { break; + } prev = c; } - if (term == '-' && prev == '-' && !relaxed) + if (term == '-' && prev == '-' && !relaxed) { error("illegal comment delimiter: --->"); + } read(); read(); - if (push && term != '?') + if (push && term != '?') { txtPos--; + } } return result; } - /** precondition: <! consumed */ - - private final void parseDoctype(boolean push) - throws IOException, XmlPullParserException { - + /** + * precondition: <! consumed + */ + private void parseDoctype(boolean push) throws IOException, XmlPullParserException { int nesting = 1; boolean quoted = false; - // read(); - while (true) { int i = read(); switch (i) { - case -1 : + case -1: error(UNEXPECTED_EOF); return; - case '\'' : + case '\'': quoted = !quoted; break; - case '<' : - if (!quoted) + case '<': + if (!quoted) { nesting++; + } break; - case '>' : + case '>': if (!quoted) { - if ((--nesting) == 0) + if ((--nesting) == 0) { return; + } } break; } - if (push) + if (push) { push(i); + } } } - /* precondition: </ consumed */ - - private final void parseEndTag() - throws IOException, XmlPullParserException { - + /** + * precondition: </ consumed + */ + private void parseEndTag() throws IOException, XmlPullParserException { read(); // '<' read(); // '/' name = readName(); @@ -560,66 +522,42 @@ public class KXmlParser implements XmlPullParser { } if (!relaxed) { - if (!name.equals(elementStack[sp + 3])) { - error("expected: /" + elementStack[sp + 3] + " read: " + name); - - // become case insensitive in relaxed mode - -// int probe = sp; -// while (probe >= 0 && !name.toLowerCase().equals(elementStack[probe + 3].toLowerCase())) { -// stackMismatch++; -// probe -= 4; -// } -// -// if (probe < 0) { -// stackMismatch = 0; -// // text = "unexpected end tag ignored"; -// type = COMMENT; -// return; -// } - } + if (!name.equals(elementStack[sp + 3])) { + error("expected: /" + elementStack[sp + 3] + " read: " + name); + } - namespace = elementStack[sp]; - prefix = elementStack[sp + 1]; - name = elementStack[sp + 2]; + namespace = elementStack[sp]; + prefix = elementStack[sp + 1]; + name = elementStack[sp + 2]; } } - private final int peekType() throws IOException { + private int peekType() throws IOException { switch (peek(0)) { - case -1 : + case -1: return END_DOCUMENT; - case '&' : + case '&': return ENTITY_REF; - case '<' : + case '<': switch (peek(1)) { - case '/' : + case '/': return END_TAG; - case '?' : - case '!' : + case '?': + case '!': return LEGACY; - default : + default: return START_TAG; } - default : + default: return TEXT; } } - private final String get(int pos) { + private String get(int pos) { return new String(txtBuf, pos, txtPos - pos); } - /* - private final String pop (int pos) { - String result = new String (txtBuf, pos, txtPos - pos); - txtPos = pos; - return result; - } - */ - - private final void push(int c) { - + private void push(int c) { isWhitespace &= c <= ' '; if (txtPos == txtBuf.length) { @@ -631,13 +569,13 @@ public class KXmlParser implements XmlPullParser { txtBuf[txtPos++] = (char) c; } - /** Sets name and attributes */ - - private final void parseStartTag(boolean xmldecl) - throws IOException, XmlPullParserException { - - if (!xmldecl) + /** + * Sets name and attributes + */ + private void parseStartTag(boolean xmldecl) throws IOException, XmlPullParserException { + if (!xmldecl) { read(); + } name = readName(); attributeCount = 0; @@ -652,8 +590,7 @@ public class KXmlParser implements XmlPullParser { read('>'); return; } - } - else { + } else { if (c == '/') { degenerated = true; read(); @@ -670,7 +607,6 @@ public class KXmlParser implements XmlPullParser { if (c == -1) { error(UNEXPECTED_EOF); - //type = COMMENT; return; } @@ -678,7 +614,6 @@ public class KXmlParser implements XmlPullParser { if (attrName.length() == 0) { error("attr name expected"); - //type = COMMENT; break; } @@ -693,8 +628,8 @@ public class KXmlParser implements XmlPullParser { skip(); if (peek(0) != '=') { - if(!relaxed){ - error("Attr.value missing f. "+attrName); + if (!relaxed) { + error("Attr.value missing f. " + attrName); } attributes[i] = attrName; } else { @@ -703,7 +638,7 @@ public class KXmlParser implements XmlPullParser { int delimiter = peek(0); if (delimiter != '\'' && delimiter != '"') { - if(!relaxed){ + if (!relaxed) { error("attr value delimiter missing!"); } delimiter = ' '; @@ -712,15 +647,14 @@ public class KXmlParser implements XmlPullParser { } int p = txtPos; - // BEGIN android-changed: distinguish attribute values from normal text. pushText(delimiter, true, true); - // END android-changed attributes[i] = get(p); txtPos = p; - if (delimiter != ' ') + if (delimiter != ' ') { read(); // skip endquote + } } } @@ -737,20 +671,11 @@ public class KXmlParser implements XmlPullParser { nspCounts[depth] = nspCounts[depth - 1]; - /* - if(!relaxed){ - for (int i = attributeCount - 1; i > 0; i--) { - for (int j = 0; j < i; j++) { - if (getAttributeName(i).equals(getAttributeName(j))) - exception("Duplicate Attribute: " + getAttributeName(i)); - } - } - } - */ - if (processNsp) + if (processNsp) { adjustNsp(); - else + } else { namespace = ""; + } elementStack[sp] = namespace; elementStack[sp + 1] = prefix; @@ -758,36 +683,26 @@ public class KXmlParser implements XmlPullParser { } /** - * result: isWhitespace; if the setName parameter is set, - * the name of the entity is stored in "name" */ - - private final void pushEntity() - throws IOException, XmlPullParserException { - + * result: isWhitespace; if the setName parameter is set, the name of the entity is stored in + * "name" + */ + private void pushEntity() throws IOException, XmlPullParserException { push(read()); // & - int pos = txtPos; while (true) { int c = peek(0); if (c == ';') { - read(); - break; + read(); + break; } if (c < 128 && (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '_' && c != '-' && c != '#') { - if(!relaxed){ + if (!relaxed) { error("unterminated entity ref"); } - // BEGIN android-removed: avoid log spam. - // System.out.println("broken entitiy: "+get(pos-1)); - // END android-removed - - //; ends with:"+(char)c); -// if (c != -1) -// push(c); return; } @@ -796,106 +711,99 @@ public class KXmlParser implements XmlPullParser { String code = get(pos); txtPos = pos - 1; - if (token && type == ENTITY_REF){ + if (token && type == ENTITY_REF) { name = code; } if (code.charAt(0) == '#') { - int c = - (code.charAt(1) == 'x' + int c = code.charAt(1) == 'x' ? Integer.parseInt(code.substring(2), 16) - : Integer.parseInt(code.substring(1))); + : Integer.parseInt(code.substring(1)); push(c); return; } - String result = (String) entityMap.get(code); + String result = entityMap.get(code); unresolved = result == null; if (unresolved) { - if (!token) + if (!token) { error("unresolved: &" + code + ";"); - } - else { - for (int i = 0; i < result.length(); i++) + } + } else { + for (int i = 0; i < result.length(); i++) { push(result.charAt(i)); + } } } - /** types: - '<': parse to any token (for nextToken ()) - '"': parse to quote - ' ': parse to whitespace or '>' - */ - - private final void pushText(int delimiter, boolean resolveEntities, boolean inAttributeValue) - throws IOException, XmlPullParserException { + /** + * types: '<': parse to any token (for nextToken ()) '"': parse to quote ' ': parse to + * whitespace or '>' + */ + private void pushText(int delimiter, boolean resolveEntities, boolean inAttributeValue) + throws IOException, XmlPullParserException { int next = peek(0); int cbrCount = 0; while (next != -1 && next != delimiter) { // covers eof, '<', '"' - if (delimiter == ' ') - if (next <= ' ' || next == '>') - break; + if (delimiter == ' ' && (next <= ' ' || next == '>')) { + break; + } - // BEGIN android-changed: "<" is not allowed in attribute values. if (next == '&') { - if (!resolveEntities) + if (!resolveEntities) { break; + } pushEntity(); - } - else if (next == '<' && inAttributeValue) { + } else if (next == '<' && inAttributeValue) { error("Illegal: \"<\" inside attribute value"); - } - else if (next == '\n' && type == START_TAG) { + } else if (next == '\n' && type == START_TAG) { read(); push(' '); - } - else + } else { push(read()); - // END android-changed + } - // BEGIN android-changed: "]]>" *is* allowed in attribute values, but - // is not allowed in regular text between markup. + /* + * "]]>" is allowed in attribute values, but is not allowed in + * regular text between markup. + */ final boolean allowCloseCdata = inAttributeValue; if (!allowCloseCdata && (next == '>' && cbrCount >= 2 && delimiter != ']')) { error("Illegal: \"]]>\" outside CDATA section"); } - // END android-changed - if (next == ']') + if (next == ']') { cbrCount++; - else + } else { cbrCount = 0; + } next = peek(0); } } - private final void read(char c) - throws IOException, XmlPullParserException { + private void read(char c) throws IOException, XmlPullParserException { int a = read(); - if (a != c) + if (a != c) { error("expected: '" + c + "' actual: '" + ((char) a) + "'"); + } } - private final int read() throws IOException { + private int read() throws IOException { int result; - if (peekCount == 0) + if (peekCount == 0) { result = peek(0); - else { + } else { result = peek[0]; peek[0] = peek[1]; } - // else { - // result = peek[0]; - // System.arraycopy (peek, 1, peek, 0, peekCount-1); - // } peekCount--; column++; @@ -909,24 +817,23 @@ public class KXmlParser implements XmlPullParser { return result; } - /** Does never read more than needed */ - - private final int peek(int pos) throws IOException { - + /** + * Does never read more than needed + */ + private int peek(int pos) throws IOException { while (pos >= peekCount) { - int nw; - - if (srcBuf.length <= 1) + if (srcBuf.length <= 1) { nw = reader.read(); - else if (srcPos < srcCount) + } else if (srcPos < srcCount) { nw = srcBuf[srcPos++]; - else { + } else { srcCount = reader.read(srcBuf, 0, srcBuf.length); - if (srcCount <= 0) + if (srcCount <= 0) { nw = -1; - else + } else { nw = srcBuf[0]; + } srcPos = 1; } @@ -934,14 +841,14 @@ public class KXmlParser implements XmlPullParser { if (nw == '\r') { wasCR = true; peek[peekCount++] = '\n'; - } - else { + } else { if (nw == '\n') { - if (!wasCR) + if (!wasCR) { peek[peekCount++] = '\n'; - } - else + } + } else { peek[peekCount++] = nw; + } wasCR = false; } @@ -950,43 +857,42 @@ public class KXmlParser implements XmlPullParser { return peek[pos]; } - private final String readName() - throws IOException, XmlPullParserException { - + private String readName() throws IOException, XmlPullParserException { int pos = txtPos; int c = peek(0); if ((c < 'a' || c > 'z') - && (c < 'A' || c > 'Z') - && c != '_' - && c != ':' - && c < 0x0c0 - && !relaxed) + && (c < 'A' || c > 'Z') + && c != '_' + && c != ':' + && c < 0x0c0 + && !relaxed) { error("name expected"); + } do { push(read()); c = peek(0); } while ((c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9') - || c == '_' - || c == '-' - || c == ':' - || c == '.' - || c >= 0x0b7); + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || c == '_' + || c == '-' + || c == ':' + || c == '.' + || c >= 0x0b7); String result = get(pos); txtPos = pos; return result; } - private final void skip() throws IOException { - + private void skip() throws IOException { while (true) { int c = peek(0); - if (c > ' ' || c == -1) + if (c > ' ' || c == -1) { break; + } read(); } } @@ -1007,15 +913,16 @@ public class KXmlParser implements XmlPullParser { version = null; standalone = null; - if (reader == null) + if (reader == null) { return; + } srcPos = 0; srcCount = 0; peekCount = 0; depth = 0; - entityMap = new Hashtable(); + entityMap = new HashMap<String, String>(); entityMap.put("amp", "&"); entityMap.put("apos", "'"); entityMap.put("gt", ">"); @@ -1023,18 +930,16 @@ public class KXmlParser implements XmlPullParser { entityMap.put("quot", "\""); } - public void setInput(InputStream is, String _enc) - throws XmlPullParserException { - + public void setInput(InputStream is, String _enc) throws XmlPullParserException { srcPos = 0; srcCount = 0; String enc = _enc; - if (is == null) + if (is == null) { throw new IllegalArgumentException(); + } try { - if (enc == null) { // read four bytes @@ -1042,63 +947,66 @@ public class KXmlParser implements XmlPullParser { while (srcCount < 4) { int i = is.read(); - if (i == -1) + if (i == -1) { break; + } chk = (chk << 8) | i; srcBuf[srcCount++] = (char) i; } if (srcCount == 4) { switch (chk) { - case 0x00000FEFF : + case 0x00000FEFF: enc = "UTF-32BE"; srcCount = 0; break; - case 0x0FFFE0000 : + case 0x0FFFE0000: enc = "UTF-32LE"; srcCount = 0; break; - case 0x03c : + case 0x03c: enc = "UTF-32BE"; srcBuf[0] = '<'; srcCount = 1; break; - case 0x03c000000 : + case 0x03c000000: enc = "UTF-32LE"; srcBuf[0] = '<'; srcCount = 1; break; - case 0x0003c003f : + case 0x0003c003f: enc = "UTF-16BE"; srcBuf[0] = '<'; srcBuf[1] = '?'; srcCount = 2; break; - case 0x03c003f00 : + case 0x03c003f00: enc = "UTF-16LE"; srcBuf[0] = '<'; srcBuf[1] = '?'; srcCount = 2; break; - case 0x03c3f786d : + case 0x03c3f786d: while (true) { int i = is.read(); - if (i == -1) + if (i == -1) { break; + } srcBuf[srcCount++] = (char) i; if (i == '>') { String s = new String(srcBuf, 0, srcCount); int i0 = s.indexOf("encoding"); if (i0 != -1) { while (s.charAt(i0) != '"' - && s.charAt(i0) != '\'') + && s.charAt(i0) != '\'') { i0++; + } char deli = s.charAt(i0++); int i1 = s.indexOf(deli, i0); enc = s.substring(i0, i1); @@ -1107,20 +1015,18 @@ public class KXmlParser implements XmlPullParser { } } - default : + default: if ((chk & 0x0ffff0000) == 0x0FEFF0000) { enc = "UTF-16BE"; srcBuf[0] = - (char) ((srcBuf[2] << 8) | srcBuf[3]); + (char) ((srcBuf[2] << 8) | srcBuf[3]); srcCount = 1; - } - else if ((chk & 0x0ffff0000) == 0x0fffe0000) { + } else if ((chk & 0x0ffff0000) == 0x0fffe0000) { enc = "UTF-16LE"; srcBuf[0] = - (char) ((srcBuf[3] << 8) | srcBuf[2]); + (char) ((srcBuf[3] << 8) | srcBuf[2]); srcCount = 1; - } - else if ((chk & 0x0ffffff00) == 0x0EFBBBF00) { + } else if ((chk & 0x0ffffff00) == 0x0EFBBBF00) { enc = "UTF-8"; srcBuf[0] = srcBuf[3]; srcCount = 1; @@ -1129,29 +1035,30 @@ public class KXmlParser implements XmlPullParser { } } - if (enc == null) + if (enc == null) { enc = "UTF-8"; + } int sc = srcCount; setInput(new InputStreamReader(is, enc)); encoding = _enc; srcCount = sc; - } - catch (Exception e) { + } catch (Exception e) { throw new XmlPullParserException( - "Invalid stream or encoding: " + e.toString(), - this, - e); + "Invalid stream or encoding: " + e.toString(), + this, + e); } } public boolean getFeature(String feature) { - if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) + if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) { return processNsp; - else if (isProp(feature, false, "relaxed")) + } else if (isProp(feature, false, "relaxed")) { return relaxed; - else + } else { return false; + } } public String getInputEncoding() { @@ -1159,25 +1066,30 @@ public class KXmlParser implements XmlPullParser { } public void defineEntityReplacementText(String entity, String value) - throws XmlPullParserException { - if (entityMap == null) + throws XmlPullParserException { + if (entityMap == null) { throw new RuntimeException("entity replacement text must be defined after setInput!"); + } entityMap.put(entity, value); } public Object getProperty(String property) { - if (isProp(property, true, "xmldecl-version")) + if (isProp(property, true, "xmldecl-version")) { return version; - if (isProp(property, true, "xmldecl-standalone")) + } + if (isProp(property, true, "xmldecl-standalone")) { return standalone; - if (isProp(property, true, "location")) + } + if (isProp(property, true, "location")) { return location != null ? location : reader.toString(); + } return null; } public int getNamespaceCount(int depth) { - if (depth > this.depth) + if (depth > this.depth) { throw new IndexOutOfBoundsException(); + } return nspCounts[depth]; } @@ -1191,18 +1103,21 @@ public class KXmlParser implements XmlPullParser { public String getNamespace(String prefix) { - if ("xml".equals(prefix)) + if ("xml".equals(prefix)) { return "http://www.w3.org/XML/1998/namespace"; - if ("xmlns".equals(prefix)) + } + if ("xmlns".equals(prefix)) { return "http://www.w3.org/2000/xmlns/"; + } for (int i = (getNamespaceCount(depth) << 1) - 2; i >= 0; i -= 2) { if (prefix == null) { - if (nspStack[i] == null) + if (nspStack[i] == null) { return nspStack[i + 1]; - } - else if (prefix.equals(nspStack[i])) + } + } else if (prefix.equals(nspStack[i])) { return nspStack[i + 1]; + } } return null; } @@ -1212,50 +1127,53 @@ public class KXmlParser implements XmlPullParser { } public String getPositionDescription() { - - StringBuffer buf = - new StringBuffer(type < TYPES.length ? TYPES[type] : "unknown"); + StringBuilder buf = new StringBuilder(type < TYPES.length ? TYPES[type] : "unknown"); buf.append(' '); if (type == START_TAG || type == END_TAG) { - if (degenerated) + if (degenerated) { buf.append("(empty) "); + } buf.append('<'); - if (type == END_TAG) + if (type == END_TAG) { buf.append('/'); + } - if (prefix != null) + if (prefix != null) { buf.append("{" + namespace + "}" + prefix + ":"); + } buf.append(name); int cnt = attributeCount << 2; for (int i = 0; i < cnt; i += 4) { buf.append(' '); - if (attributes[i + 1] != null) + if (attributes[i + 1] != null) { buf.append( - "{" + attributes[i] + "}" + attributes[i + 1] + ":"); + "{" + attributes[i] + "}" + attributes[i + 1] + ":"); + } buf.append(attributes[i + 2] + "='" + attributes[i + 3] + "'"); } buf.append('>'); - } - else if (type == IGNORABLE_WHITESPACE); - else if (type != TEXT) + } else if (type == IGNORABLE_WHITESPACE) { + ; + } else if (type != TEXT) { buf.append(getText()); - else if (isWhitespace) + } else if (isWhitespace) { buf.append("(whitespace)"); - else { + } else { String text = getText(); - if (text.length() > 16) + if (text.length() > 16) { text = text.substring(0, 16) + "..."; + } buf.append(text); } - buf.append("@"+line + ":" + column); - if(location != null){ + buf.append("@" + line + ":" + column); + if (location != null) { buf.append(" in "); buf.append(location); - } else if(reader != null){ + } else if (reader != null) { buf.append(" in "); buf.append(reader.toString()); } @@ -1271,14 +1189,15 @@ public class KXmlParser implements XmlPullParser { } public boolean isWhitespace() throws XmlPullParserException { - if (type != TEXT && type != IGNORABLE_WHITESPACE && type != CDSECT) + if (type != TEXT && type != IGNORABLE_WHITESPACE && type != CDSECT) { exception(ILLEGAL_TYPE); + } return isWhitespace; } public String getText() { return type < TEXT - || (type == ENTITY_REF && unresolved) ? null : get(0); + || (type == ENTITY_REF && unresolved) ? null : get(0); } public char[] getTextCharacters(int[] poslen) { @@ -1311,8 +1230,9 @@ public class KXmlParser implements XmlPullParser { } public boolean isEmptyElementTag() throws XmlPullParserException { - if (type != START_TAG) + if (type != START_TAG) { exception(ILLEGAL_TYPE); + } return degenerated; } @@ -1329,26 +1249,30 @@ public class KXmlParser implements XmlPullParser { } public String getAttributeNamespace(int index) { - if (index >= attributeCount) + if (index >= attributeCount) { throw new IndexOutOfBoundsException(); + } return attributes[index << 2]; } public String getAttributeName(int index) { - if (index >= attributeCount) + if (index >= attributeCount) { throw new IndexOutOfBoundsException(); + } return attributes[(index << 2) + 2]; } public String getAttributePrefix(int index) { - if (index >= attributeCount) + if (index >= attributeCount) { throw new IndexOutOfBoundsException(); + } return attributes[(index << 2) + 1]; } public String getAttributeValue(int index) { - if (index >= attributeCount) + if (index >= attributeCount) { throw new IndexOutOfBoundsException(); + } return attributes[(index << 2) + 3]; } @@ -1356,8 +1280,9 @@ public class KXmlParser implements XmlPullParser { for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) { if (attributes[i + 2].equals(name) - && (namespace == null || attributes[i].equals(namespace))) + && (namespace == null || attributes[i].equals(namespace))) { return attributes[i + 3]; + } } return null; @@ -1376,22 +1301,23 @@ public class KXmlParser implements XmlPullParser { do { nextImpl(); - if (type < minType) + if (type < minType) { minType = type; + } // if (curr <= TEXT) type = curr; } while (minType > ENTITY_REF // ignorable - || (minType >= TEXT && peekType() >= TEXT)); + || (minType >= TEXT && peekType() >= TEXT)); type = minType; - if (type > TEXT) + if (type > TEXT) { type = TEXT; + } return type; } public int nextToken() throws XmlPullParserException, IOException { - isWhitespace = true; txtPos = 0; @@ -1400,90 +1326,69 @@ public class KXmlParser implements XmlPullParser { return type; } - // // utility methods to make XML parsing easier ... public int nextTag() throws XmlPullParserException, IOException { - next(); - if (type == TEXT && isWhitespace) + if (type == TEXT && isWhitespace) { next(); + } - if (type != END_TAG && type != START_TAG) + if (type != END_TAG && type != START_TAG) { exception("unexpected type"); + } return type; } public void require(int type, String namespace, String name) - throws XmlPullParserException, IOException { + throws XmlPullParserException, IOException { if (type != this.type - || (namespace != null && !namespace.equals(getNamespace())) - || (name != null && !name.equals(getName()))) + || (namespace != null && !namespace.equals(getNamespace())) + || (name != null && !name.equals(getName()))) { exception( - "expected: " + TYPES[type] + " {" + namespace + "}" + name); + "expected: " + TYPES[type] + " {" + namespace + "}" + name); + } } public String nextText() throws XmlPullParserException, IOException { - if (type != START_TAG) + if (type != START_TAG) { exception("precondition: START_TAG"); + } next(); String result; - if (type == TEXT) { result = getText(); next(); - } - else + } else { result = ""; + } - if (type != END_TAG) + if (type != END_TAG) { exception("END_TAG expected"); + } return result; } - public void setFeature(String feature, boolean value) - throws XmlPullParserException { - if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) + public void setFeature(String feature, boolean value) throws XmlPullParserException { + if (XmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature)) { processNsp = value; - else if (isProp(feature, false, "relaxed")) + } else if (isProp(feature, false, "relaxed")) { relaxed = value; - else + } else { exception("unsupported feature: " + feature); + } } - public void setProperty(String property, Object value) - throws XmlPullParserException { - if(isProp(property, true, "location")) { + public void setProperty(String property, Object value) throws XmlPullParserException { + if (isProp(property, true, "location")) { location = value; } else { throw new XmlPullParserException("unsupported property: " + property); } } - - /** - * Skip sub tree that is currently porser positioned on. - * <br>NOTE: parser must be on START_TAG and when funtion returns - * parser will be positioned on corresponding END_TAG. - */ - - // Implementation copied from Alek's mail... - - public void skipSubTree() throws XmlPullParserException, IOException { - require(START_TAG, null, null); - int level = 1; - while (level > 0) { - int eventType = next(); - if (eventType == END_TAG) { - --level; - } - else if (eventType == START_TAG) { - ++level; - } - } - } } |