1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.BufferedReader;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.InputStreamReader;
28
import java.util.ArrayList;
29
import java.util.HashMap;
30
import java.util.List;
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
41
* href="http://www.cafeconleche.org/books/xmljava/">Processing
42
* XML with Java</a></cite>.
45
* @author Elliotte Rusty Harold
49
public class BudgetData {
51
public static List parse(InputStream src) throws IOException {
53
// The document as published by the OMB is encoded in Latin-1
54
InputStreamReader isr = new InputStreamReader(src, "8859_1");
55
BufferedReader in = new BufferedReader(isr);
56
List records = new ArrayList();
58
while ((lineItem = in.readLine()) != null) {
59
records.add(splitLine(lineItem));
65
// the field names in order
66
final static String[] keys = {
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"
86
private static Map splitLine(String record) {
88
record = record.trim();
91
Map result = new HashMap();
92
for (int i = 0; i < keys.length; i++) {
94
StringBuffer sb = new StringBuffer();
96
boolean inString = false;
98
c = record.charAt(index);
99
if (!inString && c == '"') inString = true;
100
else if (inString && c == '"') inString = false;
101
else if (!inString && c == ',') break;
104
if (index == record.length()) break;
106
String s = sb.toString().trim();
107
result.put(keys[i], s);
b'\\ No newline at end of file'