Debian Codegraph @

net/sf/saxon/dom/DocumentWrapper.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.dom;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.AxisInfo;
13 import net.sf.saxon.om.GenericTreeInfo;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.pattern.NodeKindTest;
16 import net.sf.saxon.tree.iter.AxisIterator;
17 import org.w3c.dom.*;
18
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.*;
22
23 /**
24 * The tree info object for a tree implemented as a wrapper around a DOM Document.
25 * <p>Because the DOM is not thread-safe even when reading, and because Saxon-EE can spawn multiple
26 * threads that access the same input tree, all methods that invoke DOM methods are synchronized
27 * on the Document node</p>
28 */
29
30 public class DocumentWrapper extends GenericTreeInfo {
31
32 protected boolean domLevel3;
33 public final Node docNode;
34 private Map<String, NodeInfo> idIndex = null;
35
36 /**
37 * Wrap a DOM Document or DocumentFragment node
38 *
39 * @param doc a DOM Document or DocumentFragment node
40 * @param baseURI the base URI of the document
41 * @param config the Saxon configuration
42 */
43
44 public DocumentWrapper(Node doc, String baseURI, Configuration config) {
45 super(config);
46 if (doc.getNodeType() != Node.DOCUMENT_NODE && doc.getNodeType() != Node.DOCUMENT_FRAGMENT_NODE) {
47 throw new IllegalArgumentException("Node must be a DOM Document or DocumentFragment");
48 }
49 if (config.getExternalObjectModel(doc.getClass()) == null) {
50 throw new IllegalArgumentException(
51 "Node class " + doc.getClass().getName() + " is not recognized in this Saxon configuration");
52 }
53 domLevel3 = true;
54 this.docNode = doc;
55 setRootNode(wrap(doc));
56 setSystemId(baseURI);
57 }
58
59 /**
60 * Create a wrapper for a node in this document
61 *
62 * @param node the DOM node to be wrapped. This must be a node within the document wrapped by this
63 * DocumentWrapper
64 * @return the wrapped node
65 * @throws IllegalArgumentException if the node is not a descendant of the Document node wrapped by
66 * this DocumentWrapper
67 */
68
69 public DOMNodeWrapper wrap(Node node) {
70 return DOMNodeWrapper.makeWrapper(node, this);
71 // Document doc = node.getOwnerDocument();
72 // if (doc == this.node || (domLevel3 && doc != null && doc.isSameNode(this.node))) {
73 //
74 // } else {
75 // throw new IllegalArgumentException(
76 // "DocumentWrapper#wrap: supplied node does not belong to the wrapped DOM document");
77 // }
78 }
79
80 /**
81 * Set the level of DOM interface to be used
82 *
83 * @param level the DOM level. Must be 2 or 3. By default Saxon assumes that DOM level 3 is available;
84 * this parameter can be set to the value 2 to indicate that Saxon should not use methods unless they
85 * are available in DOM level 2. From Saxon 9.2, this switch remains available, but the use of
86 * DOM level 2 is untested and unsupported.
87 */
88
89 public void setDOMLevel(int level) {
90 if (!(level == 2 || level == 3)) {
91 throw new IllegalArgumentException("DOM Level must be 2 or 3");
92 }
93 domLevel3 = level == 3;
94 }
95
96 /**
97 * Get the level of DOM interface to be used
98 *
99 * @return the DOM level. Always 2 or 3.
100 */
101
102 public int getDOMLevel() {
103 return domLevel3 ? 3 : 2;
104 }
105
106 /**
107 * Get the element with a given ID, if any
108 *
109 * @param id the required ID value
110 * @param getParent true if the parent of the element having the given ID value is required
111 * @return a NodeInfo representing the element with the given ID, or null if there
112 * is no such element. This implementation does not necessarily conform to the
113 * rule that if an invalid document contains two elements with the same ID, the one
114 * that comes last should be returned.
115 */
116
117 public NodeInfo selectID(String id, boolean getParent) {
118 synchronized (docNode) {
119 Node node = ((DOMNodeWrapper)getRootNode()).node;
120 if (node instanceof Document) {
121 Node el = ((Document) node).getElementById(id);
122 if (el != null) {
123 return wrap(el);
124 }
125 }
126 // Search for xml:id attributes (which the DOM does not necessarily expose as ID values)
127 if (idIndex != null) {
128 return idIndex.get(id);
129 } else {
130 idIndex = new HashMap<>();
131 AxisIterator iter = getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT);
132 NodeInfo e;
133 while ((e = iter.next()) != null) {
134 String xmlId = e.getAttributeValue(NamespaceConstant.XML, "id");
135 if (xmlId != null) {
136 idIndex.put(xmlId, e);
137 }
138 }
139 return idIndex.get(id);
140 }
141 }
142 }
143
144
145 /**
146 * Get the list of unparsed entities defined in this document
147 *
148 * @return an Iterator, whose items are of type String, containing the names of all
149 * unparsed entities defined in this document. If there are no unparsed entities or if the
150 * information is not available then an empty iterator is returned
151 * @since 9.1 (implemented for this subclass since 9.2)
152 */
153
154 public Iterator<String> getUnparsedEntityNames() {
155 synchronized (docNode) {
156 Node node = ((DOMNodeWrapper) getRootNode()).node;
157 if (node instanceof Document) {
158 DocumentType docType = ((Document) node).getDoctype();
159 if (docType == null) {
160 List<String> ls = Collections.emptyList();
161 return ls.iterator();
162 }
163 NamedNodeMap map = docType.getEntities();
164 if (map == null) {
165 List<String> ls = Collections.emptyList();
166 return ls.iterator();
167 }
168 List<String> names = new ArrayList<>(map.getLength());
169 for (int i = 0; i < map.getLength(); i++) {
170 Entity e = (Entity) map.item(i);
171 if (e.getNotationName() != null) {
172 // it is an unparsed entity
173 names.add(e.getLocalName());
174 }
175 }
176 return names.iterator();
177 } else {
178 return null;
179 }
180 }
181 }
182
183 /**
184 * Get the unparsed entity with a given name
185 *
186 * @param name the name of the entity
187 * @return if the entity exists, return an array of two Strings, the first
188 * holding the system ID of the entity (as an absolute URI if possible),
189 * the second holding the public ID if there is one, or null if not.
190 * If the entity does not exist, the method returns null.
191 * Applications should be written on the assumption that this array may
192 * be extended in the future to provide additional information.
193 * @since 8.4 (implemented for this subclass since 9.2)
194 */
195
196 public String[] getUnparsedEntity(String name) {
197 synchronized (docNode) {
198 Node node = ((DOMNodeWrapper) getRootNode()).node;
199 if (node instanceof Document) {
200 DocumentType docType = ((Document) node).getDoctype();
201 if (docType == null) {
202 return null;
203 }
204 NamedNodeMap map = docType.getEntities();
205 if (map == null) {
206 return null;
207 }
208 Entity entity = (Entity) map.getNamedItem(name);
209 if (entity == null || entity.getNotationName() == null) {
210 // In the first case, no entity found. In the second case, it's a parsed entity.
211 return null;
212 }
213 String systemId = entity.getSystemId();
214 try {
215 URI systemIdURI = new URI(systemId);
216 if (!systemIdURI.isAbsolute()) {
217 String base = getRootNode().getBaseURI();
218 if (base != null) {
219 systemIdURI = new URI(base).resolve(systemIdURI);
220 systemId = systemIdURI.toString();
221 } else {
222 // base URI unknown: return the relative URI as written
223 }
224 }
225 } catch (URISyntaxException err) {
226 // invalid URI: no action - return the "URI" as written
227 }
228 return new String[]{systemId, entity.getPublicId()};
229 } else {
230 return null;
231 }
232 }
233 }
234
235
236 }
237