1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.IOException;
25
import java.io.OutputStreamWriter;
26
import java.io.Writer;
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;
34
import nu.xom.ParsingException;
39
* Demonstrates filtered streaming via a <code>NodeFactory</code>
43
* @author Elliotte Rusty Harold
47
public class StreamingTextExtractor extends NodeFactory {
50
private Nodes empty = new Nodes();
52
public StreamingTextExtractor(Writer out) {
54
throw new NullPointerException("Writer must be non-null.");
59
public StreamingTextExtractor() {
60
this(new OutputStreamWriter(System.out));
63
public Nodes makeComment(String data) {
67
public Nodes makeText(String data) {
71
catch (IOException ex) {
72
System.err.println(ex);
77
public Element makeRootElement(String name, String namespace) {
78
Element result = new Element(name, namespace);
82
public Element startMakingElement(String name, String namespace) {
86
public Nodes makeAttribute(String name, String namespace,
87
String value, Attribute.Type type) {
91
public Nodes makeDocType(String rootElementName,
92
String publicID, String systemID) {
96
public Nodes makeProcessingInstruction(
97
String target, String data) {
101
public void finishMakingDocument(Document doc) {
105
catch (IOException ex) {
106
System.err.println(ex);
110
public static void main(String[] args) {
112
if (args.length <= 0) {
114
"Usage: java nu.xom.samples.StreamingTextExtractor URL"
120
Builder parser = new Builder(new StreamingTextExtractor());
121
parser.build(args[0]);
123
catch (ParsingException ex) {
124
System.out.println(args[0] + " is not well-formed.");
125
System.out.println(ex.getMessage());
127
catch (IOException ex) {
129
"Due to an IOException, the parser could not read "