KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > DOMUtilitiesCharacterEscaping


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

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 /**
36  * Checks that Text nodes can be properly written and read.
37  * This test creates a Document with a CDATA section and checks
38  * that the CDATA section content can be written out and then read
39  * without being altered.
40  *
41  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
42  */

43 public class DOMUtilitiesCharacterEscaping extends AbstractTest {
44     public TestReport runImpl() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc docString = stringWriter.toString();
65         System.err.println(">>>>>>>>>>> Document content \n\n" + docString + "\n\n<<<<<<<<<<<<<<<<");
66
67         String JavaDoc 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