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

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/HierarchicalXMLBudget.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.io.FileInputStream;
 
25
import java.io.FileOutputStream;
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
import java.io.OutputStream;
 
29
import java.util.Iterator;
 
30
import java.util.List;
 
31
import java.util.Map;
 
32
 
 
33
import nu.xom.Document;
 
34
import nu.xom.Serializer;
 
35
 
 
36
 
 
37
/**
 
38
 * 
 
39
 * <p>
 
40
 * Demonstrates building a structured XML document,
 
41
 * from flat, tabular data. A different version of this 
 
42
 * example was originally developed for Chapter 4 of 
 
43
 * <cite><a target="_top"
 
44
 * href="http://www.cafeconleche.org/books/xmljava/">Processing 
 
45
 * XML with Java</a></cite>.
 
46
 * </p>
 
47
 * 
 
48
 * @author Elliotte Rusty Harold
 
49
 * @version 1.0
 
50
 *
 
51
 */
 
52
public class HierarchicalXMLBudget {
 
53
 
 
54
  public static void convert(List budgetData, String year, 
 
55
   OutputStream out) throws IOException { 
 
56
     
 
57
    Budget budget = new Budget(year);
 
58
    Iterator records = budgetData.iterator();
 
59
    while (records.hasNext()) {
 
60
      Map lineItem = (Map) records.next();
 
61
      budget.add(lineItem);
 
62
    }
 
63
 
 
64
    Document doc = new Document(budget.getXML());
 
65
    Serializer sout = new Serializer(out, "UTF-8");
 
66
    sout.write(doc); 
 
67
    sout.flush();
 
68
        
 
69
  }
 
70
 
 
71
  public static void main(String[] args) {
 
72
  
 
73
    try {
 
74
        
 
75
      if (args.length < 2) {
 
76
        System.out.println(
 
77
         "Usage: nu.xom.samples.HierarchicalXMLBudget year infile outfile");
 
78
        return;
 
79
      }
 
80
      
 
81
      // simple error checking on the year value
 
82
      try {
 
83
        if (!args[0].equals("TransitionalQuarter")) {
 
84
          Integer.parseInt(args[0]);
 
85
        }
 
86
      }
 
87
      catch (NumberFormatException ex) {
 
88
        System.out.println(
 
89
         "Usage: HierarchicalXMLBudget year infile outfile");
 
90
        return;        
 
91
      }
 
92
      
 
93
      InputStream in = new FileInputStream(args[1]); 
 
94
      OutputStream out; 
 
95
      if (args.length < 3) {
 
96
        out = System.out;
 
97
      }
 
98
      else {
 
99
        out = new FileOutputStream(args[2]); 
 
100
      }
 
101
 
 
102
      List results = BudgetData.parse(in);
 
103
      convert(results, args[0], out);
 
104
    }
 
105
    catch (IOException e) {
 
106
      System.err.println(e);       
 
107
    }
 
108
  
 
109
  }
 
110
 
 
111
}
 
 
b'\\ No newline at end of file'