KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlcleaner > CompactXmlSerializer


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>Compact XML serializer - creates resulting XML by stripping whitespaces.</p>
46  *
47  * Created by: Vladimir Nikic<br/>
48  * Date: November, 2006.
49  */

50 public class CompactXmlSerializer extends XmlSerializer {
51
52     protected CompactXmlSerializer(Writer JavaDoc writer, HtmlCleaner htmlCleaner) {
53         super(writer, htmlCleaner);
54     }
55
56     private void serialize(List nodes, TagNode tagNode) throws IOException JavaDoc {
57         if ( nodes != null && !nodes.isEmpty() ) {
58             ListIterator childrenIt = nodes.listIterator();
59             while ( childrenIt.hasNext() ) {
60                 Object JavaDoc item = childrenIt.next();
61                 if (item != null) {
62                     if (item instanceof List) {
63                         serialize((List)item, tagNode);
64                     } else if ( item instanceof ContentToken ) {
65                         ContentToken contentToken = (ContentToken) item;
66                         String JavaDoc content = contentToken.getContent().toString().trim();
67                         if ( !dontEscape(tagNode) ) {
68                             content = escapeXml(content).toString();
69                         } else {
70                             content = content.replaceAll("]]>", "]]&amp;");
71                         }
72                         writer.write(content);
73
74                         if (childrenIt.hasNext()) {
75                             if ( isEmptyContentToken(childrenIt.next()) ) {
76                                 writer.write("\n");
77                             }
78                             childrenIt.previous();
79                         }
80                     } else if (item instanceof CommentToken) {
81                         String JavaDoc content = ((CommentToken) item).getContent().toString().trim();
82                         writer.write(content);
83                     } else {
84                         ((BaseToken)item).serialize(this);
85                     }
86                 }
87             }
88         }
89     }
90
91     /**
92      * Checks if specified object is empty content token - it's string
93      * representation is made of spaces or is empty string.
94      * @param token
95      */

96     private boolean isEmptyContentToken(Object JavaDoc token) {
97         if (token instanceof ContentToken) {
98             ContentToken contentToken = (ContentToken) token;
99             if ( !"".equals(contentToken.getContent().toString().trim()) ) {
100                 return true;
101             }
102         }
103
104         return false;
105     }
106
107     protected void serialize(TagNode tagNode) throws IOException JavaDoc {
108         serializeOpenTag(tagNode, false);
109
110         List tagChildren = tagNode.getChildren();
111         if ( !tagChildren.isEmpty() ) {
112             serialize(tagChildren, tagNode);
113             serializeEndTag(tagNode, false);
114         }
115     }
116
117 }
Popular Tags