You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Ricardo Amador (JIRA)" <xe...@xml.apache.org> on 2006/11/23 08:42:02 UTC

[jira] Created: (XERCESJ-1216) CDATA not closed when serializing a DocumentFragment

CDATA not closed when serializing a DocumentFragment
----------------------------------------------------

                 Key: XERCESJ-1216
                 URL: http://issues.apache.org/jira/browse/XERCESJ-1216
             Project: Xerces2-J
          Issue Type: Bug
          Components: Serialization
    Affects Versions: 2.7.1, 2.8.1
            Reporter: Ricardo Amador


org.apache.xml.serialize.XMLSerializer merges a  sequence of CDATA/Text nodes started by a CDATA node into a single CDATA, no problem here (although I wasn't expecting it). The problem occurs when this sequence is placed in the end of a DocumentFragment and you serialize it. The CDATA section is not properly closed. For instance the following code writes 'txt.<![CDATA[.cdt..txt..cdt.' instead of '.txt.<![CDATA[.cdt..txt..cdt.]]>':

	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	Document d = db.newDocument();
	DocumentFragment df = d.createDocumentFragment();
	df.appendChild(d.createTextNode(".txt."));
	df.appendChild(d.createCDATASection(".cdt."));
	df.appendChild(d.createTextNode(".txt."));
	df.appendChild(d.createCDATASection(".cdt."));
	XMLSerializer ser = new XMLSerializer(System.out, new OutputFormat());
	ser.serialize(df);

It is possible to work around this problem by subclassing XMLSerializer in the following manner:

public class XMLSerializer extends org.apache.xml.serialize.XMLSerializer {

	public XMLSerializer() { super(); }
	public XMLSerializer(OutputFormat arg0) { super(arg0); }
	public XMLSerializer(Writer arg0, OutputFormat arg1) { super(arg0, arg1); }
	public XMLSerializer(OutputStream arg0, OutputFormat arg1) { super(arg0, arg1); }

	public void serialize(DocumentFragment df) throws IOException {
		super.serialize(df);
		ElementState state = getElementState();
		if (state.inCData && !state.doCData) {
			// CDATA properly closed, but still open in the Printer
			_printer.printText( "]]>" );
			state.inCData = false;
			_printer.flush();
			if ( _printer.getException() != null )
				throw _printer.getException();
		}
	}
}


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org