KickJava   Java API By Example, From Geeks To Geeks.

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


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

49 public class BudgetData {
50
51   public static List JavaDoc parse(InputStream JavaDoc src) throws IOException JavaDoc {
52       
53     // The document as published by the OMB is encoded in Latin-1
54
InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(src, "8859_1");
55     BufferedReader JavaDoc in = new BufferedReader JavaDoc(isr);
56     List JavaDoc records = new ArrayList JavaDoc();
57     String JavaDoc lineItem;
58     while ((lineItem = in.readLine()) != null) {
59       records.add(splitLine(lineItem));
60     }
61     return records;
62         
63   }
64
65   // the field names in order
66
final static String JavaDoc[] keys = {
67     "AgencyCode",
68     "AgencyName",
69     "BureauCode",
70     "BureauName",
71     "AccountCode",
72     "AccountName",
73     "TreasuryAgencyCode",
74     "SubfunctionCode",
75     "SubfunctionTitle",
76     "BEACategory",
77     "On-Off-BudgetIndicator",
78     "FY1976", "TransitionQuarter", "FY1977", "FY1978", "FY1979",
79     "FY1980", "FY1981", "FY1982", "FY1983", "FY1984", "FY1985",
80     "FY1986", "FY1987", "FY1988", "FY1989", "FY1990", "FY1991",
81     "FY1992", "FY1993", "FY1994", "FY1995", "FY1996", "FY1997",
82     "FY1998", "FY1999", "FY2000", "FY2001", "FY2002", "FY2003",
83     "FY2004", "FY2005", "FY2006"
84    };
85
86   private static Map JavaDoc splitLine(String JavaDoc record) {
87      
88     record = record.trim();
89     
90     int index = 0;
91     Map JavaDoc result = new HashMap JavaDoc();
92     for (int i = 0; i < keys.length; i++) {
93       //find the next comma
94
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95       char c;
96       boolean inString = false;
97       while (true) {
98         c = record.charAt(index);
99         if (!inString && c == '"') inString = true;
100         else if (inString && c == '"') inString = false;
101         else if (!inString && c == ',') break;
102         else sb.append(c);
103         index++;
104         if (index == record.length()) break;
105       }
106       String JavaDoc s = sb.toString().trim();
107       result.put(keys[i], s);
108       index++;
109     }
110         
111     return result;
112         
113   }
114
115 }
Popular Tags