~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/Agency.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
25
import java.util.HashMap;
 
26
import java.util.Iterator;
 
27
import java.util.List;
 
28
import java.util.Map;
 
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 code;
 
51
  private String name;
 
52
  private String treasuryCode;
 
53
  private String year;
 
54
  
 
55
  private List   bureaus = new ArrayList();
 
56
  
 
57
  private static Map instances = new HashMap();
 
58
 
 
59
  // A private constructor so instantiators 
 
60
  // have to use the factory method
 
61
  private Agency(String name, String code, String treasuryCode, 
 
62
     String 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 name, String code, 
 
72
     String treasuryCode, String year) {
 
73
        
 
74
    // Agencies can be uniquely identified by code alone
 
75
    String 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 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
}
 
 
b'\\ No newline at end of file'