KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > xom > DocumentWrapper


1 package net.sf.saxon.xom;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.om.DocumentInfo;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.NodeInfo;
7 import net.sf.saxon.om.VirtualNode;
8 import net.sf.saxon.type.Type;
9 import nu.xom.Attribute;
10 import nu.xom.Document;
11 import nu.xom.Element;
12 import nu.xom.Node;
13
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * The root node of an XPath tree. (Or equivalently, the tree itself).
18  * <P>
19  * This class is used not only for the root of a document, but also for the root
20  * of a result tree fragment, which is not constrained to contain a single
21  * top-level element.
22  *
23  * @author Michael H. Kay
24  * @author Wolfgang Hoschek (ported net.sf.saxon.jdom to XOM)
25  */

26
27 public class DocumentWrapper extends NodeWrapper implements DocumentInfo {
28
29     protected Configuration config;
30
31     protected String JavaDoc baseURI;
32
33     protected int documentNumber;
34
35     private HashMap JavaDoc idIndex;
36
37     /**
38      * Create a Saxon wrapper for a XOM document
39      *
40      * @param doc
41      * The XOM document
42      * @param baseURI
43      * The base URI for all the nodes in the document
44      * @param config
45      * The configuration which defines the name pool used for all
46      * names in this document
47      */

48
49     public DocumentWrapper(Document doc, String JavaDoc baseURI, Configuration config) {
50         super(doc, null, 0);
51         this.node = doc;
52         this.nodeKind = Type.DOCUMENT;
53         this.baseURI = baseURI;
54         this.docWrapper = this;
55         setConfiguration(config);
56         XOMObjectModel model = (XOMObjectModel)config.findExternalObjectModel(doc);
57         model.addToDocumentMap(doc, this);
58     }
59
60     /**
61      * Wrap a node in the XOM document.
62      *
63      * @param node
64      * The node to be wrapped. This must be a node in the same
65      * document (the system does not check for this).
66      * @return the wrapping NodeInfo object
67      */

68
69     public VirtualNode wrap(Node node) {
70         if (node == this.node) {
71             return this;
72         }
73         return makeWrapper(node, this);
74     }
75
76     /**
77      * Set the configuration, which defines the name pool used for all names in
78      * this document. This is always called after a new document has been
79      * created. The implementation must register the name pool with the
80      * document, so that it can be retrieved using getNamePool(). It must also
81      * call NamePool.allocateDocumentNumber(), and return the relevant document
82      * number when getDocumentNumber() is subsequently called.
83      *
84      * @param config
85      * The configuration to be used
86      */

87
88     public void setConfiguration(Configuration config) {
89         this.config = config;
90         this.documentNumber = config.getDocumentNumberAllocator().allocateDocumentNumber();
91     }
92
93     /**
94      * Get the configuration previously set using setConfiguration
95      */

96
97     public Configuration getConfiguration() {
98         return config;
99     }
100
101     /**
102      * Get the name pool used for the names in this document
103      *
104      * @return the name pool in which all the names used in this document are
105      * registered
106      */

107
108     public NamePool getNamePool() {
109         return config.getNamePool();
110     }
111
112     /**
113      * Get the unique document number for this document (the number is unique
114      * for all documents within a NamePool)
115      *
116      * @return the unique number identifying this document within the name pool
117      */

118
119     public int getDocumentNumber() {
120         return documentNumber;
121     }
122
123     /**
124      * Get the element with a given ID, if any
125      *
126      * @param id
127      * the required ID value
128      * @return the element with the given ID, or null if there is no such ID
129      * present (or if the parser has not notified attributes as being of
130      * type ID). This implementation does not necessarily conform to the
131      * rule that if an invalid document contains two elements with the same ID, the one
132      * that comes last should be returned
133      */

134
135     public NodeInfo selectID(String JavaDoc id) {
136         if (idIndex == null) {
137             idIndex = new HashMap JavaDoc(50);
138             Document doc = (Document) node;
139             buildIDIndex(doc.getRootElement());
140         }
141         return (NodeInfo)idIndex.get(id);
142     }
143
144
145     private void buildIDIndex(Element elem) {
146         // walk the tree in reverse document order, to satisfy the XPath 1.0 rule
147
// that says if an ID appears twice, the first one wins
148
for (int i=elem.getChildCount(); --i >= 0 ; ) {
149             Node child = elem.getChild(i);
150             if (child instanceof Element) {
151                 buildIDIndex((Element)child);
152             }
153         }
154         for (int i=elem.getAttributeCount(); --i >= 0 ; ) {
155             Attribute att = elem.getAttribute(i);
156             if (att.getType() == Attribute.Type.ID) {
157                 idIndex.put(att.getValue(), wrap(elem));
158             }
159         }
160     }
161
162     /**
163      * Get the unparsed entity with a given name
164      *
165      * @param name
166      * the name of the entity
167      * @return null: XOM does not provide access to unparsed entities
168      * @return if the entity exists, return an array of two Strings, the first
169      * holding the system ID of the entity, the second holding the
170      * public ID if there is one, or null if not. If the entity does not
171      * exist, return null.
172      */

173
174     public String JavaDoc[] getUnparsedEntity(String JavaDoc name) {
175         return null;
176     }
177
178 }
179
180 //
181
// The contents of this file are subject to the Mozilla Public License Version
182
// 1.0 (the "License");
183
// you may not use this file except in compliance with the License. You may
184
// obtain a copy of the
185
// License at http://www.mozilla.org/MPL/
186
//
187
// Software distributed under the License is distributed on an "AS IS" basis,
188
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
189
// See the License for the specific language governing rights and limitations
190
// under the License.
191
//
192
// The Original Code is: all this file.
193
//
194
// The Initial Developer of the Original Code is Michael Kay
195
//
196
// Portions created by (your name) are Copyright (C) (your legal entity). All
197
// Rights Reserved.
198
//
199
// Contributor(s): none.
200
//
201
Popular Tags