KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > TinyDocumentImpl


1 package net.sf.saxon.tinytree;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.om.*;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.Type;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.List JavaDoc;
11
12
13 /**
14   * A node in the XML parse tree representing the Document itself (or equivalently, the root
15   * node of the Document).<P>
16   */

17
18 public final class TinyDocumentImpl extends TinyParentNodeImpl
19     implements DocumentInfo {
20
21     private HashMap JavaDoc idTable = null;
22     private HashMap JavaDoc elementList = null;
23     private HashMap JavaDoc entityTable = null;
24
25
26     public TinyDocumentImpl(TinyTree tree) {
27         this.tree = tree;
28     }
29
30     /**
31      * Get the tree containing this node
32      */

33
34     public TinyTree getTree() {
35         return tree;
36     }
37
38     /**
39     * Set the Configuration that contains this document
40     */

41
42     public void setConfiguration(Configuration config) {
43         if (config != tree.getConfiguration()) {
44             throw new IllegalArgumentException JavaDoc(
45                     "Configuration of document differs from that of the supporting TinyTree");
46         }
47     }
48
49     /**
50      * Get the configuration previously set using setConfiguration
51      */

52
53     public Configuration getConfiguration() {
54         return tree.getConfiguration();
55     }
56
57     /**
58     * Set the system id of this node
59     */

60
61     public void setSystemId(String JavaDoc uri) {
62         tree.setSystemId(nodeNr, uri);
63     }
64
65     /**
66     * Get the system id of this root node
67     */

68
69     public String JavaDoc getSystemId() {
70         return tree.getSystemId(nodeNr);
71     }
72
73     /**
74     * Get the base URI of this root node. For a root node the base URI is the same as the
75     * System ID.
76     */

77
78     public String JavaDoc getBaseURI() {
79         return getSystemId();
80     }
81
82     /**
83     * Get the line number of this root node.
84     * @return 0 always
85     */

86
87     public int getLineNumber() {
88         return 0;
89     }
90
91     /**
92     * Return the type of node.
93     * @return Type.DOCUMENT (always)
94     */

95
96     public final int getNodeKind() {
97         return Type.DOCUMENT;
98     }
99
100     /**
101      * Find the parent node of this node.
102      * @return The Node object describing the containing element or root node.
103      */

104
105     public NodeInfo getParent() {
106         return null;
107     }
108
109     /**
110     * Get the root node
111     * @return the NodeInfo that is the root of the tree - not necessarily a document node
112     */

113
114     public NodeInfo getRoot() {
115         return this;
116     }
117
118     /**
119     * Get the root (document) node
120     * @return the DocumentInfo representing the document node, or null if the
121     * root of the tree is not a document node
122     */

123
124     public DocumentInfo getDocumentRoot() {
125         return this;
126     }
127
128     /**
129     * Get a character string that uniquely identifies this node
130     * @return an identifier based on the document number
131     */

132
133     public String JavaDoc generateId() {
134         return "d"+getDocumentNumber();
135     }
136
137     /**
138     * Get a list of all elements with a given name. This is implemented
139     * as a memo function: the first time it is called for a particular
140     * element type, it remembers the result for next time.
141     */

142
143     AxisIterator getAllElements(int fingerprint) {
144         Integer JavaDoc key = new Integer JavaDoc(fingerprint);
145         if (elementList==null) {
146             elementList = new HashMap JavaDoc(20);
147         }
148         List JavaDoc list = (List JavaDoc)elementList.get(key);
149         if (list==null) {
150             list = getElementList(fingerprint);
151             elementList.put(key, list);
152         }
153         return new ListIterator(list);
154     }
155
156     /**
157      * Get a list containing all the elements with a given element name
158      * @param fingerprint the fingerprint of the element name
159      * @return list a List containing the TinyElementImpl objects
160      */

161
162     List JavaDoc getElementList(int fingerprint) {
163         List JavaDoc list = new ArrayList JavaDoc(tree.getNumberOfNodes()/20);
164         int i = nodeNr+1;
165         while (tree.depth[i] != 0) {
166             if (tree.nodeKind[i]==Type.ELEMENT &&
167                     (tree.nameCode[i] & 0xfffff) == fingerprint) {
168                 list.add(tree.getNode(i));
169             }
170             i++;
171         }
172         return list;
173     }
174
175     /**
176     * Register a unique element ID. Fails if there is already an element with that ID.
177     * @param e The NodeInfo (always an element) having a particular unique ID value
178     * @param id The unique ID value. The caller is responsible for checking that this
179      * is a valid NCName.
180     */

181
182     void registerID(NodeInfo e, String JavaDoc id) {
183         if (idTable==null) {
184             idTable = new HashMap JavaDoc(256);
185         }
186
187         // the XPath spec (5.2.1) says ignore the second ID if it's not unique
188
NodeInfo old = (NodeInfo)idTable.get(id);
189         if (old==null) {
190             idTable.put(id, e);
191         }
192
193     }
194
195     /**
196     * Get the element with a given ID.
197     * @param id The unique ID of the required element, previously registered using registerID()
198     * @return The NodeInfo (always an Element) for the given ID if one has been registered,
199     * otherwise null.
200     */

201
202     public NodeInfo selectID(String JavaDoc id) {
203         if (idTable==null) return null; // no ID values found
204
return (NodeInfo)idTable.get(id);
205     }
206
207     /**
208     * Set an unparsed entity URI associated with this document. For system use only, while
209     * building the document.
210     */

211
212     void setUnparsedEntity(String JavaDoc name, String JavaDoc uri, String JavaDoc publicId) {
213         if (entityTable==null) {
214             entityTable = new HashMap JavaDoc(20);
215         }
216         String JavaDoc[] ids = new String JavaDoc[2];
217         ids[0] = uri;
218         ids[1] = publicId;
219         entityTable.put(name, ids);
220     }
221
222     /**
223     * Get the unparsed entity with a given nameID if there is one, or null if not. If the entity
224     * does not exist, return null.
225     * @param name the name of the entity
226     * @return if the entity exists, return an array of two Strings, the first holding the system ID
227     * of the entity, the second holding the public
228     */

229
230     public String JavaDoc[] getUnparsedEntity(String JavaDoc name) {
231         if (entityTable==null) {
232             return null;
233         }
234         return (String JavaDoc[])entityTable.get(name);
235     }
236
237     /**
238     * Copy this node to a given outputter
239     */

240
241     public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
242
243         out.startDocument(0);
244
245         // output the children
246

247         AxisIterator children = iterateAxis(Axis.CHILD);
248         while (true) {
249             NodeInfo n = (NodeInfo)children.next();
250             if (n == null) {
251                 break;
252             }
253             n.copy(out, whichNamespaces, copyAnnotations, locationId);
254         }
255
256         out.endDocument();
257     }
258
259     public void showSize() {
260         tree.showSize();
261     }
262
263 }
264
265 //
266
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
267
// you may not use this file except in compliance with the License. You may obtain a copy of the
268
// License at http://www.mozilla.org/MPL/
269
//
270
// Software distributed under the License is distributed on an "AS IS" basis,
271
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
272
// See the License for the specific language governing rights and limitations under the License.
273
//
274
// The Original Code is: all this file
275
//
276
// The Initial Developer of the Original Code is Michael H. Kay.
277
//
278
// Contributor(s):
279
//
280
Popular Tags