1 21 22 package nu.xom.samples; 23 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import nu.xom.Attribute; 34 import nu.xom.Document; 35 import nu.xom.Element; 36 import nu.xom.Serializer; 37 38 53 public class AttributesXMLBudget { 54 55 public static void convert(List data, OutputStream out) 56 throws IOException { 57 58 Element budget = new Element("Budget"); 59 Document doc = new Document(budget); 60 61 Iterator records = data.iterator(); 62 while (records.hasNext()) { 63 Element lineItem = new Element("LineItem"); 64 Map record = (Map ) records.next(); 65 66 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 ) 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 private static void setYear(Element element, String name, 95 Map record) { 96 element.addAttribute(new Attribute(name, (String ) record.get(name))); 97 } 98 99 private static void setAmount(Element element, String year, 100 Map record) { 101 Element amount = new Element("Amount"); 102 amount.addAttribute(new Attribute("year", String.valueOf(year))); 103 amount.appendChild((String ) record.get("Y" + year)); 104 element.appendChild(amount); 105 } 106 107 public static void main(String [] 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 in = new FileInputStream (args[0]); 119 OutputStream out; 120 if (args.length < 2) { 121 out = System.out; 122 } 123 else { 124 out = new FileOutputStream (args[1]); 125 } 126 127 List results = BudgetData.parse(in); 128 convert(results, out); 129 } 130 catch (IOException ex) { 131 System.err.println(ex); 132 } 133 134 } 135 136 } 137 | Popular Tags |