KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlcleaner > PrettyXmlSerializer


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.IOException JavaDoc;
41 import java.io.Writer JavaDoc;
42 import java.util.*;
43
44 /**
45  * <p>Pretty XML serializer - creates resulting XML with indenting lines.</p>
46  *
47  * Created by: Vladimir Nikic<br/>
48  * Date: November, 2006.
49  */

50 public class PrettyXmlSerializer extends XmlSerializer {
51     
52     private static final String JavaDoc INDENTATION_STRING = "\t";
53
54     protected PrettyXmlSerializer(Writer JavaDoc writer, HtmlCleaner htmlCleaner) {
55         super(writer, htmlCleaner);
56     }
57     
58     protected void serialize(TagNode tagNode) throws IOException JavaDoc {
59         serializePrettyXml(tagNode, 0);
60     }
61
62     /**
63      * @param level
64      * @return Appropriate indentation for the specified depth.
65      */

66     private String JavaDoc indent(int level) {
67         String JavaDoc result = "";
68         while (level > 0) {
69             result += INDENTATION_STRING;
70             level--;
71         }
72
73         return result;
74     }
75
76     private String JavaDoc getIndentedText(String JavaDoc content, int level) {
77         String JavaDoc indent = indent(level);
78         StringBuffer JavaDoc result = new StringBuffer JavaDoc( content.length() );
79         StringTokenizer tokenizer = new StringTokenizer(content, "\n\r");
80
81         while (tokenizer.hasMoreTokens()) {
82             String JavaDoc line = tokenizer.nextToken().trim();
83             if (!"".equals(line)) {
84                 result.append(indent + line + "\n");
85             }
86         }
87
88         return result.toString();
89     }
90
91     private String JavaDoc getSingleLineOfChildren(List children) {
92         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
93         Iterator childrenIt = children.iterator();
94         boolean isFirst = true;
95
96         while (childrenIt.hasNext()) {
97             Object JavaDoc child = childrenIt.next();
98             
99             if ( !(child instanceof ContentToken) ) {
100                 return null;
101             } else {
102                 ContentToken contentToken = (ContentToken) child;
103                 String JavaDoc content = contentToken.getContent().toString();
104
105                 // if first item trims it from left
106
if (isFirst) {
107                     content = Utils.ltrim(content);
108                 }
109                 
110                 // if last item trims it from right
111
if (!childrenIt.hasNext()) {
112                     content = Utils.rtrim(content);
113                 }
114                 
115                 if ( content.indexOf("\n") >= 0 || content.indexOf("\r") >= 0 ) {
116                     return null;
117                 }
118                 result.append(content);
119             }
120             
121             isFirst = false;
122         }
123
124         return result.toString();
125     }
126     
127     private void serializePrettyXml(List nodes, int level, boolean dontEscape) throws IOException JavaDoc {
128         Iterator childrenIt = nodes.iterator();
129         while (childrenIt.hasNext()) {
130             Object JavaDoc child = childrenIt.next();
131             if (child instanceof TagNode) {
132                 serializePrettyXml( (TagNode)child, level + 1 );
133             } else if (child instanceof ContentToken) {
134                 ContentToken contentToken = (ContentToken) child;
135                 String JavaDoc content = contentToken.getContent();
136                 if ( !dontEscape ) {
137                     content = escapeXml(content).toString();
138                 } else {
139                     content = content.replaceAll("]]>", "]]&amp;");
140                 }
141                 writer.write( getIndentedText(content, level + 1) );
142             } else if (child instanceof CommentToken) {
143                 CommentToken commentToken = (CommentToken) child;
144                 String JavaDoc content = commentToken.getContent();
145                 writer.write( getIndentedText(content, level + 1) );
146             } else if (child instanceof List) {
147                 serializePrettyXml( (List)child, level, true );
148             }
149         }
150     }
151
152     protected void serializePrettyXml(TagNode tagNode, int level) throws IOException JavaDoc {
153         List tagChildren = tagNode.getChildren();
154         String JavaDoc indent = indent(level);
155
156         writer.write(indent);
157         serializeOpenTag(tagNode);
158
159         if ( !tagChildren.isEmpty() ) {
160             String JavaDoc singleLine = getSingleLineOfChildren(tagChildren);
161             boolean dontEscape = dontEscape(tagNode);
162             if (singleLine != null) {
163                 if ( !dontEscape(tagNode) ) {
164                     writer.write( escapeXml(singleLine) );
165                 } else {
166                     writer.write( singleLine.replaceAll("]]>", "]]&amp;") );
167                 }
168             } else {
169                 writer.write("\n");
170                 serializePrettyXml(tagChildren, level, dontEscape);
171             }
172
173             if (singleLine == null) {
174                 writer.write(indent);
175             }
176             
177             serializeEndTag(tagNode);
178         }
179     }
180
181 }
Popular Tags