001: package net.xoetrope.xml.jaxp;
002:
003: import java.io.IOException;
004: import java.io.Reader;
005: import javax.xml.parsers.DocumentBuilder;
006: import javax.xml.parsers.DocumentBuilderFactory;
007: import javax.xml.parsers.ParserConfigurationException;
008:
009: import org.w3c.dom.Document;
010: import org.xml.sax.InputSource;
011: import org.xml.sax.SAXException;
012:
013: import net.xoetrope.xml.XmlElement;
014: import net.xoetrope.xml.XmlParser;
015: import org.xml.sax.ErrorHandler;
016:
017: /**
018: * A wrapper for JAXP parsers
019: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
020: * <p> $Revision: 1.2 $</p>
021: * <p> License: see License.txt</p>
022: */
023: public class JaxpXmlParser implements XmlParser {
024: /** Constants used for JAXP 1.2 */
025: public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
026: public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
027: public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
028:
029: private static ErrorHandler errorHandler;
030:
031: public JaxpXmlParser() {
032: }
033:
034: public XmlElement createXmlElement(String name) {
035: throw new UnsupportedOperationException();
036: }
037:
038: public XmlElement parse(Reader input) {
039: // Step 1: create a DocumentBuilderFactory and setNamespaceAware
040: DocumentBuilderFactory dbf = DocumentBuilderFactory
041: .newInstance();
042: dbf.setNamespaceAware(true);
043:
044: // Step 2: create a DocumentBuilder
045: DocumentBuilder db = null;
046: try {
047: db = dbf.newDocumentBuilder();
048: } catch (ParserConfigurationException ex) {
049: ex.printStackTrace();
050: }
051:
052: // Step 3: parse the input file to get a Document object
053: Document doc = null;
054: try {
055: doc = db.parse(new InputSource(input));
056: } catch (IOException ex) {
057: ex.printStackTrace();
058: } catch (SAXException ex) {
059: ex.printStackTrace();
060: }
061:
062: return new JaxpXmlElement(doc.getDocumentElement());
063: }
064:
065: public XmlElement parse(Reader input, String schemaSource) {
066: // Step 1: create a DocumentBuilderFactory and setNamespaceAware
067: DocumentBuilderFactory dbf = DocumentBuilderFactory
068: .newInstance();
069: dbf.setNamespaceAware(true);
070: dbf.setValidating(true);
071:
072: try {
073: dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
074: } catch (IllegalArgumentException ex1) {
075: // JAXP 1.2 not supported.
076: }
077:
078: if (schemaSource != null)
079: dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaSource);
080:
081: // Step 2: create a DocumentBuilder
082: DocumentBuilder db = null;
083: try {
084: db = dbf.newDocumentBuilder();
085: if (errorHandler != null)
086: db.setErrorHandler(errorHandler);
087: } catch (ParserConfigurationException ex) {
088: ex.printStackTrace();
089: }
090:
091: // Step 3: parse the input file to get a Document object
092: Document doc = null;
093: try {
094: doc = db.parse(new InputSource(input));
095: } catch (IOException ex) {
096: ex.printStackTrace();
097: } catch (SAXException ex) {
098: ex.printStackTrace();
099: }
100:
101: return new JaxpXmlElement(doc.getDocumentElement());
102: }
103:
104: public static void setErrorHandler(ErrorHandler eh) {
105: errorHandler = eh;
106: }
107: }
|