1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.UnsupportedEncodingException ; 27 28 import nu.xom.Attribute; 29 import nu.xom.Builder; 30 import nu.xom.DocType; 31 import nu.xom.Document; 32 import nu.xom.Element; 33 import nu.xom.ParsingException; 34 import nu.xom.ProcessingInstruction; 35 import nu.xom.Serializer; 36 37 48 public class WrappingSerializer extends Serializer { 49 50 public WrappingSerializer(OutputStream out) { 51 super(out); 52 } 53 54 public WrappingSerializer(OutputStream out, String encoding) 55 throws UnsupportedEncodingException { 56 super(out, encoding); 57 } 58 59 private int maxLength; 60 61 68 public int getMaxLength() { 69 return this.maxLength; 70 } 71 72 94 public void setMaxLength(int maxLength) { 95 this.maxLength = maxLength; 96 } 97 98 protected void writeStartTag(Element element) 99 throws IOException { 100 writeRaw("<"); 101 writeRaw(element.getQualifiedName()); 102 writeAttributes(element); 103 writeNamespaceDeclarations(element); 104 if (needsBreak()) breakLine(); 105 writeRaw(">"); 106 } 107 108 protected void writeEmptyElementTag(Element element) 109 throws IOException { 110 writeRaw("<"); 111 writeRaw(element.getQualifiedName()); 112 writeAttributes(element); 113 writeNamespaceDeclarations(element); 114 if (needsBreak()) breakLine(); 115 writeRaw("/>"); 116 } 117 118 public void writeEndTag(Element element) throws IOException { 119 writeRaw("<"); 120 writeRaw(element.getQualifiedName()); 121 if (needsBreak()) breakLine(); 122 writeRaw("/>"); 123 } 124 125 137 protected void write(Attribute attribute) throws IOException { 138 139 String name = attribute.getQualifiedName(); 140 if (maxLength <= this.getColumnNumber() + name.length()) { 141 breakLine(); 142 } 143 writeRaw(name); 144 if (this.getColumnNumber() == maxLength) { 145 breakLine(); 146 } 147 writeRaw("="); 148 String value = attribute.getValue(); 149 if (maxLength < value.length() + 2) { 150 breakLine(); 151 } 152 writeRaw("\""); 153 writeAttributeValue(attribute.getValue()); 154 writeRaw("\""); 155 156 } 157 158 172 protected void writeNamespaceDeclaration(String prefix, String uri) 173 throws IOException { 174 175 String name; 176 if ("".equals(prefix)) { 177 name = "xmlns"; 178 } 179 else { 180 name = "xmlns:" + prefix; 181 } 182 if (this.maxLength < this.getColumnNumber() + name.length()) { 183 breakLine(); 184 } 185 writeRaw(name); 186 187 if (this.getColumnNumber() == maxLength) { 188 breakLine(); 189 } 190 writeRaw("="); 191 192 if (this.maxLength < this.getColumnNumber() + uri.length() + 2) { 193 breakLine(); 194 } 195 writeRaw("\""); 196 writeEscaped(uri); 197 writeRaw("\""); 198 } 199 200 private boolean needsBreak() { 201 if (maxLength > 0) { 202 return this.maxLength - this.getColumnNumber() <= 10; 203 } 204 return false; 205 } 206 207 228 protected void write(ProcessingInstruction instruction) 229 throws IOException { 230 writeRaw("<?"); 231 writeRaw(instruction.getTarget()); 232 String value = instruction.getValue(); 233 if (maxLength < getColumnNumber() + value.length() + 3) { 234 breakLine(); 235 } 236 else { 237 writeRaw(" "); 238 } 239 writeRaw(value); 240 writeRaw("?>"); 241 } 242 243 254 protected void write(DocType doctype) throws IOException { 255 writeRaw("<!DOCTYPE"); 256 String rootElementName = doctype.getRootElementName(); 257 if (maxLength < getColumnNumber() + rootElementName.length() + 1) { 258 breakLine(); 259 } 260 else { 261 writeRaw(" "); 262 } 263 writeRaw(rootElementName); 264 265 String publicID = doctype.getPublicID(); 266 String systemID = doctype.getSystemID(); 267 if (publicID != null) { 268 if (maxLength < getColumnNumber() + 6) { 269 breakLine(); 270 } 271 else { 272 writeRaw(" "); 273 } 274 writeRaw("PUBLIC"); 275 276 if (maxLength < getColumnNumber() + publicID.length() + 2) { 277 breakLine(); 278 } 279 else { 280 writeRaw(" "); 281 } 282 writeRaw("\""); 283 writeRaw(publicID); 284 writeRaw("\""); 285 286 if (maxLength < getColumnNumber() + systemID.length() + 2) { 287 breakLine(); 288 } 289 else { 290 writeRaw(" "); 291 } 292 writeRaw("\""); 293 writeRaw(systemID); 294 writeRaw("\""); 295 } 296 else if (systemID != null) { 297 if (maxLength < getColumnNumber() + 6) { 298 breakLine(); 299 } 300 else { 301 writeRaw(" "); 302 } 303 writeRaw("SYSTEM"); 304 305 if (maxLength < getColumnNumber() + systemID.length() + 2) { 306 breakLine(); 307 } 308 else { 309 writeRaw(" "); 310 } 311 writeRaw("\""); 312 writeRaw(systemID); 313 writeRaw("\""); 314 } 315 316 String internalDTDSubset = doctype.getInternalDTDSubset(); 317 if (!internalDTDSubset.equals("")) { 318 if (maxLength < getColumnNumber() + 2) { 319 breakLine(); 320 } 321 else writeRaw(" "); 322 writeRaw("["); 323 breakLine(); 324 writeRaw(internalDTDSubset); 325 breakLine(); 326 writeRaw("]"); 327 } 328 329 if (maxLength < getColumnNumber() + 1) { 330 breakLine(); 331 } 332 writeRaw(">"); 333 334 } 335 336 public static void main(String [] args) { 337 338 if (args.length <= 0) { 339 System.out.println("Usage: java nu.xom.samples.WrappingSerializer URL"); 340 return; 341 } 342 343 try { 344 Builder parser = new Builder(); 345 Document doc = parser.build(args[0]); 346 Serializer serializer = new WrappingSerializer(System.out, "ISO-8859-1"); 347 serializer.setIndent(4); 348 serializer.setMaxLength(24); 349 serializer.setPreserveBaseURI(true); 350 serializer.write(doc); 351 serializer.flush(); 352 } 353 catch (ParsingException ex) { 354 System.out.println(args[0] + " is not well-formed."); 355 System.out.println(ex.getMessage()); 356 } 357 catch (IOException ex) { 358 System.out.println( 359 "Due to an IOException, the parser could not read " 360 + args[0] 361 ); 362 } 363 364 } 365 366 } 367 | Popular Tags |