001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of HtmlCleaner may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "HtmlCleaner" in the
035: subject line.
036: */
037:
038: package org.htmlcleaner;
039:
040: import java.io.IOException;
041: import java.io.Writer;
042: import java.util.*;
043:
044: /**
045: * <p>Compact XML serializer - creates resulting XML by stripping whitespaces.</p>
046: *
047: * Created by: Vladimir Nikic<br/>
048: * Date: November, 2006.
049: */
050: public class CompactXmlSerializer extends XmlSerializer {
051:
052: protected CompactXmlSerializer(Writer writer,
053: HtmlCleaner htmlCleaner) {
054: super (writer, htmlCleaner);
055: }
056:
057: private void serialize(List nodes, TagNode tagNode)
058: throws IOException {
059: if (nodes != null && !nodes.isEmpty()) {
060: ListIterator childrenIt = nodes.listIterator();
061: while (childrenIt.hasNext()) {
062: Object item = childrenIt.next();
063: if (item != null) {
064: if (item instanceof List) {
065: serialize((List) item, tagNode);
066: } else if (item instanceof ContentToken) {
067: ContentToken contentToken = (ContentToken) item;
068: String content = contentToken.getContent()
069: .toString();
070: boolean isFirstChSpace = Character
071: .isWhitespace(content.charAt(0));
072: boolean isLastChSpace = Character
073: .isWhitespace(content.charAt(content
074: .length() - 1));
075: content = content.trim();
076: if (isFirstChSpace)
077: content = " " + content;
078: if (isLastChSpace)
079: content += " ";
080: //if ( !dontEscape(tagNode) ) {
081: // content = escapeXml(content).toString();
082: //} else {
083: content = content.replaceAll("]]>", "]]&");
084: //}
085: writer.write(" " + content);
086:
087: if (childrenIt.hasNext()) {
088: if (isEmptyContentToken(childrenIt.next())) {
089: writer.write("\n");
090: }
091: childrenIt.previous();
092: }
093: } else if (item instanceof CommentToken) {
094: String content = ((CommentToken) item)
095: .getCommentedContent().toString()
096: .trim();
097: writer.write(content);
098: } else {
099: ((BaseToken) item).serialize(this );
100: }
101: }
102: }
103: }
104: }
105:
106: /**
107: * Checks if specified object is empty content token - it's string
108: * representation is made of spaces or is empty string.
109: * @param token
110: */
111: private boolean isEmptyContentToken(Object token) {
112: if (token instanceof ContentToken) {
113: ContentToken contentToken = (ContentToken) token;
114: if (!"".equals(contentToken.getContent().toString().trim())) {
115: return true;
116: }
117: }
118:
119: return false;
120: }
121:
122: protected void serialize(TagNode tagNode) throws IOException {
123: serializeOpenTag(tagNode, false);
124:
125: List tagChildren = tagNode.getChildren();
126: if (!tagChildren.isEmpty()) {
127: serialize(tagChildren, tagNode);
128: serializeEndTag(tagNode, false);
129: }
130: }
131:
132: }
|