diff options
author | Jesse Wilson <jessewilson@google.com> | 2010-11-19 14:47:30 -0800 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2010-11-19 14:50:52 -0800 |
commit | 3b8cee4de39f9ff8199b4c1fd46cc8037fa45d15 (patch) | |
tree | cbe726232f5d3b7f277ea7ed707b72bf8d55b2f2 /xml/src/main/java/org/kxml2 | |
parent | f0c77ee86dbfc8ec553ca5140d63e4d8bf0d9c1a (diff) | |
download | libcore-3b8cee4de39f9ff8199b4c1fd46cc8037fa45d15.zip libcore-3b8cee4de39f9ff8199b4c1fd46cc8037fa45d15.tar.gz libcore-3b8cee4de39f9ff8199b4c1fd46cc8037fa45d15.tar.bz2 |
Fix problems with entity references and relaxed namespaces.
Change-Id: I5c19ad97c98e6276129ed7228cc38d414b9517db
http://b/3090550
Diffstat (limited to 'xml/src/main/java/org/kxml2')
-rw-r--r-- | xml/src/main/java/org/kxml2/io/KXmlParser.java | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java index 9c20e0d..d3834aa 100644 --- a/xml/src/main/java/org/kxml2/io/KXmlParser.java +++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java @@ -521,15 +521,13 @@ public class KXmlParser implements XmlPullParser { return; } - if (!relaxed) { - if (!name.equals(elementStack[sp + 3])) { - throw new XmlPullParserException( - "expected: /" + elementStack[sp + 3] + " read: " + name, this, null); - } - + if (name.equals(elementStack[sp + 3])) { namespace = elementStack[sp]; prefix = elementStack[sp + 1]; name = elementStack[sp + 2]; + } else if (!relaxed) { + throw new XmlPullParserException( + "expected: /" + elementStack[sp + 3] + " read: " + name, this, null); } } @@ -705,6 +703,7 @@ public class KXmlParser implements XmlPullParser { int c = peekCharacter(); if (c == ';') { + out.append(';'); position++; break; @@ -727,35 +726,36 @@ public class KXmlParser implements XmlPullParser { } } - String code = out.substring(start + 1); - out.delete(start, out.length()); + String code = out.substring(start + 1, out.length() - 1); if (token && type == ENTITY_REF) { name = code; } - if (code.charAt(0) == '#') { - // TODO: check IndexOutOfBoundsException? - // TODO: save an intermediate string for 'code' if unneeded? - int c = code.charAt(1) == 'x' - ? Integer.parseInt(code.substring(2), 16) - : Integer.parseInt(code.substring(1)); - // TODO: set unresolved to false? - out.append((char) c); - return; - } - - String resolved = entityMap.get(code); - if (resolved != null) { - unresolved = false; + String resolved; + if (code.startsWith("#")) { + try { + int c = code.startsWith("#x") + ? Integer.parseInt(code.substring(2), 16) + : Integer.parseInt(code.substring(1)); + out.delete(start, out.length()); + out.appendCodePoint(c); + unresolved = false; + } catch (NumberFormatException notANumber) { + throw new XmlPullParserException("Invalid character reference: &" + code); + } catch (IllegalArgumentException invalidCodePoint) { + throw new XmlPullParserException("Invalid character reference: &" + code); + } + } else if ((resolved = entityMap.get(code)) != null) { + out.delete(start, out.length()); out.append(resolved); - return; - } - - unresolved = true; - if (!token) { - checkRelaxed("unresolved: &" + code + ";"); - // TODO: should the &code; show up in the text in relaxed mode? + unresolved = false; + } else { + // keep the unresolved entity "&code;" in the text for relaxed clients + unresolved = true; + if (!token) { + checkRelaxed("unresolved: &" + code + ";"); + } } } |