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

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/Budget.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.util.ArrayList;
 
25
import java.util.Iterator;
 
26
import java.util.List;
 
27
import java.util.Map;
 
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   agencies = new ArrayList();
 
51
  private String year;
 
52
  
 
53
  public Budget(String 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 lineItem) { 
 
63
           
 
64
    String agencyName = (String) lineItem.get("AgencyName");
 
65
    String agencyCode = (String) lineItem.get("AgencyCode");
 
66
    String treasuryAgencyCode 
 
67
     = (String) lineItem.get("TreasuryAgencyCode");
 
68
    Agency agency = Agency.getInstance(agencyName, agencyCode, 
 
69
     treasuryAgencyCode, year);
 
70
    this.add(agency);
 
71
    
 
72
    String bureauName = (String) lineItem.get("BureauName");
 
73
    String bureauCode = (String) 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 accountName = (String) lineItem.get("AccountName");
 
81
    String accountCode = (String) lineItem.get("AccountCode");
 
82
    String category    = (String) 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 subfunctionTitle = (String) lineItem.get("SubfunctionTitle");
 
90
    String subfunctionCode
 
91
     = (String) lineItem.get("SubfunctionCode");
 
92
    String yearKey = year;
 
93
    if (!yearKey.equals("TransitionalQuarter")) {
 
94
      yearKey = "Y" + year;
 
95
    }
 
96
    long amount
 
97
     = 1000L * Long.parseLong((String) 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 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
}
 
 
b'\\ No newline at end of file'