KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > StreamingTextExtractor


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@metalab.unc.edu. 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 JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Writer JavaDoc;
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 JavaDoc out;
50     private Nodes empty = new Nodes();
51     
52     public StreamingTextExtractor(Writer JavaDoc out) {
53       if (out == null) {
54       throw new NullPointerException JavaDoc("Writer must be non-null.");
55       }
56       this.out = out;
57     }
58     
59     public StreamingTextExtractor() {
60       this(new OutputStreamWriter JavaDoc(System.out));
61     }
62     
63     public Nodes makeComment(String JavaDoc data) {
64         return empty;
65     }
66
67     public Nodes makeText(String JavaDoc data) {
68         try {
69             out.write(data);
70         }
71         catch (IOException JavaDoc ex) {
72             System.err.println(ex);
73         }
74         return empty;
75     }
76
77     public Element makeRootElement(String JavaDoc name, String JavaDoc namespace) {
78         Element result = new Element(name, namespace);
79         return result;
80     }
81     
82     public Element startMakingElement(String JavaDoc name, String JavaDoc namespace) {
83         return null;
84     }
85
86     public Nodes makeAttribute(String JavaDoc name, String JavaDoc namespace,
87       String JavaDoc value, Attribute.Type type) {
88         return empty;
89     }
90
91     public Nodes makeDocType(String JavaDoc rootElementName,
92       String JavaDoc publicID, String JavaDoc systemID) {
93         return empty;
94     }
95
96     public Nodes makeProcessingInstruction(
97       String JavaDoc target, String JavaDoc data) {
98         return empty;
99     }
100     
101     public void finishMakingDocument(Document doc) {
102         try {
103             out.flush();
104         }
105         catch (IOException JavaDoc ex) {
106            System.err.println(ex);
107         }
108     }
109     
110     public static void main(String JavaDoc[] 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 JavaDoc 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