#62006 revisited: make fix work even with top-level comments etc. version-2-3-52
authorjglick@netbeans.org
Wed, 12 Oct 2005 00:38:53 +0000
changeset 70a557a8b68a58
parent 69 617368fcbdd3
child 71 684a884b2c22
#62006 revisited: make fix work even with top-level comments etc.
openide.util/src/org/openide/xml/XMLUtil.java
openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java
openide.util/test/unit/src/org/openide/xml/random.dtd
     1.1 --- a/openide.util/src/org/openide/xml/XMLUtil.java	Wed Oct 05 00:53:06 2005 +0000
     1.2 +++ b/openide.util/src/org/openide/xml/XMLUtil.java	Wed Oct 12 00:38:53 2005 +0000
     1.3 @@ -706,10 +706,6 @@
     1.4       * @see "#62006"
     1.5       */
     1.6      private static Document normalize(Document orig) throws IOException {
     1.7 -        if (orig.getChildNodes().getLength() != 1) {
     1.8 -            // Don't mess around with it.
     1.9 -            return orig;
    1.10 -        }
    1.11          DocumentBuilder builder = (DocumentBuilder) builderTL[0].get();
    1.12          if (builder == null) {
    1.13              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    1.14 @@ -722,10 +718,35 @@
    1.15              }
    1.16              builderTL[0].set(builder);
    1.17          }
    1.18 -        Document doc = builder.newDocument();
    1.19 -        doc.appendChild(doc.importNode(orig.getDocumentElement(), true));
    1.20 +        DocumentType doctype = null;
    1.21 +        NodeList nl = orig.getChildNodes();
    1.22 +        for (int i = 0; i < nl.getLength(); i++) {
    1.23 +            if (nl.item(i) instanceof DocumentType) {
    1.24 +                // We cannot import DocumentType's, so we need to manually copy it.
    1.25 +                doctype = (DocumentType) nl.item(i);
    1.26 +            }
    1.27 +        }
    1.28 +        Document doc;
    1.29 +        if (doctype != null) {
    1.30 +            doc = builder.getDOMImplementation().createDocument(
    1.31 +                orig.getDocumentElement().getNamespaceURI(),
    1.32 +                orig.getDocumentElement().getTagName(),
    1.33 +                builder.getDOMImplementation().createDocumentType(
    1.34 +                    orig.getDoctype().getName(),
    1.35 +                    orig.getDoctype().getPublicId(),
    1.36 +                    orig.getDoctype().getSystemId()));
    1.37 +            // XXX what about entity decls inside the DOCTYPE?
    1.38 +            doc.removeChild(doc.getDocumentElement());
    1.39 +        } else {
    1.40 +            doc = builder.newDocument();
    1.41 +        }
    1.42 +        for (int i = 0; i < nl.getLength(); i++) {
    1.43 +            if (!(nl.item(i) instanceof DocumentType)) {
    1.44 +                doc.appendChild(doc.importNode(nl.item(i), true));
    1.45 +            }
    1.46 +        }
    1.47          doc.normalize();
    1.48 -        NodeList nl = doc.getElementsByTagName("*"); // NOI18N
    1.49 +        nl = doc.getElementsByTagName("*"); // NOI18N
    1.50          for (int i = 0; i < nl.getLength(); i++) {
    1.51              Element e = (Element) nl.item(i);
    1.52              NodeList nl2 = e.getChildNodes();
     2.1 --- a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java	Wed Oct 05 00:53:06 2005 +0000
     2.2 +++ b/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java	Wed Oct 12 00:38:53 2005 +0000
     2.3 @@ -284,6 +284,12 @@
     2.4      public void testIndentation2() throws Exception {
     2.5          String data =
     2.6                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
     2.7 +                "<!--\n" +
     2.8 +                "Some license or whatever.\n" +
     2.9 +                "-->\n" +
    2.10 +                "<?stylesheet location=\"here\"?>\n" +
    2.11 +                "<!DOCTYPE p PUBLIC \"random DTD\" \"" + XMLUtilTest.class.getResource("random.dtd") + "\">\n" +
    2.12 +                "\n" + // XXX currently it seems that the serializer adds an extra \n after DOCTYPE, for no apparent reason!
    2.13                  "<p>\n" +
    2.14                  "    <t/>\n" +
    2.15                  "    <c>\n" +
    2.16 @@ -301,7 +307,7 @@
    2.17          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    2.18          XMLUtil.write(doc, baos, "UTF-8");
    2.19          String data2 = baos.toString().replaceAll("\r\n", "\n");
    2.20 -        assertEquals("identity replacement should not mess up indentation", data, data2);
    2.21 +        assertEquals("identity replacement should not mess up indentation in \n" + data2, data, data2);
    2.22      }
    2.23      
    2.24      public void testSignificantWhitespace() throws Exception {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/openide.util/test/unit/src/org/openide/xml/random.dtd	Wed Oct 12 00:38:53 2005 +0000
     3.3 @@ -0,0 +1,2 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<!ELEMENT p ANY>