KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > Builder


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.Controller;
5 import net.sf.saxon.om.DocumentInfo;
6 import net.sf.saxon.om.NamePool;
7 import net.sf.saxon.om.NodeInfo;
8 import net.sf.saxon.om.StrippedDocument;
9 import net.sf.saxon.tinytree.TinyBuilder;
10 import net.sf.saxon.tinytree.TinyDocumentImpl;
11 import net.sf.saxon.trans.DynamicError;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.TreeBuilder;
14
15 import javax.xml.transform.Source JavaDoc;
16 import javax.xml.transform.dom.DOMSource JavaDoc;
17 import java.util.Date JavaDoc;
18
19 /**
20  * The abstract Builder class is responsible for taking a stream of SAX events
21  * and constructing a Document tree. There is one concrete subclass for each
22  * tree implementation.
23  * @author Michael H. Kay
24  */

25
26 public abstract class Builder implements Receiver {
27     public static final int STANDARD_TREE = 0;
28     public static final int TINY_TREE = 1;
29
30     protected PipelineConfiguration pipe;
31     protected Configuration config;
32     protected NamePool namePool;
33     protected String JavaDoc systemId;
34     protected NodeInfo currentRoot;
35     protected boolean lineNumbering = false;
36
37     protected boolean started = false;
38     protected boolean timing = false;
39
40     private long startTime;
41
42     /**
43      * create a Builder and initialise variables
44      */

45
46     public Builder() {
47     }
48
49     public void setPipelineConfiguration(PipelineConfiguration pipe) {
50         this.pipe = pipe;
51         this.config = pipe.getConfiguration();
52         this.namePool = config.getNamePool();
53         this.lineNumbering = (lineNumbering || config.isLineNumbering());
54     }
55
56     public PipelineConfiguration getPipelineConfiguration () {
57         return pipe;
58     }
59
60     public Configuration getConfiguration() {
61         return config;
62     }
63
64     public void setSystemId(String JavaDoc systemId) {
65         this.systemId = systemId;
66     }
67
68     public String JavaDoc getSystemId() {
69         return systemId;
70     }
71
72     public void setLineNumbering(boolean is) {
73         lineNumbering = is;
74     }
75
76     /////////////////////////////////////////////////////////////////////////
77
// Methods setting and getting options for building the tree
78
/////////////////////////////////////////////////////////////////////////
79

80     /**
81      * Set the root (document) node to use. This method is used to support
82      * the JAXP facility to attach transformation output to a supplied Document
83      * node. It must be called before startDocument(), and the type of document
84      * node must be compatible with the type of Builder used.
85      */

86
87 // public void setRootNode(DocumentInfo doc) {
88
// currentRoot = doc;
89
// }
90

91
92     /**
93      * Set timing option on or off
94      */

95
96     public void setTiming(boolean on) {
97         timing = on;
98     }
99
100     /**
101      * Get timing option
102      */

103
104     public boolean isTiming() {
105         return timing;
106     }
107
108     public void open() throws XPathException {
109         if (timing) {
110             System.err.println("Building tree for " + getSystemId() + " using " + getClass());
111             startTime = (new Date JavaDoc()).getTime();
112         }
113     }
114
115     public void close() throws XPathException {
116         if (timing) {
117             long endTime = (new Date JavaDoc()).getTime();
118             System.err.println("Tree built in " + (endTime - startTime) + " milliseconds");
119             if (currentRoot instanceof TinyDocumentImpl) {
120                 ((TinyDocumentImpl)currentRoot).showSize();
121             }
122             startTime = endTime;
123         }
124     }
125
126     /**
127      * Start of a document node.
128      * This event is ignored: we simply add the contained elements to the current document
129     */

130
131     public void startDocument(int properties) throws XPathException { }
132
133     /**
134      * Notify the end of a document node
135      */

136
137     public void endDocument() throws XPathException { }
138
139     /**
140      * Get the current root node. This will normally be a document node, but if the root of the tree
141      * is an element node, it can be an element.
142      * @return the root of the tree that is currently being built, or that has been most recently built
143      * using this builder
144      */

145
146     public NodeInfo getCurrentRoot() {
147         return currentRoot;
148     }
149
150     /**
151      * Static method to build a document from any kind of Source object. If the source
152      * is already in the form of a tree, it is wrapped as required.
153      * @param source Any javax.xml.transform.Source object
154      * @param stripper A stripper object, if whitespace text nodes are to be stripped;
155      * otherwise null.
156      * @param config The Configuration object
157      * @return the NodeInfo of the start node in the resulting document object.
158      */

159
160     public static NodeInfo build(Source JavaDoc source, Stripper stripper, Configuration config) throws XPathException {
161         if (source == null) {
162             throw new NullPointerException JavaDoc("Source supplied to builder cannot be null");
163         }
164
165         NodeInfo start;
166         if (source instanceof DOMSource JavaDoc || source instanceof NodeInfo) {
167             start = Controller.unravel(source, config);
168             if (stripper != null) {
169                 DocumentInfo docInfo = start.getDocumentRoot();
170                 StrippedDocument strippedDoc = new StrippedDocument(docInfo, stripper);
171                 start = strippedDoc.wrap(start);
172             }
173
174         } else {
175             // we have a SAXSource or StreamSource
176
Builder b;
177             if (config.getTreeModel() == Builder.TINY_TREE) {
178                 b = new TinyBuilder();
179             } else {
180                 b = new TreeBuilder();
181             }
182             PipelineConfiguration pipe = config.makePipelineConfiguration();
183             pipe.setConfiguration(config);
184             b.setPipelineConfiguration(pipe);
185             b.setLineNumbering(config.isLineNumbering());
186             Receiver receiver = b;
187             if (stripper != null) {
188                 stripper.setUnderlyingReceiver(b);
189                 receiver = stripper;
190             }
191             try {
192                 new Sender(pipe).send(source, receiver);
193             } catch (XPathException err) {
194                 throw new DynamicError(err);
195             }
196             start = b.getCurrentRoot();
197         }
198         return start;
199     }
200
201 }
202
203 //
204
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
205
// you may not use this file except in compliance with the License. You may obtain a copy of the
206
// License at http://www.mozilla.org/MPL/
207
//
208
// Software distributed under the License is distributed on an "AS IS" basis,
209
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
210
// See the License for the specific language governing rights and limitations under the License.
211
//
212
// The Original Code is: all this file.
213
//
214
// The Initial Developer of the Original Code is Michael H. Kay.
215
//
216
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
217
//
218
// Contributor(s): none.
219
//
220
Popular Tags