KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > DocumentBuilderImpl


1 package net.sf.saxon.dom;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.event.Builder;
4 import net.sf.saxon.event.PipelineConfiguration;
5 import net.sf.saxon.event.Sender;
6 import net.sf.saxon.om.NamePool;
7 import net.sf.saxon.tinytree.TinyBuilder;
8 import net.sf.saxon.tinytree.TinyDocumentImpl;
9 import net.sf.saxon.trans.XPathException;
10 import org.w3c.dom.DOMImplementation JavaDoc;
11 import org.w3c.dom.Document JavaDoc;
12 import org.xml.sax.EntityResolver JavaDoc;
13 import org.xml.sax.ErrorHandler JavaDoc;
14 import org.xml.sax.InputSource JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
16
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.transform.sax.SAXSource JavaDoc;
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 JavaDoc {
28
29     private EntityResolver JavaDoc entityResolver;
30     private ErrorHandler JavaDoc errorHandler;
31
32     public boolean isNamespaceAware() {
33         return true;
34     }
35
36     public boolean isValidating() {
37         return false;
38     }
39
40     public Document JavaDoc 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
// Builder builder = new TinyBuilder();
44
// Configuration config = new Configuration();
45
// PipelineConfiguration pipe = config.makePipelineConfiguration();
46
// builder.setPipelineConfiguration(pipe);
47
// DocumentInfo doc;
48
// try {
49
// builder.open();
50
// builder.startDocument(0);
51
// doc = (DocumentInfo)builder.getCurrentRoot();
52
// builder.endDocument();
53
// builder.close();
54
// } catch (XPathException e) {
55
// throw new IllegalStateException(e.getMessage());
56
// }
57
// return (Document)DocumentOverNodeInfo.wrap(doc);
58
return new DocumentOverNodeInfo();
59     }
60
61     public Document JavaDoc parse(InputSource JavaDoc in) throws SAXException JavaDoc {
62         try {
63             Builder builder = new TinyBuilder();
64             Configuration config = new Configuration();
65             NamePool pool = config.getNamePool();
66             PipelineConfiguration pipe = config.makePipelineConfiguration();
67             builder.setPipelineConfiguration(pipe);
68             SAXSource JavaDoc source = new SAXSource JavaDoc(in);
69             if (entityResolver != null) {
70                 source.getXMLReader().setEntityResolver(entityResolver);
71             }
72             if (errorHandler != null) {
73                 source.getXMLReader().setErrorHandler(errorHandler);
74             }
75             source.setSystemId(in.getSystemId());
76             new Sender(pipe).send(source, builder);
77             TinyDocumentImpl doc = (TinyDocumentImpl)builder.getCurrentRoot();
78             //pool.allocateDocumentNumber(doc);
79
return (Document JavaDoc)DocumentOverNodeInfo.wrap(doc);
80         } catch (XPathException err) {
81             throw new SAXException JavaDoc(err);
82         }
83     }
84
85     public void setEntityResolver(EntityResolver JavaDoc er) {
86         entityResolver = er;
87     }
88
89     public void setErrorHandler(ErrorHandler JavaDoc eh) {
90         errorHandler = eh;
91     }
92
93     public DOMImplementation JavaDoc getDOMImplementation() {
94         return newDocument().getImplementation();
95     }
96 }
97
98
99 //
100
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
101
// you may not use this file except in compliance with the License. You may obtain a copy of the
102
// License at http://www.mozilla.org/MPL/
103
//
104
// Software distributed under the License is distributed on an "AS IS" basis,
105
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
106
// See the License for the specific language governing rights and limitations under the License.
107
//
108
// The Original Code is: all this file.
109
//
110
// The Initial Developer of the Original Code is Michael H. Kay
111
//
112
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
113
//
114
// Contributor(s): none
115
//
Popular Tags