001: /* Copyright 2002, 2003 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.samples;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import nu.xom.Element;
031:
032: /**
033: *
034: * <p>
035: * Demonstrates building a structured XML document,
036: * from flat, tabular data. A different version of this
037: * example was originally developed for Chapter 4 of
038: * <cite><a
039: * href="http://www.cafeconleche.org/books/xmljava/">Processing
040: * XML with Java</a></cite>.
041: * </p>
042: *
043: * @author Elliotte Rusty Harold
044: * @version 1.0
045: *
046: */
047: public class Bureau {
048:
049: // Agency code plus bureau code uniquely identify a bureau
050: // Bureau code alone is definitely not sufficient
051: private String code;
052: private String name;
053: private String year;
054: private String agencyCode;
055:
056: private List accounts = new ArrayList();
057:
058: private static Map instances = new HashMap();
059:
060: // Use a private constructor so instantiators
061: // have to use the factory method
062: private Bureau(String name, String code, String agencyCode,
063: String year) {
064:
065: this .name = name;
066: this .code = code;
067: this .agencyCode = agencyCode;
068: this .year = year;
069:
070: }
071:
072: public static Bureau getInstance(String name, String code,
073: String agencyCode, String year) {
074:
075: String key = agencyCode + " " + code + " " + year;
076: Bureau bureau = (Bureau) instances.get(key);
077: if (bureau == null) {
078: bureau = new Bureau(name, code, agencyCode, year);
079: instances.put(key, bureau);
080: }
081:
082: return bureau;
083:
084: }
085:
086: public void add(Account account) {
087: if (!accounts.contains(account))
088: accounts.add(account);
089: }
090:
091: public Element getXML() {
092:
093: Element bureau = new Element("Bureau");
094: Element name = new Element("Name");
095: Element code = new Element("Code");
096:
097: name.appendChild(this .name);
098: code.appendChild(this .code);
099: bureau.appendChild(name);
100: bureau.appendChild(code);
101:
102: Iterator 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: }
|