KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import nu.xom.Document;
34 import nu.xom.Serializer;
35
36
37 /**
38  *
39  * <p>
40  * Demonstrates building a structured XML document,
41  * from flat, tabular data. A different version of this
42  * example was originally developed for Chapter 4 of
43  * <cite><a target="_top"
44  * HREF="http://www.cafeconleche.org/books/xmljava/">Processing
45  * XML with Java</a></cite>.
46  * </p>
47  *
48  * @author Elliotte Rusty Harold
49  * @version 1.0
50  *
51  */

52 public class HierarchicalXMLBudget {
53
54   public static void convert(List JavaDoc budgetData, String JavaDoc year,
55    OutputStream JavaDoc out) throws IOException JavaDoc {
56      
57     Budget budget = new Budget(year);
58     Iterator JavaDoc records = budgetData.iterator();
59     while (records.hasNext()) {
60       Map JavaDoc lineItem = (Map JavaDoc) records.next();
61       budget.add(lineItem);
62     }
63
64     Document doc = new Document(budget.getXML());
65     Serializer sout = new Serializer(out, "UTF-8");
66     sout.write(doc);
67     sout.flush();
68         
69   }
70
71   public static void main(String JavaDoc[] args) {
72   
73     try {
74         
75       if (args.length < 2) {
76         System.out.println(
77          "Usage: nu.xom.samples.HierarchicalXMLBudget year infile outfile");
78         return;
79       }
80       
81       // simple error checking on the year value
82
try {
83         if (!args[0].equals("TransitionalQuarter")) {
84           Integer.parseInt(args[0]);
85         }
86       }
87       catch (NumberFormatException JavaDoc ex) {
88         System.out.println(
89          "Usage: HierarchicalXMLBudget year infile outfile");
90         return;
91       }
92       
93       InputStream JavaDoc in = new FileInputStream JavaDoc(args[1]);
94       OutputStream JavaDoc out;
95       if (args.length < 3) {
96         out = System.out;
97       }
98       else {
99         out = new FileOutputStream JavaDoc(args[2]);
100       }
101
102       List JavaDoc results = BudgetData.parse(in);
103       convert(results, args[0], out);
104     }
105     catch (IOException JavaDoc e) {
106       System.err.println(e);
107     }
108   
109   }
110
111 }
Popular Tags