01: package net.sf.saxon.dom;
02:
03: import net.sf.saxon.Configuration;
04: import net.sf.saxon.event.Builder;
05: import net.sf.saxon.event.PipelineConfiguration;
06: import net.sf.saxon.event.Sender;
07: import net.sf.saxon.tinytree.TinyBuilder;
08: import net.sf.saxon.tinytree.TinyDocumentImpl;
09: import net.sf.saxon.trans.XPathException;
10: import org.w3c.dom.DOMImplementation;
11: import org.w3c.dom.Document;
12: import org.xml.sax.EntityResolver;
13: import org.xml.sax.ErrorHandler;
14: import org.xml.sax.InputSource;
15: import org.xml.sax.SAXException;
16:
17: import javax.xml.parsers.DocumentBuilder;
18: import javax.xml.transform.sax.SAXSource;
19:
20: /**
21: * This class implements the JAXP DocumentBuilder interface, allowing a Saxon TinyTree to be
22: * constructed using standard JAXP parsing interfaces. Note that although the TinyTree
23: * implements the DOM interfaces, it is read-only, and all attempts to update it will throw
24: * an exception. No schema or DTD validation is carried out on the document.
25: */
26:
27: public class DocumentBuilderImpl extends DocumentBuilder {
28:
29: private EntityResolver entityResolver;
30: private ErrorHandler errorHandler;
31:
32: public boolean isNamespaceAware() {
33: return true;
34: }
35:
36: public boolean isValidating() {
37: return false;
38: }
39:
40: public Document newDocument() {
41: // The returned document will be of little use, because it is immutable.
42: // But it can be used in a DOMResult as the result of a transformation
43: return new DocumentOverNodeInfo();
44: }
45:
46: public Document parse(InputSource in) throws SAXException {
47: try {
48: Builder builder = new TinyBuilder();
49: Configuration config = new Configuration();
50: PipelineConfiguration pipe = config
51: .makePipelineConfiguration();
52: builder.setPipelineConfiguration(pipe);
53: SAXSource source = new SAXSource(in);
54: if (entityResolver != null) {
55: source.getXMLReader().setEntityResolver(entityResolver);
56: }
57: if (errorHandler != null) {
58: source.getXMLReader().setErrorHandler(errorHandler);
59: }
60: source.setSystemId(in.getSystemId());
61: new Sender(pipe).send(source, builder);
62: TinyDocumentImpl doc = (TinyDocumentImpl) builder
63: .getCurrentRoot();
64: return (Document) DocumentOverNodeInfo.wrap(doc);
65: } catch (XPathException err) {
66: throw new SAXException(err);
67: }
68: }
69:
70: public void setEntityResolver(EntityResolver er) {
71: entityResolver = er;
72: }
73:
74: public void setErrorHandler(ErrorHandler eh) {
75: errorHandler = eh;
76: }
77:
78: public DOMImplementation getDOMImplementation() {
79: return newDocument().getImplementation();
80: }
81: }
82:
83: //
84: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
85: // you may not use this file except in compliance with the License. You may obtain a copy of the
86: // License at http://www.mozilla.org/MPL/
87: //
88: // Software distributed under the License is distributed on an "AS IS" basis,
89: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
90: // See the License for the specific language governing rights and limitations under the License.
91: //
92: // The Original Code is: all this file.
93: //
94: // The Initial Developer of the Original Code is Michael H. Kay
95: //
96: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
97: //
98: // Contributor(s): none
99: //
|