#160806: problems with validating and writing entity includes.
authorJesse Glick <jglick@netbeans.org>
Mon, 06 Apr 2009 14:24:49 -0400
changeset 528cf69b3714944
parent 526 519bef079589
child 529 92a0f73a01c3
#160806: problems with validating and writing entity includes.
openide.util/src/org/openide/xml/XMLUtil.java
openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java
     1.1 --- a/openide.util/src/org/openide/xml/XMLUtil.java	Fri Mar 20 16:46:35 2009 +0100
     1.2 +++ b/openide.util/src/org/openide/xml/XMLUtil.java	Mon Apr 06 14:24:49 2009 -0400
     1.3 @@ -415,7 +415,10 @@
     1.4                  if (pub != null) {
     1.5                      t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
     1.6                  }
     1.7 -                t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
     1.8 +                String sys = dt.getSystemId();
     1.9 +                if (sys != null) {
    1.10 +                    t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, sys);
    1.11 +                }
    1.12              }
    1.13              t.setOutputProperty(OutputKeys.ENCODING, enc);
    1.14  
    1.15 @@ -512,7 +515,7 @@
    1.16          return copy;
    1.17      }
    1.18      private static void fixupAttrsSingle(Element e) throws DOMException {
    1.19 -        e.removeAttributeNS("http://www.w3.org/XML/1998/namespace", "base"); // NOI18N
    1.20 +        removeXmlBase(e);
    1.21          Map<String, String> replace = new HashMap<String, String>();
    1.22          NamedNodeMap attrs = e.getAttributes();
    1.23          for (int j = 0; j < attrs.getLength(); j++) {
    1.24 @@ -526,6 +529,10 @@
    1.25              e.setAttributeNS(null, entry.getKey(), entry.getValue());
    1.26          }
    1.27      }
    1.28 +    private static void removeXmlBase(Element e) {
    1.29 +        e.removeAttributeNS("http://www.w3.org/XML/1998/namespace", "base"); // NOI18N
    1.30 +        e.removeAttribute("xml:base"); // NOI18N
    1.31 +    }
    1.32  
    1.33      /**
    1.34       * Escape passed string as XML attibute value
    1.35 @@ -817,6 +824,7 @@
    1.36          nl = doc.getElementsByTagName("*"); // NOI18N
    1.37          for (int i = 0; i < nl.getLength(); i++) {
    1.38              Element e = (Element) nl.item(i);
    1.39 +            removeXmlBase(e);
    1.40              NodeList nl2 = e.getChildNodes();
    1.41              for (int j = 0; j < nl2.getLength(); j++) {
    1.42                  Node n = nl2.item(j);
     2.1 --- a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java	Fri Mar 20 16:46:35 2009 +0100
     2.2 +++ b/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java	Mon Apr 06 14:24:49 2009 -0400
     2.3 @@ -190,25 +190,6 @@
     2.4              XMLUtil.validate(r, s);
     2.5              fail();
     2.6          } catch (SAXException x) {/*OK*/}
     2.7 -        // #146081: xml:base attributes get inserted sometimes and can mess up validation.
     2.8 -        xsd =
     2.9 -                "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' targetNamespace='some://where' xmlns='some://where' elementFormDefault='qualified'>\n" +
    2.10 -                " <xsd:element name='root'>\n" +
    2.11 -                "  <xsd:complexType>\n" +
    2.12 -                "   <xsd:sequence>\n" +
    2.13 -                "    <xsd:element name='hello' type='xsd:NMTOKEN'/>\n" +
    2.14 -                "   </xsd:sequence>\n" +
    2.15 -                "  </xsd:complexType>\n" +
    2.16 -                " </xsd:element>\n" +
    2.17 -                "</xsd:schema>\n";
    2.18 -        s = f.newSchema(new StreamSource(new StringReader(xsd)));
    2.19 -        File d = getWorkDir();
    2.20 -        File main = new File(d, "main.xml");
    2.21 -        File ent = new File(d, "ent.xml");
    2.22 -        TestFileUtils.writeFile(main, "<!DOCTYPE root [<!ENTITY ent SYSTEM 'ent.xml'>]> <root xmlns='some://where'>&ent;</root>");
    2.23 -        TestFileUtils.writeFile(ent, "<hello xmlns='some://where'>there</hello>");
    2.24 -        r = XMLUtil.parse(new InputSource(main.toURI().toString()), false, true, null, null).getDocumentElement();
    2.25 -        XMLUtil.validate(r, s);
    2.26      }
    2.27      
    2.28      public void testToAttributeValue() throws IOException {
    2.29 @@ -460,4 +441,46 @@
    2.30          assertTrue("Expecting CDATASection node", child instanceof CDATASection);
    2.31          assertEquals("Wrong CDATASection content", cdataContent, ((CDATASection) child).getNodeValue());
    2.32      }
    2.33 +
    2.34 +    public void testEntityIncludes() throws Exception {
    2.35 +        clearWorkDir();
    2.36 +        // #146081: xml:base attributes get inserted sometimes and can mess up validation.
    2.37 +        // #160806: problems writing: no system ID, xml:base inserted.
    2.38 +        SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    2.39 +        String xsd =
    2.40 +                "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' targetNamespace='some://where' xmlns='some://where' elementFormDefault='qualified'>\n" +
    2.41 +                " <xsd:element name='root'>\n" +
    2.42 +                "  <xsd:complexType>\n" +
    2.43 +                "   <xsd:sequence>\n" +
    2.44 +                "    <xsd:element name='hello' type='xsd:NMTOKEN'/>\n" +
    2.45 +                "   </xsd:sequence>\n" +
    2.46 +                "  </xsd:complexType>\n" +
    2.47 +                " </xsd:element>\n" +
    2.48 +                "</xsd:schema>\n";
    2.49 +        Schema s = f.newSchema(new StreamSource(new StringReader(xsd)));
    2.50 +        File d = getWorkDir();
    2.51 +        File main = new File(d, "main.xml");
    2.52 +        File ent = new File(d, "ent.xml");
    2.53 +        TestFileUtils.writeFile(main, "<!DOCTYPE root [<!ENTITY ent SYSTEM 'ent.xml'>]> <root xmlns='some://where'>&ent;</root>");
    2.54 +        TestFileUtils.writeFile(ent, "<hello xmlns='some://where'>there</hello>");
    2.55 +        Document doc = XMLUtil.parse(new InputSource(main.toURI().toString()), false, true, null, null);
    2.56 +        XMLUtil.validate(doc.getDocumentElement(), s);
    2.57 +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    2.58 +        XMLUtil.write(doc, baos, "UTF-8");
    2.59 +        String expanded =
    2.60 +                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    2.61 +                "<root xmlns=\"some://where\">\n" +
    2.62 +                "    <hello>there</hello>\n" +
    2.63 +                "</root>\n";
    2.64 +        assertEquals(expanded, baos.toString("UTF-8"));
    2.65 +        // XXX #160806 reported a problem with "xml:base" being consider a no-NS attr; not yet caught by test
    2.66 +        // Try again with no xmlns specified in entity; should inherit from main.xml:
    2.67 +        TestFileUtils.writeFile(ent, "<hello>there</hello>");
    2.68 +        doc = XMLUtil.parse(new InputSource(main.toURI().toString()), false, true, null, null);
    2.69 +        XMLUtil.validate(doc.getDocumentElement(), s);
    2.70 +        baos = new ByteArrayOutputStream();
    2.71 +        XMLUtil.write(doc, baos, "UTF-8");
    2.72 +        assertEquals(expanded, baos.toString("UTF-8"));
    2.73 +    }
    2.74 +
    2.75  }