1 /* Copyright 2002, 2003 Elliotte Rusty Harold 2 3 This library is free software; you can redistribute it and/or modify 4 it under the terms of version 2.1 of the GNU Lesser General Public 5 License as published by the Free Software Foundation. 6 7 This library is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU Lesser General Public License for more details. 11 12 You should have received a copy of the GNU Lesser General Public 13 License along with this library; if not, write to the 14 Free Software Foundation, Inc., 59 Temple Place, Suite 330, 15 Boston, MA 02111-1307 USA 16 17 You can contact Elliotte Rusty Harold by sending e-mail to 18 elharo@ibiblio.org. Please include the word "XOM" in the 19 subject line. The XOM home page is located at http://www.xom.nu/ 20 */ 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 /** 38 * <p> 39 * Demonstrates filtered streaming via a <code>NodeFactory</code> 40 * subclass. 41 * </p> 42 * 43 * @author Elliotte Rusty Harold 44 * @version 1.0 45 * 46 */ 47 public class StreamingTextExtractor extends NodeFactory { 48 49 private Writer out; 50 private Nodes empty = new Nodes(); 51 StreamingTextExtractor(Writer out)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 StreamingTextExtractor()59 public StreamingTextExtractor() { 60 this(new OutputStreamWriter(System.out)); 61 } 62 makeComment(String data)63 public Nodes makeComment(String data) { 64 return empty; 65 } 66 makeText(String data)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 makeRootElement(String name, String namespace)77 public Element makeRootElement(String name, String namespace) { 78 Element result = new Element(name, namespace); 79 return result; 80 } 81 startMakingElement(String name, String namespace)82 public Element startMakingElement(String name, String namespace) { 83 return null; 84 } 85 makeAttribute(String name, String namespace, String value, Attribute.Type type)86 public Nodes makeAttribute(String name, String namespace, 87 String value, Attribute.Type type) { 88 return empty; 89 } 90 makeDocType(String rootElementName, String publicID, String systemID)91 public Nodes makeDocType(String rootElementName, 92 String publicID, String systemID) { 93 return empty; 94 } 95 makeProcessingInstruction( String target, String data)96 public Nodes makeProcessingInstruction( 97 String target, String data) { 98 return empty; 99 } 100 finishMakingDocument(Document doc)101 public void finishMakingDocument(Document doc) { 102 try { 103 out.flush(); 104 } 105 catch (IOException ex) { 106 System.err.println(ex); 107 } 108 } 109 main(String[] args)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