KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlcleaner > XmlSerializer


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3     
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7     
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11     
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16     
17     * The name of HtmlCleaner may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32     
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "HtmlCleaner" in the
35     subject line.
36 */

37
38 package org.htmlcleaner;
39
40 import java.io.BufferedWriter JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * <p>Abstract XML serializer - contains common logic for descendants.</p>
49  *
50  * Created by: Vladimir Nikic<br/>
51  * Date: November, 2006.
52  */

53 public abstract class XmlSerializer {
54     
55     protected final String JavaDoc XML_DECLARATION = "<?xml version=\"1.0\"?>";
56     
57     protected HtmlCleaner htmlCleaner;
58     protected BufferedWriter JavaDoc writer;
59     
60     protected XmlSerializer() {
61     }
62     
63     protected XmlSerializer(Writer JavaDoc writer, HtmlCleaner htmlCleaner) {
64         this.writer = new BufferedWriter JavaDoc(writer);
65         this.htmlCleaner = htmlCleaner;
66     }
67     
68     protected void createXml(TagNode tagNode) throws IOException JavaDoc {
69         if ( !htmlCleaner.isOmitXmlDeclaration() ) {
70             writer.write(XML_DECLARATION + "\n");
71         }
72         
73         if ( !htmlCleaner.isOmitDoctypeDeclaration() ) {
74             DoctypeToken doctypeToken = htmlCleaner.getDoctype();
75             if ( doctypeToken != null ) {
76                 doctypeToken.serialize(this);
77             }
78         }
79         
80         serialize(tagNode);
81
82         writer.flush();
83         writer.close();
84     }
85     
86     protected String JavaDoc escapeXml(String JavaDoc xmlContent) {
87         return Utils.escapeXml(
88                     xmlContent,
89                     htmlCleaner.isAdvancedXmlEscape(),
90                     htmlCleaner.isRecognizeUnicodeChars(),
91                     htmlCleaner.isTranslateSpecialEntities()
92                );
93     }
94     
95     protected boolean dontEscape(TagNode tagNode) {
96         String JavaDoc tagName = tagNode.getName();
97         return htmlCleaner.isUseCdataForScriptAndStyle() && ("script".equalsIgnoreCase(tagName) || "style".equalsIgnoreCase(tagName));
98     }
99     
100     protected boolean isScriptOrStyle(TagNode tagNode) {
101         String JavaDoc tagName = tagNode.getName();
102         return "script".equalsIgnoreCase(tagName) || "style".equalsIgnoreCase(tagName);
103     }
104     
105     protected void serializeOpenTag(TagNode tagNode, boolean newLine) throws IOException JavaDoc {
106         String JavaDoc tagName = tagNode.getName();
107         Map JavaDoc tagAtttributes = tagNode.getAttributes();
108         List JavaDoc tagChildren = tagNode.getChildren();
109         
110         writer.write("<" + tagName);
111         Iterator JavaDoc it = tagAtttributes.keySet().iterator();
112         while (it.hasNext()) {
113             String JavaDoc attName = (String JavaDoc) it.next();
114             String JavaDoc attValue = (String JavaDoc) tagAtttributes.get(attName);
115             
116             if ( htmlCleaner.isOmitXmlnsAttributes() && "xmlns".equals(attName) ) {
117                 continue;
118             }
119             
120             writer.write(" " + attName + "=\"" + escapeXml(attValue) + "\"");
121         }
122         
123         if ( tagChildren.size() == 0 ) {
124             writer.write("/>");
125             if (newLine) {
126                 writer.write("\n");
127             }
128         } else if (dontEscape(tagNode)) {
129             writer.write("><![CDATA[");
130         } else {
131             writer.write(">");
132         }
133     }
134     
135     protected void serializeOpenTag(TagNode tagNode) throws IOException JavaDoc {
136         serializeOpenTag(tagNode, true);
137     }
138     
139     protected void serializeEndTag(TagNode tagNode, boolean newLine) throws IOException JavaDoc {
140         String JavaDoc tagName = tagNode.getName();
141         
142         if (dontEscape(tagNode)) {
143             writer.write("]]>");
144         }
145         
146         writer.write( "</" + tagName + ">" );
147
148         if (newLine) {
149             writer.write("\n");
150         }
151     }
152
153     Writer JavaDoc getWriter() {
154         return writer;
155     }
156     
157     protected void serializeEndTag(TagNode tagNode) throws IOException JavaDoc {
158         serializeEndTag(tagNode, true);
159     }
160
161
162     protected abstract void serialize(TagNode tagNode) throws IOException JavaDoc;
163     
164 }
Popular Tags