From 6bcf32ab404c39b85d25430f6df16503ef3526cf Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 16 Nov 2009 21:23:11 -0800 Subject: Various XML fixes. Add tests for bug 2487, exploring the situations where "]]>" is and isn't allowed. Fix the bug by changing KXmlParser so it pays attention to whether it's dealing with normal text or text in an attribute value and reports errors appropriately. In order to pass the new tests, we also need to fix DocumentBuilder to pay attention to its DocumentBuilderFactory's "coalescing" setting: whether or not adjacent text/CDATA nodes should be coalesced. This in turn fixes a @KnownFailure in DocumentBuilderFactoryTest: previously we didn't allow the caller to turn "coalescing" off (though until my previous patch, we didn't actually coalesce anyway). Now we support both, and I've made coalescing the default, because bug reports tell us that's what users want. It's how the RI behaves, too. Bug: 2487 --- xml/src/main/java/org/kxml2/io/KXmlParser.java | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'xml/src/main/java/org/kxml2') diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java index 98aae04..c4d8f3d 100644 --- a/xml/src/main/java/org/kxml2/io/KXmlParser.java +++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java @@ -319,7 +319,9 @@ public class KXmlParser implements XmlPullParser { return; case TEXT : - pushText('<', !token); + // BEGIN android-changed: distinguish attribute values from normal text. + pushText('<', !token, false); + // END android-changed if (depth == 0) { if (isWhitespace) type = IGNORABLE_WHITESPACE; @@ -680,7 +682,9 @@ public class KXmlParser implements XmlPullParser { read(); int p = txtPos; - pushText(delimiter, true); + // BEGIN android-changed: distinguish attribute values from normal text. + pushText(delimiter, true, true); + // END android-changed attributes[i] = get(p); txtPos = p; @@ -800,7 +804,7 @@ public class KXmlParser implements XmlPullParser { ' ': parse to whitespace or '>' */ - private final void pushText(int delimiter, boolean resolveEntities) + private final void pushText(int delimiter, boolean resolveEntities, boolean inAttributeValue) throws IOException, XmlPullParserException { int next = peek(0); @@ -812,21 +816,31 @@ public class KXmlParser implements XmlPullParser { if (next <= ' ' || next == '>') break; + // BEGIN android-changed: "<" is not allowed in attribute values. if (next == '&') { if (!resolveEntities) break; pushEntity(); } + else if (next == '<' && inAttributeValue) { + error("Illegal: \"<\" inside attribute value"); + } else if (next == '\n' && type == START_TAG) { read(); push(' '); } else push(read()); + // END android-changed - if (next == '>' && cbrCount >= 2 && delimiter != ']') - error("Illegal: ]]>"); + // BEGIN android-changed: "]]>" *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 == ']') cbrCount++; -- cgit v1.1