Debian Codegraph @

net/sf/saxon/tree/tiny/TinyDocumentImpl.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.tree.tiny;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.AtomicSequence;
14 import net.sf.saxon.om.AxisInfo;
15 import net.sf.saxon.om.CopyOptions;
16 import net.sf.saxon.om.NodeInfo;
17 import net.sf.saxon.pattern.NameTest;
18 import net.sf.saxon.pattern.NodeKindTest;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.tree.iter.AxisIterator;
21 import net.sf.saxon.tree.util.FastStringBuffer;
22 import net.sf.saxon.type.AnyType;
23 import net.sf.saxon.type.SchemaType;
24 import net.sf.saxon.type.Type;
25 import net.sf.saxon.type.Untyped;
26 import net.sf.saxon.value.UntypedAtomicValue;
27 import net.sf.saxon.z.IntHashMap;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33
34 /**
35 * A node in the XML parse tree representing the Document itself (or equivalently, the root
36 * node of the Document).
37 */
38
39 public final class TinyDocumentImpl extends TinyParentNodeImpl {
40
41
42 private IntHashMap<List<NodeInfo>> elementList;
43 private String baseURI;
44
45
46 public TinyDocumentImpl(/*@NotNull*/ TinyTree tree) {
47 this.tree = tree;
48 nodeNr = 0;
49 }
50
51 /**
52 * Get the tree containing this node
53 */
54
55 public TinyTree getTree() {
56 return tree;
57 }
58
59 /**
60 * Get the NodeInfo object representing the document node at the root of the tree
61 *
62 * @return the document node
63 */
64
65 public NodeInfo getRootNode() {
66 return this;
67 }
68
69 /**
70 * Get the configuration previously set using setConfiguration
71 */
72
73 public Configuration getConfiguration() {
74 return tree.getConfiguration();
75 }
76
77 /**
78 * Set the system id of this node
79 */
80
81 public void setSystemId(String uri) {
82 tree.setSystemId(nodeNr, uri);
83 }
84
85 /**
86 * Get the system id of this root node
87 */
88
89 public String getSystemId() {
90 return tree.getSystemId(nodeNr);
91 }
92
93 /**
94 * Set the base URI of this document node
95 *
96 * @param uri the base URI
97 */
98
99 public void setBaseURI(String uri) {
100 baseURI = uri;
101 }
102
103 /**
104 * Get the base URI of this root node.
105 */
106
107 public String getBaseURI() {
108 if (baseURI != null) {
109 return baseURI;
110 }
111 return getSystemId();
112 }
113
114 /**
115 * Get the line number of this root node.
116 *
117 * @return 0 always
118 */
119
120 public int getLineNumber() {
121 return 0;
122 }
123
124 /**
125 * Ask whether the document contains any nodes whose type annotation is anything other than
126 * UNTYPED
127 *
128 * @return true if the document contains elements whose type is other than UNTYPED
129 */
130 public boolean isTyped() {
131 return tree.getTypeArray() != null;
132 }
133
134 /**
135 * Return the type of node.
136 *
137 * @return Type.DOCUMENT (always)
138 */
139
140 public final int getNodeKind() {
141 return Type.DOCUMENT;
142 }
143
144 /**
145 * Find the parent node of this node.
146 *
147 * @return The Node object describing the containing element or root node.
148 */
149
150 /*@Nullable*/
151 public TinyNodeImpl getParent() {
152 return null;
153 }
154
155 /**
156 * Get the root node
157 *
158 * @return the NodeInfo that is the root of the tree - not necessarily a document node
159 */
160
161 /*@NotNull*/
162 public NodeInfo getRoot() {
163 return this;
164 }
165
166 /**
167 * Get a character string that uniquely identifies this node
168 *
169 * @param buffer to contain an identifier based on the document number
170 */
171
172 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
173 buffer.append('d');
174 buffer.append(Long.toString(getTreeInfo().getDocumentNumber()));
175 }
176
177 /**
178 * Get the typed value.
179 *
180 * @return the typed value. This will either be a single AtomicValue or a Value whose items are
181 * atomic values.
182 */
183
184 /*@NotNull*/
185 public AtomicSequence atomize() throws XPathException {
186 return new UntypedAtomicValue(getStringValueCS());
187 }
188
189 /**
190 * Get a list of all elements with a given name. This is implemented
191 * as a memo function: the first time it is called for a particular
192 * element type, it remembers the result for next time.
193 *
194 * @param fingerprint the fingerprint identifying the required element name
195 * @return an iterator over all elements with this name
196 */
197
198 /*@NotNull*/ AxisIterator getAllElements(int fingerprint) {
199 if (elementList == null) {
200 elementList = new IntHashMap<List<NodeInfo>>(20);
201 }
202 List<NodeInfo> list = elementList.get(fingerprint);
203 if (list == null) {
204 list = makeElementList(fingerprint);
205 elementList.put(fingerprint, list);
206 }
207 return new net.sf.saxon.tree.iter.ListIterator.OfNodes(list);
208 }
209
210 /**
211 * Make a list containing all the elements with a given element name, in document order
212 *
213 * @param fingerprint the fingerprint of the element name
214 * @return list a List containing the TinyElementImpl objects
215 */
216
217 List<NodeInfo> makeElementList(int fingerprint) {
218 int size = tree.getNumberOfNodes() / 20;
219 if (size > 100) {
220 size = 100;
221 }
222 if (size < 20) {
223 size = 20;
224 }
225 ArrayList<NodeInfo> list = new ArrayList<>(size);
226 int i = nodeNr + 1;
227 try {
228 while (tree.depth[i] != 0) {
229 byte kind = tree.nodeKind[i];
230 if ((kind & 0x0f) == Type.ELEMENT &&
231 (tree.nameCode[i] & 0xfffff) == fingerprint) {
232 list.add(tree.getNode(i));
233 } else if (kind == Type.EXTERNAL_NODE_REFERENCE) {
234 AxisIterator externals = tree.externalNodes.get(tree.alpha[i])
235 .iterateAxis(AxisInfo.DESCENDANT, new NameTest(Type.ELEMENT, fingerprint, getNamePool()));
236 NodeInfo ext;
237 while ((ext = externals.next()) != null) {
238 list.add(ext);
239 }
240 }
241 i++;
242 }
243 } catch (ArrayIndexOutOfBoundsException e) {
244 // this shouldn't happen. If it does happen, it means the tree wasn't properly closed
245 // during construction (there is no stopper node at the end). In this case, we'll recover
246 return list;
247 }
248 list.trimToSize();
249 return list;
250 }
251
252
253 /**
254 * Get the type annotation of this node, if any. The type annotation is represented as
255 * SchemaType object.
256 * <p>Types derived from a DTD are not reflected in the result of this method.</p>
257 *
258 * @return For element and attribute nodes: the type annotation derived from schema
259 * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema
260 * validation). For comments, text nodes, processing instructions, and namespaces: null.
261 * For document nodes, either xs:untyped if the document has not been validated, or
262 * xs:anyType if it has.
263 * @since 9.4
264 */
265 public SchemaType getSchemaType() {
266 AxisIterator children = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
267 NodeInfo node = children.next();
268 if (node == null || node.getSchemaType() == Untyped.getInstance()) {
269 return Untyped.getInstance();
270 } else {
271 return AnyType.getInstance();
272 }
273 }
274
275 /**
276 * Copy this node to a given outputter
277 */
278
279 public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId) throws XPathException {
280
281 out.startDocument(CopyOptions.getStartDocumentProperties(copyOptions));
282
283 // copy any unparsed entities
284
285 if (tree.entityTable != null) {
286 for (Map.Entry<String, String[]> entry : tree.entityTable.entrySet()) {
287 String name = entry.getKey();
288 String[] details = entry.getValue();
289 String systemId = details[0];
290 String publicId = details[1];
291 out.setUnparsedEntity(name, systemId, publicId);
292 }
293 }
294
295 // output the children
296
297 iterateAxis(AxisInfo.CHILD).forEachOrFail(n -> n.copy(out, copyOptions, locationId));
298
299 out.endDocument();
300 }
301
302 public void showSize() {
303 tree.showSize();
304 }
305
306 /**
307 * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal
308 * (represent the same node) then they must have the same hashCode()
309 *
310 * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers
311 * should therefore be aware that third party implementations of the NodeInfo interface may
312 * not implement the correct semantics.
313 */
314
315 public int hashCode() {
316 // Chosen to give a hashcode that is likely (a) to be distinct from other documents, and (b) to
317 // be distinct from other nodes in the same document
318 return (int) tree.getDocumentNumber();
319 }
320
321 /**
322 * Diagnostic method
323 * @return true if the tree contains grafted nodes
324 */
325 public boolean containsGraftedNodes() {
326 return tree.externalNodes != null && !tree.externalNodes.isEmpty();
327 }
328
329
330 }
331