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 PrettyXmlSerializer extends XmlSerializer { 51 52 private static final String INDENTATION_STRING = "\t"; 53 54 protected PrettyXmlSerializer(Writer writer, HtmlCleaner htmlCleaner) { 55 super(writer, htmlCleaner); 56 } 57 58 protected void serialize(TagNode tagNode) throws IOException { 59 serializePrettyXml(tagNode, 0); 60 } 61 62 66 private String indent(int level) { 67 String result = ""; 68 while (level > 0) { 69 result += INDENTATION_STRING; 70 level--; 71 } 72 73 return result; 74 } 75 76 private String getIndentedText(String content, int level) { 77 String indent = indent(level); 78 StringBuffer result = new StringBuffer ( content.length() ); 79 StringTokenizer tokenizer = new StringTokenizer(content, "\n\r"); 80 81 while (tokenizer.hasMoreTokens()) { 82 String 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 getSingleLineOfChildren(List children) { 92 StringBuffer result = new StringBuffer (); 93 Iterator childrenIt = children.iterator(); 94 boolean isFirst = true; 95 96 while (childrenIt.hasNext()) { 97 Object child = childrenIt.next(); 98 99 if ( !(child instanceof ContentToken) ) { 100 return null; 101 } else { 102 ContentToken contentToken = (ContentToken) child; 103 String content = contentToken.getContent().toString(); 104 105 if (isFirst) { 107 content = Utils.ltrim(content); 108 } 109 110 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 { 128 Iterator childrenIt = nodes.iterator(); 129 while (childrenIt.hasNext()) { 130 Object 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 content = contentToken.getContent(); 136 if ( !dontEscape ) { 137 content = escapeXml(content).toString(); 138 } else { 139 content = content.replaceAll("]]>", "]]&"); 140 } 141 writer.write( getIndentedText(content, level + 1) ); 142 } else if (child instanceof CommentToken) { 143 CommentToken commentToken = (CommentToken) child; 144 String 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 { 153 List tagChildren = tagNode.getChildren(); 154 String indent = indent(level); 155 156 writer.write(indent); 157 serializeOpenTag(tagNode); 158 159 if ( !tagChildren.isEmpty() ) { 160 String 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("]]>", "]]&") ); 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 |