1 37 38 package org.htmlcleaner; 39 40 import java.io.IOException ; 41 import java.io.Writer ; 42 import java.util.*; 43 44 50 public class CompactXmlSerializer extends XmlSerializer { 51 52 protected CompactXmlSerializer(Writer writer, HtmlCleaner htmlCleaner) { 53 super(writer, htmlCleaner); 54 } 55 56 private void serialize(List nodes, TagNode tagNode) throws IOException { 57 if ( nodes != null && !nodes.isEmpty() ) { 58 ListIterator childrenIt = nodes.listIterator(); 59 while ( childrenIt.hasNext() ) { 60 Object 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 content = contentToken.getContent().toString().trim(); 67 if ( !dontEscape(tagNode) ) { 68 content = escapeXml(content).toString(); 69 } else { 70 content = content.replaceAll("]]>", "]]&"); 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 content = ((CommentToken) item).getContent().toString().trim(); 82 writer.write(content); 83 } else { 84 ((BaseToken)item).serialize(this); 85 } 86 } 87 } 88 } 89 } 90 91 96 private boolean isEmptyContentToken(Object 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 { 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 |