01: /* Copyright (c) 2006-2007, Vladimir Nikic
02: All rights reserved.
03:
04: Redistribution and use of this software in source and binary forms,
05: with or without modification, are permitted provided that the following
06: conditions are met:
07:
08: * Redistributions of source code must retain the above
09: 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;
41: import java.io.Writer;
42: import java.util.*;
43:
44: /**
45: * <p>Simple XML serializer - creates resulting XML without indenting lines.</p>
46: *
47: * Created by: Vladimir Nikic<br/>
48: * Date: November, 2006.
49: */
50: public class SimpleXmlSerializer extends XmlSerializer {
51:
52: protected SimpleXmlSerializer(Writer writer, HtmlCleaner htmlCleaner) {
53: super (writer, htmlCleaner);
54: }
55:
56: private void serialize(List nodes, TagNode tagNode)
57: throws IOException {
58: if (nodes != null && !nodes.isEmpty()) {
59: Iterator childrenIt = nodes.iterator();
60: while (childrenIt.hasNext()) {
61: Object item = childrenIt.next();
62: if (item != null) {
63: if (item instanceof List) {
64: serialize((List) item, tagNode);
65: } else if (item instanceof ContentToken) {
66: ContentToken contentToken = (ContentToken) item;
67: String content = contentToken.getContent()
68: .toString();
69: //if ( !dontEscape(tagNode) ) {
70: // content = escapeXml(content).toString();
71: //} else {
72: content = content.replaceAll("]]>", "]]&");
73: //}
74: writer.write(content);
75: } else {
76: ((BaseToken) item).serialize(this );
77: }
78: }
79: }
80: }
81: }
82:
83: protected void serialize(TagNode tagNode) throws IOException {
84: serializeOpenTag(tagNode);
85:
86: List tagChildren = tagNode.getChildren();
87: if (!tagChildren.isEmpty()) {
88: serialize(tagChildren, tagNode);
89: serializeEndTag(tagNode, false);
90: }
91: }
92:
93: }
|