1
/* Copyright 2002-2004 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.BufferedWriter;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.OutputStreamWriter;
29
import java.io.Writer;
31
import nu.xom.Attribute;
32
import nu.xom.Builder;
33
import nu.xom.Element;
34
import nu.xom.NodeFactory;
36
import nu.xom.ParsingException;
40
* Demonstrates extracting the content of particular named elements
41
* from one XML document, and storing them into new files.
42
* The names of these files are based on an attribute of the
46
* @author Elliotte Rusty Harold
50
public class StreamingExampleExtractor extends NodeFactory {
52
private int chapter = 0;
53
private boolean inExample = false;
54
private Nodes empty = new Nodes();
57
// We don't need the comments.
58
public Nodes makeComment(String data) {
63
// We need text nodes only inside examples
64
public Nodes makeText(String data) {
65
if (inExample) return super.makeText(data);
70
public Element makeRootElement(String name, String namespace) {
71
if ("example".equals(name)) {
74
if ("chapter".equals(name)) {
77
return super.startMakingElement(name, namespace);
81
public Element startMakingElement(String name, String namespace) {
82
if ("example".equals(name)) {
85
if ("chapter".equals(name)) {
88
if (inExample) return super.startMakingElement(name, namespace);
93
public Nodes finishMakingElement(Element element) {
94
if (element.getQualifiedName().equals("example")) {
96
extractExample(element, chapter);
98
catch (IOException ex) {
100
"Problem writing " + element.getAttributeValue("id")
101
+ " in chapter " + chapter
106
return new Nodes(element);
110
public Nodes makeAttribute(String name, String URI,
111
String value, Attribute.Type type) {
112
if (inExample && name.equals("id")) {
113
return super.makeAttribute(name, URI, value, type);
119
public Nodes makeDocType(String rootElementName,
120
String publicID, String systemID) {
125
public Nodes makeProcessingInstruction(
126
String target, String data) {
131
private static void extractExample(Element example, int chapter)
134
String fileName = example.getAttribute("id").getValue();
135
System.out.println(fileName);
136
Element programlisting
137
= example.getFirstChildElement("programlisting");
139
// A few examples use screens instead of programlistings
140
if (programlisting == null) {
141
programlisting = example.getFirstChildElement("screen");
143
// If it's still null, skip it
144
if (programlisting == null) return;
145
String code = programlisting.getValue();
147
// write code into a file
148
File dir = new File("examples2/" + chapter);
150
File file = new File(dir, fileName);
151
System.out.println(file);
152
FileOutputStream fout = new FileOutputStream(file);
154
Writer out = new OutputStreamWriter(fout, "UTF-8");
155
// Buffering almost always helps performance a lot
156
out = new BufferedWriter(out);
158
// Be sure to flush and close your streams
168
public static void main(String[] args) {
170
if (args.length <= 0) {
172
"Usage: java nu.xom.samples.StreamingExampleExtractor URL");
175
String url = args[0];
178
Builder builder = new Builder(new StreamingExampleExtractor());
180
builder.build(args[0]);
182
catch (ParsingException ex) {
183
System.out.println(ex);
185
catch (IOException ex) {
187
"Due to an IOException, the parser could not read " + url
189
System.out.println(ex);
b'\\ No newline at end of file'