-
Bug
-
Resolution: Fixed
-
P2
-
5.0u4
-
b60
-
x86
-
linux
Compile and run the following class:
---%<---
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class TestAppendElementToDocument {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
String xml = "<root/>";
Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.appendChild(doc.createElement("child"));
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
---%<---
Under JDK 1.4 the bogus call to appendChild is handled reasonably by Crimson:
---%<---
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
Exception in thread "main" org.apache.crimson.tree.DomEx: HIERARCHY_REQUEST_ERR: This node isn't allowed there.
at org.apache.crimson.tree.XmlDocument.appendChild(XmlDocument.java:661)
at TestAppendElementToDocument.main(TestAppendElementToDocument.java:16)
---%<---
But under JDK 1.5/1.6, using Xerces, the DOM impl quietly lets you create a malformed Document, and even lets you serialize it to produce malformed XML:
---%<---
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
<?xml version="1.0" encoding="UTF-8"?>
<root/>
<child/>
---%<---
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b45)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b45, mixed mode, sharing)
Warning: Could not get charToByteConverterClass!
<?xml version="1.0" encoding="UTF-8"?>
<root/>
<child/>
---%<---
Of course you should not have been making such a call to begin with, but it would be much preferable to have the error be caught as soon as possible.
Just found that the DOM Level 3 Core spec specifically requires an exception in this case:
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-184E7107
"HIERARCHY_REQUEST_ERR: Raised if [...] this node is of type Document and the DOM application attempts to append a second DocumentType or Element node."
Might deserve a higher severity, since it is an apparent violation of the DOM spec.
---%<---
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class TestAppendElementToDocument {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
String xml = "<root/>";
Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.appendChild(doc.createElement("child"));
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
---%<---
Under JDK 1.4 the bogus call to appendChild is handled reasonably by Crimson:
---%<---
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
Exception in thread "main" org.apache.crimson.tree.DomEx: HIERARCHY_REQUEST_ERR: This node isn't allowed there.
at org.apache.crimson.tree.XmlDocument.appendChild(XmlDocument.java:661)
at TestAppendElementToDocument.main(TestAppendElementToDocument.java:16)
---%<---
But under JDK 1.5/1.6, using Xerces, the DOM impl quietly lets you create a malformed Document, and even lets you serialize it to produce malformed XML:
---%<---
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
<?xml version="1.0" encoding="UTF-8"?>
<root/>
<child/>
---%<---
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b45)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b45, mixed mode, sharing)
Warning: Could not get charToByteConverterClass!
<?xml version="1.0" encoding="UTF-8"?>
<root/>
<child/>
---%<---
Of course you should not have been making such a call to begin with, but it would be much preferable to have the error be caught as soon as possible.
Just found that the DOM Level 3 Core spec specifically requires an exception in this case:
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-184E7107
"HIERARCHY_REQUEST_ERR: Raised if [...] this node is of type Document and the DOM application attempts to append a second DocumentType or Element node."
Might deserve a higher severity, since it is an apparent violation of the DOM spec.