KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.dom;
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.type.Type;
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.Node JavaDoc;
10
11 import java.lang.reflect.Method JavaDoc;
12
13 /**
14  * The document node of a tree implemented as a wrapper around a DOM Document.
15  */

16
17 public class DocumentWrapper extends NodeWrapper implements DocumentInfo {
18
19     protected Configuration config;
20     protected String JavaDoc baseURI;
21     protected int documentNumber;
22     protected boolean level3 = false;
23
24     public DocumentWrapper(Document JavaDoc doc, String JavaDoc baseURI, Configuration config) {
25         super(doc, null, 0);
26         node = doc;
27         nodeKind = Type.DOCUMENT;
28         this.baseURI = baseURI;
29         docWrapper = this;
30
31         // Find out if this is a level-3 DOM implementation
32
Method JavaDoc[] methods = doc.getClass().getMethods();
33         for (int i=0; i<methods.length; i++) {
34             if (methods[i].getName().equals("isSameNode")) {
35                 level3 = true;
36                 break;
37             }
38         }
39
40         setConfiguration(config);
41     }
42
43     /**
44      * Create a wrapper for a node in this document
45      *
46      * @param node the DOM node to be wrapped. This must be a node within the document wrapped by this
47      * DocumentWrapper
48      * @throws IllegalArgumentException if the node is not a descendant of the Document node wrapped by
49      * this DocumentWrapper
50      */

51
52     public NodeWrapper wrap(Node JavaDoc node) {
53         if (node == this.node) {
54             return this;
55         }
56         if (node.getOwnerDocument() == this.node) {
57             return makeWrapper(node, this);
58         } else {
59             throw new IllegalArgumentException JavaDoc(
60                 "DocumentWrapper#wrap: supplied node does not belong to the wrapped DOM document");
61         }
62     }
63
64     /**
65      * Set the Configuration that contains this document
66      */

67
68     public void setConfiguration(Configuration config) {
69         this.config = config;
70         documentNumber = config.getDocumentNumberAllocator().allocateDocumentNumber();
71     }
72
73     /**
74      * Get the configuration previously set using setConfiguration
75      */

76
77     public Configuration getConfiguration() {
78         return config;
79     }
80
81     /**
82      * Get the name pool used for the names in this document
83      */

84
85     public NamePool getNamePool() {
86         return config.getNamePool();
87     }
88
89     /**
90      * Get the unique document number
91      */

92
93     public int getDocumentNumber() {
94         return documentNumber;
95     }
96
97     /**
98      * Get the element with a given ID, if any
99      *
100      * @param id the required ID value
101      * @return a NodeInfo representing the element with the given ID, or null if there
102      * is no such element. This implementation does not necessarily conform to the
103      * rule that if an invalid document contains two elements with the same ID, the one
104      * that comes last should be returned.
105      */

106
107     public NodeInfo selectID(String JavaDoc id) {
108         Node JavaDoc el = ((Document JavaDoc)node).getElementById(id);
109         if (el == null) {
110             return null;
111         }
112         return wrap(el);
113     }
114
115     /**
116      * Determine whether this is the same node as another node. <br />
117      * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)
118      *
119      * @return true if this Node object and the supplied Node object represent the
120      * same node in the tree.
121      */

122
123     public boolean isSameNodeInfo(NodeInfo other) {
124         if (!(other instanceof DocumentWrapper)) {
125             return false;
126         }
127         return node == ((DocumentWrapper)other).node;
128     }
129
130     /**
131      * Get the unparsed entity with a given name
132      *
133      * @param name the name of the entity
134      * @return null: JDOM does not provide access to unparsed entities
135      */

136
137     public String JavaDoc[] getUnparsedEntity(String JavaDoc name) {
138         return null;
139     }
140
141
142 }
143
144 //
145
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
146
// you may not use this file except in compliance with the License. You may obtain a copy of the
147
// License at http://www.mozilla.org/MPL/
148
//
149
// Software distributed under the License is distributed on an "AS IS" basis,
150
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
151
// See the License for the specific language governing rights and limitations under the License.
152
//
153
// The Original Code is: all this file.
154
//
155
// The Initial Developer of the Original Code is Michael H. Kay
156
//
157
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
158
//
159
// Contributor(s): none.
160
//
161
Popular Tags