KickJava   Java API By Example, From Geeks To Geeks.

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


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

48 public class Budget {
49
50   private List JavaDoc agencies = new ArrayList JavaDoc();
51   private String JavaDoc year;
52   
53   public Budget(String JavaDoc year) {
54     this.year = year;
55   }
56   
57   // not thread safe
58
public void add(Agency agency) {
59     if (!agencies.contains(agency)) agencies.add(agency);
60   }
61
62   public void add(Map JavaDoc lineItem) {
63            
64     String JavaDoc agencyName = (String JavaDoc) lineItem.get("AgencyName");
65     String JavaDoc agencyCode = (String JavaDoc) lineItem.get("AgencyCode");
66     String JavaDoc treasuryAgencyCode
67      = (String JavaDoc) lineItem.get("TreasuryAgencyCode");
68     Agency agency = Agency.getInstance(agencyName, agencyCode,
69      treasuryAgencyCode, year);
70     this.add(agency);
71     
72     String JavaDoc bureauName = (String JavaDoc) lineItem.get("BureauName");
73     String JavaDoc bureauCode = (String JavaDoc) lineItem.get("BureauCode");
74     Bureau bureau = Bureau.getInstance(bureauName, bureauCode,
75      agencyCode, year);
76     agency.add(bureau);
77     
78     // Names and codes of two accounts in different bureaus
79
// can be the same
80
String JavaDoc accountName = (String JavaDoc) lineItem.get("AccountName");
81     String JavaDoc accountCode = (String JavaDoc) lineItem.get("AccountCode");
82     String JavaDoc category = (String JavaDoc) lineItem.get("BEACategory");
83     Account account = Account.getInstance(accountName,
84      accountCode, category, bureauCode, agencyCode, year);
85     bureau.add(account);
86     
87     // Names and codes of two subfunctions in different accounts
88
// can be the same
89
String JavaDoc subfunctionTitle = (String JavaDoc) lineItem.get("SubfunctionTitle");
90     String JavaDoc subfunctionCode
91      = (String JavaDoc) lineItem.get("SubfunctionCode");
92     String JavaDoc yearKey = year;
93     if (!yearKey.equals("TransitionalQuarter")) {
94       yearKey = "Y" + year;
95     }
96     long amount
97      = 1000L * Long.parseLong((String JavaDoc) lineItem.get(yearKey));
98     Subfunction subfunction = new Subfunction(subfunctionTitle,
99      subfunctionCode, amount);
100     account.add(subfunction);
101         
102   }
103
104   public Element getXML() {
105         
106     Element budget = new Element("Budget");
107     budget.addAttribute(new Attribute("year", String.valueOf(year)));
108     Iterator JavaDoc iterator = agencies.iterator();
109     while (iterator.hasNext()) {
110       Agency agency = (Agency) iterator.next();
111       budget.appendChild(agency.getXML());
112     }
113      return budget;
114     
115   }
116
117 }
Popular Tags