KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import nu.xom.Element;
31
32
33 /**
34  *
35  * <p>
36  * Demonstrates building 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 Agency {
49  
50   private String JavaDoc code;
51   private String JavaDoc name;
52   private String JavaDoc treasuryCode;
53   private String JavaDoc year;
54   
55   private List JavaDoc bureaus = new ArrayList JavaDoc();
56   
57   private static Map JavaDoc instances = new HashMap JavaDoc();
58
59   // A private constructor so instantiators
60
// have to use the factory method
61
private Agency(String JavaDoc name, String JavaDoc code, String JavaDoc treasuryCode,
62      String JavaDoc year) {
63         
64     this.name = name;
65     this.code = code;
66     this.treasuryCode = treasuryCode;
67     this.year = year;
68     
69   }
70   
71   public static Agency getInstance(String JavaDoc name, String JavaDoc code,
72      String JavaDoc treasuryCode, String JavaDoc year) {
73         
74     // Agencies can be uniquely identified by code alone
75
String JavaDoc key = code+" "+year;
76     Agency agency = (Agency) instances.get(key);
77     if (agency == null) {
78       agency = new Agency(name, code, treasuryCode, year);
79       instances.put(key, agency);
80     }
81     
82     return agency;
83         
84   }
85   
86   public void add(Bureau b) {
87     if (!bureaus.contains(b)) {
88         bureaus.add(b);
89     }
90   }
91   
92   public Element getXML() {
93         
94     Element agency = new Element("Agency");
95     Element name = new Element("Name");
96     Element code = new Element("Code");
97     Element treasuryAgencyCode = new Element("TreasuryAgencyCode");
98     name.appendChild(this.name);
99     code.appendChild(this.code);
100     treasuryAgencyCode.appendChild(treasuryCode);
101     agency.appendChild(name);
102     agency.appendChild(code);
103     agency.appendChild(treasuryAgencyCode);
104     
105     Iterator JavaDoc iterator = bureaus.iterator();
106     while (iterator.hasNext()) {
107       Bureau bureau = (Bureau) iterator.next();
108       agency.appendChild(bureau.getXML());
109     }
110     return agency;
111     
112   }
113            
114 }
Popular Tags