KickJava   Java API By Example, From Geeks To Geeks.

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


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.Builder;
32 import nu.xom.Document;
33 import nu.xom.Element;
34 import nu.xom.Elements;
35 import nu.xom.ParsingException;
36
37 /**
38  * <p>
39  * Demonstrates extracting the content of particular named elements
40  * from one XML document, and storing them into new files.
41  * The names of these files are based on an attribute of the
42  * original element.
43  * </p>
44  *
45  * @author Elliotte Rusty Harold
46  * @version 1.0
47  *
48  */

49 public class ExampleExtractor {
50
51     private static int chapter = 0;
52   
53     public static void extract(Element element)
54       throws IOException JavaDoc {
55
56         Elements elements = element.getChildElements();
57         for (int i = 0; i < elements.size(); i++) {
58             Element child = elements.get(i);
59             if (child.getQualifiedName().equals("chapter")) {
60                 chapter++;
61             }
62              if (child.getQualifiedName().equals("example")) {
63                 extractExample(child, chapter);
64             }
65             else {
66                 extract(child);
67             }
68         } // end chapters loop
69

70     }
71
72     
73     private static void extractExample(Element example, int chapter)
74       throws IOException JavaDoc {
75
76         String JavaDoc fileName = example.getAttribute("id").getValue();
77         Element programlisting
78           = example.getFirstChildElement("programlisting");
79         
80         // A few examples use screens instead of programlistings
81
if (programlisting == null) {
82             programlisting = example.getFirstChildElement("screen");
83         }
84         // If it's still null, skip it
85
if (programlisting == null) return;
86         String JavaDoc code = programlisting.getValue();
87         
88         // write code into a file
89
File JavaDoc dir = new File JavaDoc("examples2/" + chapter);
90         dir.mkdirs();
91         File JavaDoc file = new File JavaDoc(dir, fileName);
92         System.out.println(file);
93         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(file);
94         Writer JavaDoc out = new OutputStreamWriter JavaDoc(fout, "UTF-8");
95         try {
96             // Buffering almost always helps performance a lot
97
out = new BufferedWriter JavaDoc(out);
98             out.write(code);
99             // Be sure to flush and close your streams
100
out.flush();
101         }
102         finally {
103             fout.close();
104         }
105     
106     }
107   
108     
109     public static void main(String JavaDoc[] args) {
110
111         if (args.length <= 0) {
112           System.out.println(
113             "Usage: java nu.xom.samples.ExampleExtractor URL");
114           return;
115         }
116         String JavaDoc url = args[0];
117         
118         try {
119             Builder builder = new Builder();
120             // Read the document
121
Document document = builder.build(args[0]);
122          
123              // Extract the examples
124
extract(document.getRootElement());
125     
126         }
127         catch (ParsingException ex) {
128             System.out.println(ex);
129         }
130         catch (IOException JavaDoc ex) {
131             System.out.println(
132               "Due to an IOException, the parser could not read " + url
133             );
134             System.out.println(ex);
135         }
136         catch (OutOfMemoryError JavaDoc ex) {
137             System.out.println(ex);
138             ex.printStackTrace();
139         }
140      
141     } // end main
142

143     
144 }
145   
146
Popular Tags