1 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 49 public class BudgetData { 50 51 public static List parse(InputStream src) throws IOException { 52 53 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 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 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 } | Popular Tags |