KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright 2002-2004 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.BufferedWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 import nu.xom.Attribute;
32 import nu.xom.Builder;
33 import nu.xom.Element;
34 import nu.xom.NodeFactory;
35 import nu.xom.Nodes;
36 import nu.xom.ParsingException;
37
38 /**
39  * <p>
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
43  * original element.
44  * </p>
45  *
46  * @author Elliotte Rusty Harold
47  * @version 1.0
48  *
49  */

50 public class StreamingExampleExtractor extends NodeFactory {
51
52     private int chapter = 0;
53     private boolean inExample = false;
54     private Nodes empty = new Nodes();
55
56     
57     // We don't need the comments.
58
public Nodes makeComment(String JavaDoc data) {
59         return empty;
60     }
61
62     
63     // We need text nodes only inside examples
64
public Nodes makeText(String JavaDoc data) {
65         if (inExample) return super.makeText(data);
66         return empty;
67     }
68
69     
70     public Element makeRootElement(String JavaDoc name, String JavaDoc namespace) {
71         if ("example".equals(name)) {
72             inExample = true;
73         }
74         if ("chapter".equals(name)) {
75             chapter++;
76         }
77         return super.startMakingElement(name, namespace);
78     }
79
80     
81     public Element startMakingElement(String JavaDoc name, String JavaDoc namespace) {
82         if ("example".equals(name)) {
83             inExample = true;
84         }
85         if ("chapter".equals(name)) {
86             chapter++;
87         }
88         if (inExample) return super.startMakingElement(name, namespace);
89         else return null;
90     }
91     
92     
93     public Nodes finishMakingElement(Element element) {
94         if (element.getQualifiedName().equals("example")) {
95             try {
96                 extractExample(element, chapter);
97             }
98             catch (IOException JavaDoc ex) {
99                 System.err.println(
100                   "Problem writing " + element.getAttributeValue("id")
101                   + " in chapter " + chapter
102                 );
103             }
104             inExample = false;
105         }
106         return new Nodes(element);
107     }
108
109     
110     public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
111       String JavaDoc value, Attribute.Type type) {
112         if (inExample && name.equals("id")) {
113             return super.makeAttribute(name, URI, value, type);
114         }
115         return empty;
116     }
117
118     
119     public Nodes makeDocType(String JavaDoc rootElementName,
120       String JavaDoc publicID, String JavaDoc systemID) {
121         return empty;
122     }
123
124     
125     public Nodes makeProcessingInstruction(
126       String JavaDoc target, String JavaDoc data) {
127         return empty;
128     }
129
130     
131     private static void extractExample(Element example, int chapter)
132       throws IOException JavaDoc {
133
134         String JavaDoc fileName = example.getAttribute("id").getValue();
135         System.out.println(fileName);
136         Element programlisting
137           = example.getFirstChildElement("programlisting");
138         
139         // A few examples use screens instead of programlistings
140
if (programlisting == null) {
141             programlisting = example.getFirstChildElement("screen");
142         }
143         // If it's still null, skip it
144
if (programlisting == null) return;
145         String JavaDoc code = programlisting.getValue();
146         
147         // write code into a file
148
File JavaDoc dir = new File JavaDoc("examples2/" + chapter);
149         dir.mkdirs();
150         File JavaDoc file = new File JavaDoc(dir, fileName);
151         System.out.println(file);
152         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(file);
153         try {
154             Writer JavaDoc out = new OutputStreamWriter JavaDoc(fout, "UTF-8");
155             // Buffering almost always helps performance a lot
156
out = new BufferedWriter JavaDoc(out);
157             out.write(code);
158             // Be sure to flush and close your streams
159
out.flush();
160         }
161         finally {
162             fout.close();
163         }
164     
165     }
166   
167     
168     public static void main(String JavaDoc[] args) {
169
170         if (args.length <= 0) {
171           System.out.println(
172             "Usage: java nu.xom.samples.StreamingExampleExtractor URL");
173           return;
174         }
175         String JavaDoc url = args[0];
176         
177         try {
178             Builder builder = new Builder(new StreamingExampleExtractor());
179             // Read the document
180
builder.build(args[0]);
181         }
182         catch (ParsingException ex) {
183             System.out.println(ex);
184         }
185         catch (IOException JavaDoc ex) {
186             System.out.println(
187               "Due to an IOException, the parser could not read " + url
188             );
189             System.out.println(ex);
190         }
191      
192     } // end main
193

194     
195 }
Popular Tags