1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.Iterator;
27
import java.util.List;
30
import nu.xom.Element;
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>.
44
* @author Elliotte Rusty Harold
52
private String treasuryCode;
55
private List bureaus = new ArrayList();
57
private static Map instances = new HashMap();
59
// A private constructor so instantiators
60
// have to use the factory method
61
private Agency(String name, String code, String treasuryCode,
66
this.treasuryCode = treasuryCode;
71
public static Agency getInstance(String name, String code,
72
String treasuryCode, String year) {
74
// Agencies can be uniquely identified by code alone
75
String key = code+" "+year;
76
Agency agency = (Agency) instances.get(key);
78
agency = new Agency(name, code, treasuryCode, year);
79
instances.put(key, agency);
86
public void add(Bureau b) {
87
if (!bureaus.contains(b)) {
92
public Element getXML() {
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);
105
Iterator iterator = bureaus.iterator();
106
while (iterator.hasNext()) {
107
Bureau bureau = (Bureau) iterator.next();
108
agency.appendChild(bureau.getXML());
b'\\ No newline at end of file'