package org.apache.harmony.xml.dom;
+import org.apache.xml.serializer.dom3.DOMErrorImpl;
import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMStringList;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
import java.util.Map;
import java.util.TreeMap;
}
};
}
+
+ public void normalize(Node node) {
+ /*
+ * Since we don't validate, this code doesn't take into account the
+ * following "supported" parameters: datatype-normalization, entities,
+ * schema-location, schema-type, or validate.
+ *
+ * TODO: normalize namespaces
+ */
+
+ switch (node.getNodeType()) {
+ case Node.CDATA_SECTION_NODE:
+ CDATASectionImpl cdata = (CDATASectionImpl) node;
+ if (cdataSections) {
+ if (cdata.needsSplitting()) {
+ if (splitCdataSections) {
+ cdata.split();
+ report(DOMError.SEVERITY_WARNING, "cdata-sections-splitted");
+ } else {
+ report(DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ checkTextValidity(cdata.buffer);
+ break;
+ }
+ node = cdata.replaceWithText();
+ // fall through
+
+ case Node.TEXT_NODE:
+ TextImpl text = (TextImpl) node;
+ text = text.minimize();
+ if (text != null) {
+ checkTextValidity(text.buffer);
+ }
+ break;
+
+ case Node.COMMENT_NODE:
+ CommentImpl comment = (CommentImpl) node;
+ if (!comments) {
+ comment.getParentNode().removeChild(comment);
+ break;
+ }
+ if (comment.containsDashDash()) {
+ report(DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ checkTextValidity(comment.buffer);
+ break;
+
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ checkTextValidity(((ProcessingInstructionImpl) node).getData());
+ break;
+
+ case Node.ATTRIBUTE_NODE:
+ checkTextValidity(((AttrImpl) node).getValue());
+ break;
+
+ case Node.ELEMENT_NODE:
+ ElementImpl element = (ElementImpl) node;
+ NamedNodeMap attributes = element.getAttributes();
+ for (int i = 0; i < attributes.getLength(); i++) {
+ normalize(attributes.item(i));
+ }
+ // fall through
+
+ case Node.DOCUMENT_NODE:
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ Node next;
+ for (Node child = node.getFirstChild(); child != null; child = next) {
+ // lookup next eagerly because normalize() may remove its subject
+ next = child.getNextSibling();
+ normalize(child);
+ }
+ break;
+
+ case Node.NOTATION_NODE:
+ case Node.DOCUMENT_TYPE_NODE:
+ case Node.ENTITY_NODE:
+ case Node.ENTITY_REFERENCE_NODE:
+ break;
+
+ default:
+ throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
+ "Unsupported node type " + node.getNodeType());
+ }
+ }
+
+ private void checkTextValidity(CharSequence s) {
+ if (wellFormed && !isValid(s)) {
+ report(DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+
+ /**
+ * Returns true if all of the characters in the text are permitted for use
+ * in XML documents.
+ */
+ private boolean isValid(CharSequence text) {
+ for (int i = 0; i < text.length(); i++) {
+ char c = text.charAt(i);
+ // as defined by http://www.w3.org/TR/REC-xml/#charsets.
+ boolean valid = c == 0x9 || c == 0xA || c == 0xD
+ || (c >= 0x20 && c <= 0xd7ff)
+ || (c >= 0xe000 && c <= 0xfffd);
+ if (!valid) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void report(short severity, String type) {
+ if (errorHandler != null) {
+ // TODO: abort if handleError returns false
+ errorHandler.handleError(new DOMErrorImpl(severity, type, type));
+ }
+ }
}
package tests.xml;
import junit.framework.TestCase;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
/**
}
public void testCanonicalForm() {
+ assertEquals(false, domConfiguration.getParameter("canonical-form"));
assertSupported("canonical-form", false);
assertUnsupported("canonical-form", true);
}
public void testCdataSections() {
+ assertEquals(true, domConfiguration.getParameter("cdata-sections"));
assertSupported("cdata-sections", false);
assertSupported("cdata-sections", true);
}
public void testCheckCharacterNormalization() {
+ assertEquals(false, domConfiguration.getParameter("check-character-normalization"));
assertSupported("check-character-normalization", false);
assertUnsupported("check-character-normalization", true);
}
public void testComments() {
+ assertEquals(true, domConfiguration.getParameter("comments"));
assertSupported("comments", false);
assertSupported("comments", true);
}
public void testDatatypeNormalization() {
+ assertEquals(false, domConfiguration.getParameter("datatype-normalization"));
assertSupported("datatype-normalization", false);
assertSupported("datatype-normalization", true);
}
public void testElementContentWhitespace() {
+ assertEquals(true, domConfiguration.getParameter("element-content-whitespace"));
assertUnsupported("element-content-whitespace", false);
assertSupported("element-content-whitespace", true);
}
public void testEntities() {
+ assertEquals(true, domConfiguration.getParameter("entities"));
assertSupported("entities", false);
assertSupported("entities", true);
}
public void testErrorHandler() {
+ assertEquals(null, domConfiguration.getParameter("error-handler"));
assertSupported("error-handler", null);
assertSupported("error-handler", new DOMErrorHandler() {
public boolean handleError(DOMError error) {
}
public void testInfoset() {
+ assertEquals(false, domConfiguration.getParameter("infoset"));
assertSupported("infoset", false);
assertSupported("infoset", true);
}
}
public void testNamespaces() {
+ assertEquals(true, domConfiguration.getParameter("namespaces"));
assertSupported("namespaces", false);
assertSupported("namespaces", true);
}
public void testNamespaceDeclarations() {
+ assertEquals(true, domConfiguration.getParameter("namespace-declarations"));
assertUnsupported("namespace-declarations", false); // supported in RI 6
assertSupported("namespace-declarations", true);
}
public void testNormalizeCharacters() {
+ assertEquals(false, domConfiguration.getParameter("normalize-characters"));
assertSupported("normalize-characters", false);
assertUnsupported("normalize-characters", true);
}
public void testSchemaLocation() {
+ assertEquals(null, domConfiguration.getParameter("schema-location"));
assertSupported("schema-location", "http://foo");
assertSupported("schema-location", null);
}
}
public void testSchemaTypeXmlSchema() {
+ assertEquals(null, domConfiguration.getParameter("schema-type"));
assertSupported("schema-type", null);
assertSupported("schema-type", "http://www.w3.org/2001/XMLSchema");
}
public void testSplitCdataSections() {
+ assertEquals(true, domConfiguration.getParameter("split-cdata-sections"));
assertSupported("split-cdata-sections", false);
assertSupported("split-cdata-sections", true);
}
public void testValidate() {
+ assertEquals(false, domConfiguration.getParameter("validate"));
assertSupported("validate", false);
assertSupported("validate", true);
}
public void testValidateIfSchema() {
+ assertEquals(false, domConfiguration.getParameter("validate-if-schema"));
assertSupported("validate-if-schema", false);
assertUnsupported("validate-if-schema", true);
}
public void testWellFormed() {
+ assertEquals(true, domConfiguration.getParameter("well-formed"));
assertSupported("well-formed", false);
assertSupported("well-formed", true);
}
public void testCdataSectionsNotHonoredByNodeNormalize() throws Exception {
String xml = "<foo>ABC<![CDATA[DEF]]>GHI</foo>";
- document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .parse(new InputSource(new StringReader(xml)));
- document.getDomConfig().setParameter("cdata-sections", true);
+ parse(xml);
+ domConfiguration.setParameter("cdata-sections", true);
document.getDocumentElement().normalize();
assertEquals(xml, domToString(document));
- document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .parse(new InputSource(new StringReader(xml)));
- document.getDomConfig().setParameter("cdata-sections", false);
+ parse(xml);
+ domConfiguration.setParameter("cdata-sections", false);
document.getDocumentElement().normalize();
assertEquals(xml, domToString(document));
}
public void testCdataSectionsHonoredByDocumentNormalize() throws Exception {
String xml = "<foo>ABC<![CDATA[DEF]]>GHI</foo>";
- document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .parse(new InputSource(new StringReader(xml)));
- document.getDomConfig().setParameter("cdata-sections", true);
+ parse(xml);
+ domConfiguration.setParameter("cdata-sections", true);
document.normalizeDocument();
assertEquals(xml, domToString(document));
- document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .parse(new InputSource(new StringReader(xml)));
- document.getDomConfig().setParameter("cdata-sections", false);
+ parse(xml);
+ domConfiguration.setParameter("cdata-sections", false);
document.normalizeDocument();
String expected = xml.replace("<![CDATA[DEF]]>", "DEF");
assertEquals(expected, domToString(document));
assertChildren(document.getDocumentElement(), "<br>", "<br>", "<br>");
}
+ public void testRetainingComments() throws Exception {
+ String xml = "<foo>ABC<!-- bar -->DEF<!-- baz -->GHI</foo>";
+ parse(xml);
+ domConfiguration.setParameter("comments", true);
+ document.normalizeDocument();
+ assertEquals(xml, domToString(document));
+ }
+
+ public void testCommentContainingDoubleDash() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ root.appendChild(document.createComment("ABC -- DEF"));
+ document.normalizeDocument();
+ errorRecorder.assertAllErrors(DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+
+ public void testStrippingComments() throws Exception {
+ String xml = "<foo>ABC<!-- bar -->DEF<!-- baz -->GHI</foo>";
+ parse(xml);
+ domConfiguration.setParameter("comments", false);
+ document.normalizeDocument();
+ assertChildren(document.getDocumentElement(), "ABCDEFGHI");
+ }
+
+ public void testSplittingCdataSectionsSplit() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("split-cdata-sections", true);
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ root.appendChild(document.createCDATASection("ABC]]>DEF]]>GHI"));
+ document.normalizeDocument();
+ errorRecorder.assertAllErrors(DOMError.SEVERITY_WARNING, "cdata-sections-splitted");
+ assertChildren(root, "<![CDATA[ABC]]]]>", "<![CDATA[>DEF]]]]>", "<![CDATA[>GHI]]>");
+ }
+
+ public void testSplittingCdataSectionsReportError() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("split-cdata-sections", false);
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ root.appendChild(document.createCDATASection("ABC]]>DEF"));
+ document.normalizeDocument();
+ errorRecorder.assertAllErrors(DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+
+ public void testInvalidCharactersCdata() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("cdata-sections", true);
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ CDATASection cdata = document.createCDATASection("");
+ root.appendChild(cdata);
+
+ for (int c = 0; c <= Character.MAX_VALUE; c++) {
+ cdata.setData(new String(new char[]{ 'A', 'B', (char) c }));
+ document.normalizeDocument();
+ if (isValid((char) c)) {
+ assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
+ } else {
+ errorRecorder.assertAllErrors("For character " + c,
+ DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ }
+
+ public void testInvalidCharactersText() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ Text text = document.createTextNode("");
+ root.appendChild(text);
+
+ for (int c = 0; c <= Character.MAX_VALUE; c++) {
+ text.setData(new String(new char[]{ 'A', 'B', (char) c }));
+ document.normalizeDocument();
+ if (isValid((char) c)) {
+ assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
+ } else {
+ errorRecorder.assertAllErrors("For character " + c,
+ DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ }
+
+ public void testInvalidCharactersAttribute() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+
+ for (int c = 0; c <= Character.MAX_VALUE; c++) {
+ root.setAttribute("bar", new String(new char[] { 'A', 'B', (char) c}));
+ document.normalizeDocument();
+ if (isValid((char) c)) {
+ assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
+ } else {
+ errorRecorder.assertAllErrors("For character " + c,
+ DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ }
+
+ public void testInvalidCharactersComment() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ Comment comment = document.createComment("");
+ root.appendChild(comment);
+
+ for (int c = 0; c <= Character.MAX_VALUE; c++) {
+ comment.setData(new String(new char[] { 'A', 'B', (char) c}));
+ document.normalizeDocument();
+ if (isValid((char) c)) {
+ assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
+ } else {
+ errorRecorder.assertAllErrors("For character " + c,
+ DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ }
+
+ public void testInvalidCharactersProcessingInstructionData() throws Exception {
+ ErrorRecorder errorRecorder = new ErrorRecorder();
+ domConfiguration.setParameter("error-handler", errorRecorder);
+ domConfiguration.setParameter("namespaces", false);
+ Element root = document.createElement("foo");
+ document.appendChild(root);
+ ProcessingInstruction pi = document.createProcessingInstruction("foo", "");
+ root.appendChild(pi);
+
+ for (int c = 0; c <= Character.MAX_VALUE; c++) {
+ pi.setData(new String(new char[] { 'A', 'B', (char) c}));
+ document.normalizeDocument();
+ if (isValid((char) c)) {
+ assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
+ } else {
+ errorRecorder.assertAllErrors("For character " + c,
+ DOMError.SEVERITY_ERROR, "wf-invalid-character");
+ }
+ }
+ }
+
+ // TODO: test for surrogates
+
+ private boolean isValid(char c) {
+ // as defined by http://www.w3.org/TR/REC-xml/#charsets.
+ return c == 0x9 || c == 0xA || c == 0xD || (c >= 0x20 && c <= 0xd7ff)
+ || (c >= 0xe000 && c <= 0xfffd);
+ }
+
private Document createDocumentWithAdjacentTexts(String... texts) throws Exception {
Document result = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
- actual.add(node.getNodeType() == Node.TEXT_NODE
- ? ((Text) node).getData()
- : "<" + node.getNodeName() + ">");
+ if (node.getNodeType() == Node.TEXT_NODE) {
+ actual.add(((Text) node).getData());
+ } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
+ actual.add("<![CDATA[" + ((CDATASection) node).getData() + "]]>");
+ } else {
+ actual.add("<" + node.getNodeName() + ">");
+ }
}
assertEquals(Arrays.asList(texts), actual);
}
+ private void parse(String xml) throws Exception {
+ document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ .parse(new InputSource(new StringReader(xml)));
+ domConfiguration = document.getDomConfig();
+ }
+
private String domToString(Document document) throws TransformerException {
StringWriter writer = new StringWriter();
TransformerFactory.newInstance().newTransformer()
String xml = writer.toString();
return xml.replaceFirst("<\\?xml[^?]*\\?>", "");
}
+
+ private class ErrorRecorder implements DOMErrorHandler {
+ private final List<DOMError> errors = new ArrayList<DOMError>();
+
+ public boolean handleError(DOMError error) {
+ errors.add(error);
+ return true;
+ }
+
+ public void assertAllErrors(int severity, String type) {
+ assertAllErrors("Expected one or more " + type + " errors", severity, type);
+ }
+
+ public void assertAllErrors(String message, int severity, String type) {
+ assertFalse(message, errors.isEmpty());
+ for (DOMError error : errors) {
+ assertEquals(message, severity, error.getSeverity());
+ assertEquals(message, type, error.getType());
+ }
+ errors.clear();
+ }
+ }
}