KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 Bureau {
49  
50   // Agency code plus bureau code uniquely identify a bureau
51
// Bureau code alone is definitely not sufficient
52
private String JavaDoc code;
53   private String JavaDoc name;
54   private String JavaDoc year;
55   private String JavaDoc agencyCode;
56   
57   private List JavaDoc accounts = new ArrayList JavaDoc();
58   
59   private static Map JavaDoc instances = new HashMap JavaDoc();
60
61   // Use a private constructor so instantiators
62
// have to use the factory method
63
private Bureau(String JavaDoc name, String JavaDoc code, String JavaDoc agencyCode,
64     String JavaDoc year) {
65         
66     this.name = name;
67     this.code = code;
68     this.agencyCode = agencyCode;
69     this.year = year;
70     
71   }
72   
73   public static Bureau getInstance(String JavaDoc name, String JavaDoc code,
74    String JavaDoc agencyCode, String JavaDoc year) {
75         
76     String JavaDoc key = agencyCode+" "+code+" "+year;
77     Bureau bureau = (Bureau) instances.get(key);
78     if (bureau == null) {
79       bureau = new Bureau(name, code, agencyCode, year);
80       instances.put(key, bureau);
81     }
82     
83     return bureau;
84         
85   }
86   
87   public void add(Account account) {
88     if (!accounts.contains(account)) accounts.add(account);
89   }
90   
91   public Element getXML() {
92         
93     Element bureau = new Element("Bureau");
94     Element name = new Element("Name");
95     Element code = new Element("Code");
96
97     name.appendChild(this.name);
98     code.appendChild(this.code);
99     bureau.appendChild(name);
100     bureau.appendChild(code);
101     
102     Iterator JavaDoc iterator = accounts.iterator();
103     while (iterator.hasNext()) {
104       Account account = (Account) iterator.next();
105       bureau.appendChild(account.getXML());
106     }
107     return bureau;
108     
109   }
110           
111 }
Popular Tags