~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/BudgetData.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
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;
 
31
import java.util.Map;
 
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 parse(InputStream src) throws IOException {
 
52
      
 
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();  
 
57
    String 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[] 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 splitLine(String record) {
 
87
     
 
88
    record = record.trim();
 
89
    
 
90
    int index = 0;
 
91
    Map result = new HashMap();
 
92
    for (int i = 0; i < keys.length; i++) {
 
93
      //find the next comma    
 
94
      StringBuffer sb = new StringBuffer();
 
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 s = sb.toString().trim();
 
107
      result.put(keys[i], s);
 
108
      index++;
 
109
    }  
 
110
        
 
111
    return result;   
 
112
        
 
113
  } 
 
114
 
 
115
}
 
 
b'\\ No newline at end of file'