DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22054>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22054

It's possible to create a text node with a null byte, resulting in erratic behavior





------- Additional Comments From [EMAIL PROTECTED]  2003-08-04 09:05 -------
Comment:
Shouldn't Xerces always encode binary characters? However, throwing an exception
on parse and not on createTextNode() seems inconsistent to me. If it does not
throw an exception on createTextNode() then it should encode the null-byte (like
it does with "<", ">" and so on).

2nd Test case:
Here comes the test case where you can see that the XML is corrupted. First, an
invalid XML is constructed and then saved to disk with Xalan. In the file, you
can see that the null-byte has been encoded correctly, but the CDATA section is
destroyed. Finally, the invalid XML is being serialized which results in the
same error as in the first test case.
So, you can construct an invalid XML, but you cannot serialize it. Saving it
with Xalan reveals that it is corrupted.
Xerces should either encode the null-byte (or other binary chars) or it should
throw an exception on createTextNode(),like it does on parse(). Silently
constructing an invalid XML is not OK in my opinion.

    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document d;
    Element root, mail, content;
    CDATASection data;
    OutputFormat outputFormat;
    XMLSerializer xmlSerializer;
    StringWriter sw;
    File file;
    TransformerFactory tFactory;
    Transformer transformer;
    StreamResult streamResult;
    BufferedOutputStream bos;
    String encoding = "utf-8";

    dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true); 
    dbf.setAttribute("http://apache.org/xml/features/validation/dynamic";,
                    new Boolean(true));
    dbf.setAttribute("http://apache.org/xml/features/validation/schema";, 
                     new Boolean(true));

    // construct illegal XML (ending with null-byte)
    db = dbf.newDocumentBuilder();
    d = db.newDocument();
    root = d.createElement("root");
    d.appendChild(root);
    data = d.createCDATASection("Test\0");
    root.appendChild(data);
    
    // save with Xalan
    file = new File("C:\\Test.xml");
    bos = new BufferedOutputStream(new FileOutputStream(file));
    if(encoding.equalsIgnoreCase("utf-8")) {
      bos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
    }
    tFactory = TransformerFactory.newInstance();
    transformer = tFactory.newTransformer();      
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    streamResult = new StreamResult(bos);
    transformer.transform(new DOMSource(d), streamResult);
    bos.close();
    
    // toString with XML Serializer
    sw = new StringWriter();
    outputFormat = new OutputFormat();
    outputFormat.setOmitXMLDeclaration(true);
    outputFormat.setEncoding("utf-16");
    outputFormat.setPreserveSpace(true);
    xmlSerializer = new XMLSerializer(sw, outputFormat);
    xmlSerializer.serialize(d);
    System.out.println(sw.toString());

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to