1 18 package org.apache.batik.dom; 19 20 import org.apache.batik.test.AbstractTest; 21 import org.apache.batik.test.TestReport; 22 23 import org.apache.batik.util.SVGConstants; 24 import org.apache.batik.dom.svg.SVGDOMImplementation; 25 26 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 27 import org.apache.batik.dom.util.DOMUtilities; 28 29 import org.apache.batik.util.XMLResourceDescriptor; 30 31 import org.w3c.dom.*; 32 33 import java.io.*; 34 35 43 public class DOMUtilitiesCharacterEscaping extends AbstractTest { 44 public TestReport runImpl() throws Exception { 45 DOMImplementation impl = new SVGDOMImplementation(); 46 Document doc = impl.createDocument(SVGConstants.SVG_NAMESPACE_URI, 47 "svg", null); 48 49 Element svg = doc.getDocumentElement(); 50 Element text = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 51 "text"); 52 svg.appendChild(text); 53 54 text.setAttributeNS(null, "id", "myText"); 55 String unescapedContent = "You should not escape: & # \" ..."; 56 CDATASection cdata = doc.createCDATASection(unescapedContent); 57 58 text.appendChild(cdata); 59 60 Writer stringWriter = new StringWriter(); 61 62 DOMUtilities.writeDocument(doc, stringWriter); 63 64 String docString = stringWriter.toString(); 65 System.err.println(">>>>>>>>>>> Document content \n\n" + docString + "\n\n<<<<<<<<<<<<<<<<"); 66 67 String parser = XMLResourceDescriptor.getXMLParserClassName(); 68 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); 69 doc = f.createDocument("http://xml.apache.org/batik/foo.svg", 70 new StringReader(stringWriter.toString())); 71 72 text = doc.getElementById("myText"); 73 cdata = (CDATASection)text.getFirstChild(); 74 if (cdata.getData().equals(unescapedContent)) { 75 return reportSuccess(); 76 } 77 78 TestReport report = reportError("Unexpected CDATA read-back"); 79 report.addDescriptionEntry("expected cdata", unescapedContent); 80 report.addDescriptionEntry("actual cdata", cdata.getData()); 81 return report; 82 } 83 } 84 | Popular Tags |