KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > ParameterReader


1 package jimm.datavision;
2 import jimm.util.I18N;
3 import java.io.*;
4 import javax.xml.parsers.SAXParser JavaDoc;
5 import javax.xml.parsers.SAXParserFactory JavaDoc;
6 import org.xml.sax.*;
7 import org.xml.sax.helpers.DefaultHandler JavaDoc;
8
9 /**
10  * A parameter reader reads an XML file and sets a {@link Report}'s parameter
11  * values. This class is used when the report is being run from the
12  * command line and the user has given us the name of an XML file containing
13  * parameter elements.
14  * <p>
15  * Unlike a {@link ReportReader}, a parameter reader's constructor
16  * takes not only the report but also the input method (file name, stream,
17  * or reader). That way the report object doesn't have to know how to hold
18  * on to those multipule input types.
19  *
20  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
21  */

22 public class ParameterReader extends DefaultHandler JavaDoc {
23
24 /**
25  * If there is no report element dtd-version attribute, this is the
26  * default value to use.
27  */

28 protected static final double DEFAULT_DTD_VERSION = 0.1;
29
30 protected Report report;
31 protected Parameter parameter;
32 protected String JavaDoc textData;
33 protected File inFile;
34 protected InputSource inInputSource;
35
36 /**
37  * Constructor.
38  *
39  * @param report the report whose parameters we are setting
40  * @param f the parameter XML file
41  */

42 public ParameterReader(Report report, File f) {
43     this.report = report;
44     inFile = f;
45 }
46
47 /**
48  * Constructor. To specify a URL, use <code>new
49  * InputSource("http://...")</code>.
50  *
51  * @param report the report whose parameters we are setting
52  * @param in the param XML input source
53  */

54 public ParameterReader(Report report, InputSource in) {
55     this.report = report;
56     inInputSource = in;
57 }
58
59 /**
60  * Returns the file name or, if that is <code>null</code>, the class name of
61  * whatever input source was handed to a constructor.
62  *
63  * @return a file name or class name
64  */

65 public String JavaDoc getInputName() {
66     if (inFile != null)
67     return inFile.getPath();
68     if (inInputSource != null)
69     return "org.xml.sax.InputSource";
70     return "?";
71 }
72
73 /**
74  * Reads parameter values from whichever input method was specified
75  * in the constructor.
76  */

77 public void read() throws Exception JavaDoc {
78     SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
79     if (inFile != null)
80     parser.parse(inFile, this);
81     else if (inInputSource != null)
82     parser.parse(inInputSource, this);
83 }
84
85 public void startElement(final String JavaDoc namespaceURI, final String JavaDoc localName,
86              final String JavaDoc qName, final Attributes attributes)
87     throws SAXException
88 {
89     String JavaDoc tagName = localName;
90     if (tagName == null || tagName.length() == 0)
91         tagName = qName;
92
93     // Get ready to start collecting text
94
if (textData == null || textData.length() > 0)
95     textData = new String JavaDoc();
96
97     if ("parameter".equals(tagName)) {
98     String JavaDoc id = attributes.getValue("id");
99     parameter = report.findParameter(id);
100     if (parameter == null)
101         ErrorHandler.error(I18N.get("ParameterReader.unknown_id") + ' '
102                    + id + ' '
103                    + I18N.get("ParameterReader.in_xml"));
104     }
105 }
106
107 public void endElement(final String JavaDoc namespaceURI, final String JavaDoc localName,
108                final String JavaDoc qName)
109     throws SAXException
110 {
111     String JavaDoc tagName = localName;
112     if (tagName == null || tagName.length() == 0)
113         tagName = qName;
114
115     if ("value".equals(tagName) && parameter != null)
116     parameter.addValue(textData);
117     else if ("parameter".equals(tagName))
118     parameter = null;
119 }
120
121 /**
122  * Reads text data. Text data inside a single tag can be broken up into
123  * multiple calls to this method.
124  */

125 public void characters(char ch[], int start, int length) {
126     textData += new String JavaDoc(ch, start, length);
127 }
128
129 }
130
Popular Tags