Filling in some gaps in our XML DOM v3 API.
Specifically, these methods on Node:
- setTextContent()
- isSameNode()
- lookupPrefix()
- lookupNamespaceURI()
In order to implement the last 2 I needed to fix our KXml parser
to include namespace attributes (ie. xmlns) in the pulled document.
Previously these were being elided.
Added a new testcase to verify our behaviour. It passes the RI. On
Dalvik we have a small issue with entity declarations.
Added a new testcase to verify Node.getBaseURI(). This test fails
because the method isn't implemented. Part of this test required
moving a method out to Support_Resources.java; in order to verify
the BaseURI the XML must be read from a file and not a stream (so
that path information exists).
Also...
- Style cleanup: changing static calls to look like static calls.
- Efficiency: avoiding concatenating with "" when unnecessary
- Duplication: sharing prefix validation between attributes and elements
- Renaming NodeTests to NodeTest for vogar-friendliness
Outstanding:
- I need to write a test for setTextContent().
diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java
index c4d8f3d..99eb03b 100644
--- a/xml/src/main/java/org/kxml2/io/KXmlParser.java
+++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java
@@ -45,6 +45,7 @@
private boolean processNsp;
private boolean relaxed;
+ private boolean keepNamespaceAttributes; // android-added
private Hashtable entityMap;
private int depth;
private String[] elementStack = new String[16];
@@ -80,6 +81,14 @@
private boolean degenerated;
private int attributeCount;
+
+ /**
+ * The current element's attributes arranged in groups of 4:
+ * i + 0 = attribute namespace URI
+ * i + 1 = attribute namespace prefix
+ * i + 2 = attribute qualified name (may contain ":", as in "html:h1")
+ * i + 3 = attribute value
+ */
private String[] attributes = new String[16];
// private int stackMismatch = 0;
private String error;
@@ -100,6 +109,19 @@
new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128];
}
+ // 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.
+ */
+ 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/"))
return false;
@@ -148,14 +170,23 @@
//System.out.println (prefixMap);
- System.arraycopy(
- attributes,
- i + 4,
- attributes,
- i,
- ((--attributeCount) << 2) - i);
+ // BEGIN android-changed
+ if (keepNamespaceAttributes) {
+ // explicitly set the namespace for unprefixed attributes
+ // such as xmlns="http://foo"
+ attributes[i] = "http://www.w3.org/2000/xmlns/";
+ any = true;
+ } else {
+ System.arraycopy(
+ attributes,
+ i + 4,
+ attributes,
+ i,
+ ((--attributeCount) << 2) - i);
- i -= 4;
+ i -= 4;
+ }
+ // END android-changed
}
}