KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute;
34 import nu.xom.Document;
35 import nu.xom.Element;
36 import nu.xom.Serializer;
37
38 /**
39  *
40  * <p>
41  * Demonstrates building a structured XML document,
42  * from flat, tabular data. A different version of this
43  * example was originally developed for Chapter 4 of
44  * <cite><a target="_top"
45  * HREF="http://www.cafeconleche.org/books/xmljava/">Processing
46  * XML with Java</a></cite>.
47  * </p>
48  *
49  * @author Elliotte Rusty Harold
50  * @version 1.0
51  *
52  */

53 public class AttributesXMLBudget {
54
55   public static void convert(List JavaDoc data, OutputStream JavaDoc out)
56    throws IOException JavaDoc {
57       
58     Element budget = new Element("Budget");
59     Document doc = new Document(budget);
60           
61     Iterator JavaDoc records = data.iterator();
62     while (records.hasNext()) {
63       Element lineItem = new Element("LineItem");
64       Map JavaDoc record = (Map JavaDoc) records.next();
65
66       // write the attributes
67
setYear(lineItem, "AgencyCode", record);
68       setYear(lineItem, "AgencyName", record);
69       setYear(lineItem, "BureauCode", record);
70       setYear(lineItem, "BureauName", record);
71       setYear(lineItem, "AccountCode", record);
72       setYear(lineItem, "AccountName", record);
73       setYear(lineItem, "TreasuryAgencyCode", record);
74       setYear(lineItem, "SubfunctionCode", record);
75       setYear(lineItem, "SubfunctionTitle", record);
76       setYear(lineItem, "BEACategory", record);
77       setYear(lineItem, "BudgetIndicator", record);
78       setAmount(lineItem, "1976", record);
79       Element amount = new Element("Amount");
80       amount.addAttribute(new Attribute("year", "TransitionalQuarter"));
81       amount.appendChild((String JavaDoc) record.get("TransitionalQuarter"));
82       for (int year=1977; year <= 2006; year++) {
83         setAmount(lineItem, String.valueOf(year), record);
84       }
85     }
86
87     Serializer serializer = new Serializer(out, "UTF-8");
88     serializer.write(doc);
89     serializer.flush();
90         
91   }
92
93   // Just a couple of private methods to factor out repeated code
94
private static void setYear(Element element, String JavaDoc name,
95    Map JavaDoc record) {
96     element.addAttribute(new Attribute(name, (String JavaDoc) record.get(name)));
97   }
98
99   private static void setAmount(Element element, String JavaDoc year,
100    Map JavaDoc record) {
101     Element amount = new Element("Amount");
102     amount.addAttribute(new Attribute("year", String.valueOf(year)));
103     amount.appendChild((String JavaDoc) record.get("Y" + year));
104     element.appendChild(amount);
105   }
106
107   public static void main(String JavaDoc[] args) {
108   
109     try {
110         
111       if (args.length < 1) {
112         System.out.println(
113          "Usage: nu.xom.samples.AttributesXMLBudget infile outfile"
114         );
115         return;
116       }
117       
118       InputStream JavaDoc in = new FileInputStream JavaDoc(args[0]);
119       OutputStream JavaDoc out;
120       if (args.length < 2) {
121         out = System.out;
122       }
123       else {
124         out = new FileOutputStream JavaDoc(args[1]);
125       }
126
127       List JavaDoc results = BudgetData.parse(in);
128       convert(results, out);
129     }
130     catch (IOException JavaDoc ex) {
131       System.err.println(ex);
132     }
133   
134   }
135
136 }
137
Popular Tags