001: package jimm.datavision;
002:
003: import jimm.util.I18N;
004: import java.io.*;
005: import javax.xml.parsers.SAXParser;
006: import javax.xml.parsers.SAXParserFactory;
007: import org.xml.sax.*;
008: import org.xml.sax.helpers.DefaultHandler;
009:
010: /**
011: * A parameter reader reads an XML file and sets a {@link Report}'s parameter
012: * values. This class is used when the report is being run from the
013: * command line and the user has given us the name of an XML file containing
014: * parameter elements.
015: * <p>
016: * Unlike a {@link ReportReader}, a parameter reader's constructor
017: * takes not only the report but also the input method (file name, stream,
018: * or reader). That way the report object doesn't have to know how to hold
019: * on to those multipule input types.
020: *
021: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
022: */
023: public class ParameterReader extends DefaultHandler {
024:
025: /**
026: * If there is no report element dtd-version attribute, this is the
027: * default value to use.
028: */
029: protected static final double DEFAULT_DTD_VERSION = 0.1;
030:
031: protected Report report;
032: protected Parameter parameter;
033: protected String textData;
034: protected File inFile;
035: protected InputSource inInputSource;
036:
037: /**
038: * Constructor.
039: *
040: * @param report the report whose parameters we are setting
041: * @param f the parameter XML file
042: */
043: public ParameterReader(Report report, File f) {
044: this .report = report;
045: inFile = f;
046: }
047:
048: /**
049: * Constructor. To specify a URL, use <code>new
050: * InputSource("http://...")</code>.
051: *
052: * @param report the report whose parameters we are setting
053: * @param in the param XML input source
054: */
055: public ParameterReader(Report report, InputSource in) {
056: this .report = report;
057: inInputSource = in;
058: }
059:
060: /**
061: * Returns the file name or, if that is <code>null</code>, the class name of
062: * whatever input source was handed to a constructor.
063: *
064: * @return a file name or class name
065: */
066: public String getInputName() {
067: if (inFile != null)
068: return inFile.getPath();
069: if (inInputSource != null)
070: return "org.xml.sax.InputSource";
071: return "?";
072: }
073:
074: /**
075: * Reads parameter values from whichever input method was specified
076: * in the constructor.
077: */
078: public void read() throws Exception {
079: SAXParser parser = SAXParserFactory.newInstance()
080: .newSAXParser();
081: if (inFile != null)
082: parser.parse(inFile, this );
083: else if (inInputSource != null)
084: parser.parse(inInputSource, this );
085: }
086:
087: public void startElement(final String namespaceURI,
088: final String localName, final String qName,
089: final Attributes attributes) throws SAXException {
090: String tagName = localName;
091: if (tagName == null || tagName.length() == 0)
092: tagName = qName;
093:
094: // Get ready to start collecting text
095: if (textData == null || textData.length() > 0)
096: textData = new String();
097:
098: if ("parameter".equals(tagName)) {
099: String id = attributes.getValue("id");
100: parameter = report.findParameter(id);
101: if (parameter == null)
102: ErrorHandler.error(I18N
103: .get("ParameterReader.unknown_id")
104: + ' '
105: + id
106: + ' '
107: + I18N.get("ParameterReader.in_xml"));
108: }
109: }
110:
111: public void endElement(final String namespaceURI,
112: final String localName, final String qName)
113: throws SAXException {
114: String tagName = localName;
115: if (tagName == null || tagName.length() == 0)
116: tagName = qName;
117:
118: if ("value".equals(tagName) && parameter != null)
119: parameter.addValue(textData);
120: else if ("parameter".equals(tagName))
121: parameter = null;
122: }
123:
124: /**
125: * Reads text data. Text data inside a single tag can be broken up into
126: * multiple calls to this method.
127: */
128: public void characters(char ch[], int start, int length) {
129: textData += new String(ch, start, length);
130: }
131:
132: }
|