KickJava   Java API By Example, From Geeks To Geeks.

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


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