1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 28 import nu.xom.Attribute; 29 import nu.xom.Builder; 30 import nu.xom.Document; 31 import nu.xom.Element; 32 import nu.xom.NodeFactory; 33 import nu.xom.Nodes; 34 import nu.xom.ParsingException; 35 36 37 47 public class StreamingTextExtractor extends NodeFactory { 48 49 private Writer out; 50 private Nodes empty = new Nodes(); 51 52 public StreamingTextExtractor(Writer out) { 53 if (out == null) { 54 throw new NullPointerException ("Writer must be non-null."); 55 } 56 this.out = out; 57 } 58 59 public StreamingTextExtractor() { 60 this(new OutputStreamWriter (System.out)); 61 } 62 63 public Nodes makeComment(String data) { 64 return empty; 65 } 66 67 public Nodes makeText(String data) { 68 try { 69 out.write(data); 70 } 71 catch (IOException ex) { 72 System.err.println(ex); 73 } 74 return empty; 75 } 76 77 public Element makeRootElement(String name, String namespace) { 78 Element result = new Element(name, namespace); 79 return result; 80 } 81 82 public Element startMakingElement(String name, String namespace) { 83 return null; 84 } 85 86 public Nodes makeAttribute(String name, String namespace, 87 String value, Attribute.Type type) { 88 return empty; 89 } 90 91 public Nodes makeDocType(String rootElementName, 92 String publicID, String systemID) { 93 return empty; 94 } 95 96 public Nodes makeProcessingInstruction( 97 String target, String data) { 98 return empty; 99 } 100 101 public void finishMakingDocument(Document doc) { 102 try { 103 out.flush(); 104 } 105 catch (IOException ex) { 106 System.err.println(ex); 107 } 108 } 109 110 public static void main(String [] args) { 111 112 if (args.length <= 0) { 113 System.out.println( 114 "Usage: java nu.xom.samples.StreamingTextExtractor URL" 115 ); 116 return; 117 } 118 119 try { 120 Builder parser = new Builder(new StreamingTextExtractor()); 121 parser.build(args[0]); 122 } 123 catch (ParsingException ex) { 124 System.out.println(args[0] + " is not well-formed."); 125 System.out.println(ex.getMessage()); 126 } 127 catch (IOException ex) { 128 System.out.println( 129 "Due to an IOException, the parser could not read " 130 + args[0] 131 ); 132 } 133 134 } 135 136 } 137 | Popular Tags |