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.Builder; 29 import nu.xom.Comment; 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 import nu.xom.Text; 37 38 49 public class TextSerializer extends Serializer { 50 51 60 public TextSerializer(OutputStream out) { 61 super(out); 62 } 63 64 79 public TextSerializer(OutputStream out, String encoding) 80 throws UnsupportedEncodingException { 81 super(out, encoding); 82 } 83 84 protected void writeStartTag(Element element) {} 85 protected void writeEmptyElementTag(Element element) {} 86 protected void writeEndTag(Element element) {} 87 protected void writeXMLDeclaration() {} 88 protected void write(Comment comment) {} 89 protected void write(ProcessingInstruction instruction) {} 90 protected void write(DocType doctype) {} 91 92 protected void writeText(Text text) throws IOException { 96 writeRaw(text.getValue()); 97 } 98 99 public static void main(String [] args) { 100 101 if (args.length <= 0) { 102 System.out.println( 103 "Usage: java nu.xom.samples.TextSerializer URL"); 104 return; 105 } 106 107 try { 108 Builder parser = new Builder(); 109 Document doc = parser.build(args[0]); 110 Serializer serializer = new TextSerializer(System.out); 111 serializer.write(doc); 112 } 113 catch (ParsingException ex) { 114 System.out.println(args[0] + " is not well-formed."); 115 System.out.println(" at line " + ex.getLineNumber() 116 + ", column " + ex.getColumnNumber()); 117 System.out.println(ex.getMessage()); 118 } 119 catch (IOException ex) { 120 System.out.println( 121 "Due to an IOException, the serialization of " 122 + args[0] + " could not be completed." 123 ); 124 } 125 126 } 127 128 } 129 | Popular Tags |