01: /*
02: * SAX2Reader.java
03: */
04: package com.sun.portal.sra.config;
05:
06: import javax.xml.parsers.SAXParserFactory;
07: import javax.xml.parsers.SAXParser;
08: import javax.xml.parsers.ParserConfigurationException;
09: import org.xml.sax.InputSource;
10: import org.xml.sax.SAXException;
11: import java.io.IOException;
12: import java.io.File;
13: import java.io.Reader;
14: import java.io.InputStream;
15:
16: public class SAX2Reader {
17: private final SAXParser parser;
18: private XMLHandler handler;
19:
20: public SAX2Reader() throws ParserConfigurationException,
21: SAXException {
22: this (true);
23: }
24:
25: public SAX2Reader(boolean validating)
26: throws ParserConfigurationException, SAXException {
27: SAXParserFactory spf = SAXParserFactory.newInstance();
28: spf.setValidating(validating);
29: spf.setNamespaceAware(true);
30: this .parser = spf.newSAXParser();
31: }
32:
33: public XMLElement parse(File f) throws SAXException, IOException {
34: parser.parse(f, this .getXMLHandler());
35: return this .handler.getResult();
36: }
37:
38: public XMLElement parse(InputSource is) throws SAXException,
39: IOException {
40: parser.parse(is, this .getXMLHandler());
41: return this .handler.getResult();
42: }
43:
44: public XMLElement parse(InputStream is) throws SAXException,
45: IOException {
46: return this .parse(new InputSource(is));
47: }
48:
49: public XMLElement parse(InputStream is, String systemId)
50: throws SAXException, IOException {
51: parser.parse(is, this .getXMLHandler(), systemId);
52: return this .handler.getResult();
53: }
54:
55: public XMLElement parse(String uri) throws SAXException,
56: IOException {
57: parser.parse(uri, this .getXMLHandler());
58: return this .handler.getResult();
59: }
60:
61: public XMLElement parse(Reader r) throws SAXException, IOException {
62: return this .parse(new InputSource(r));
63: }
64:
65: public XMLElement getResult() {
66: if (this .handler == null) {
67: return null;
68: }
69: return this .handler.getResult();
70: }
71:
72: public XMLHandler getXMLHandler() {
73: if (this .handler == null) {
74: this .handler = new XMLHandler();
75: }
76: return this .handler;
77:
78: }
79:
80: public void setXMLHandler(XMLHandler xhandler) {
81: this.handler = xhandler;
82:
83: }
84:
85: }
|