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

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/FlatXMLBudget.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
import java.util.Set;
 
33
 
 
34
import nu.xom.Document;
 
35
import nu.xom.Element;
 
36
import nu.xom.Serializer;
 
37
 
 
38
 
 
39
/**
 
40
 * 
 
41
 * <p>
 
42
 * Demonstrates building a structured XML document,
 
43
 * from flat, tabular data. A different version of this 
 
44
 * example was originally developed for Chapter 4 of 
 
45
 * <cite><a  target="_top" 
 
46
 * href="http://www.cafeconleche.org/books/xmljava/">Processing 
 
47
 * XML with Java</a></cite>.
 
48
 * </p>
 
49
 * 
 
50
 * @author Elliotte Rusty Harold
 
51
 * @version 1.0
 
52
 *
 
53
 */
 
54
public class FlatXMLBudget {
 
55
 
 
56
  public static void convert(List data, OutputStream out) 
 
57
   throws IOException {
 
58
      
 
59
    Element budget = new Element("Budget");
 
60
    Document doc = new Document(budget);
 
61
    
 
62
    Iterator records = data.iterator();
 
63
    while (records.hasNext()) {
 
64
      Element lineItem = new Element("LineItem");
 
65
      Map record = (Map) records.next();
 
66
      Set fields = record.entrySet();
 
67
      Iterator entries = fields.iterator();
 
68
      while (entries.hasNext()) {
 
69
        Map.Entry entry = (Map.Entry) entries.next();
 
70
        String name = (String) entry.getKey();
 
71
        String value = (String) entry.getValue();
 
72
        // some of the values contain ampersands and less than
 
73
        // signs that must be escaped
 
74
        Element field = new Element(name);
 
75
        field.appendChild(value); 
 
76
         lineItem.appendChild(field);
 
77
       }
 
78
       budget.appendChild(lineItem);
 
79
    }
 
80
 
 
81
    Serializer serializer = new Serializer(out, "UTF-8");
 
82
    serializer.write(doc);
 
83
    serializer.flush();
 
84
        
 
85
  } 
 
86
 
 
87
  public static void main(String[] args) {
 
88
  
 
89
    try {
 
90
        
 
91
      if (args.length < 1) {
 
92
       System.out.println("Usage: nu.xom.samples.FlatXMLBudget infile outfile");
 
93
       return;
 
94
      }
 
95
      
 
96
      InputStream in = new FileInputStream(args[0]); 
 
97
      OutputStream out; 
 
98
      if (args.length < 2) {
 
99
        out = System.out;
 
100
      }
 
101
      else {
 
102
        out = new FileOutputStream(args[1]); 
 
103
      }
 
104
 
 
105
      List results = BudgetData.parse(in);
 
106
      convert(results, out);
 
107
    }
 
108
    catch (IOException ex) {
 
109
      System.err.println(ex);       
 
110
    }
 
111
  
 
112
  }
 
113
 
 
114
}
 
 
b'\\ No newline at end of file'