001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.om.DocumentInfo;
005: import net.sf.saxon.om.NamePool;
006: import net.sf.saxon.om.NodeInfo;
007: import net.sf.saxon.type.Type;
008: import org.w3c.dom.Document;
009: import org.w3c.dom.Node;
010:
011: import java.lang.reflect.Method;
012:
013: /**
014: * The document node of a tree implemented as a wrapper around a DOM Document.
015: */
016:
017: public class DocumentWrapper extends NodeWrapper implements
018: DocumentInfo {
019:
020: protected Configuration config;
021: protected String baseURI;
022: protected int documentNumber;
023: protected boolean level3 = false;
024:
025: public DocumentWrapper(Document doc, String baseURI,
026: Configuration config) {
027: super (doc, null, 0);
028: node = doc;
029: nodeKind = Type.DOCUMENT;
030: this .baseURI = baseURI;
031: docWrapper = this ;
032:
033: // Find out if this is a level-3 DOM implementation
034: Method[] methods = doc.getClass().getMethods();
035: for (int i = 0; i < methods.length; i++) {
036: if (methods[i].getName().equals("isSameNode")) {
037: level3 = true;
038: break;
039: }
040: }
041:
042: setConfiguration(config);
043: }
044:
045: /**
046: * Create a wrapper for a node in this document
047: *
048: * @param node the DOM node to be wrapped. This must be a node within the document wrapped by this
049: * DocumentWrapper
050: * @throws IllegalArgumentException if the node is not a descendant of the Document node wrapped by
051: * this DocumentWrapper
052: */
053:
054: public NodeWrapper wrap(Node node) {
055: if (node == this .node) {
056: return this ;
057: }
058: if (node.getOwnerDocument() == this .node) {
059: return makeWrapper(node, this );
060: } else {
061: throw new IllegalArgumentException(
062: "DocumentWrapper#wrap: supplied node does not belong to the wrapped DOM document");
063: }
064: }
065:
066: /**
067: * Set the Configuration that contains this document
068: */
069:
070: public void setConfiguration(Configuration config) {
071: this .config = config;
072: documentNumber = config.getDocumentNumberAllocator()
073: .allocateDocumentNumber();
074: }
075:
076: /**
077: * Get the configuration previously set using setConfiguration
078: */
079:
080: public Configuration getConfiguration() {
081: return config;
082: }
083:
084: /**
085: * Get the name pool used for the names in this document
086: */
087:
088: public NamePool getNamePool() {
089: return config.getNamePool();
090: }
091:
092: /**
093: * Get the unique document number
094: */
095:
096: public int getDocumentNumber() {
097: return documentNumber;
098: }
099:
100: /**
101: * Get the element with a given ID, if any
102: *
103: * @param id the required ID value
104: * @return a NodeInfo representing the element with the given ID, or null if there
105: * is no such element. This implementation does not necessarily conform to the
106: * rule that if an invalid document contains two elements with the same ID, the one
107: * that comes last should be returned.
108: */
109:
110: public NodeInfo selectID(String id) {
111: Node el = ((Document) node).getElementById(id);
112: if (el == null) {
113: return null;
114: }
115: return wrap(el);
116: }
117:
118: /**
119: * Determine whether this is the same node as another node. <br />
120: * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)
121: *
122: * @return true if this Node object and the supplied Node object represent the
123: * same node in the tree.
124: */
125:
126: public boolean isSameNodeInfo(NodeInfo other) {
127: if (!(other instanceof DocumentWrapper)) {
128: return false;
129: }
130: return node == ((DocumentWrapper) other).node;
131: }
132:
133: /**
134: * Get the unparsed entity with a given name
135: *
136: * @param name the name of the entity
137: * @return null: JDOM does not provide access to unparsed entities
138: */
139:
140: public String[] getUnparsedEntity(String name) {
141: return null;
142: }
143:
144: }
145:
146: //
147: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
148: // you may not use this file except in compliance with the License. You may obtain a copy of the
149: // License at http://www.mozilla.org/MPL/
150: //
151: // Software distributed under the License is distributed on an "AS IS" basis,
152: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
153: // See the License for the specific language governing rights and limitations under the License.
154: //
155: // The Original Code is: all this file.
156: //
157: // The Initial Developer of the Original Code is Michael H. Kay
158: //
159: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
160: //
161: // Contributor(s): none.
162: //
|