Debian Codegraph @

net/sf/saxon/tree/tiny/TinyTextualElement.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.CopyInformee;
12 import net.sf.saxon.event.CopyNamespaceSensitiveException;
13 import net.sf.saxon.event.Receiver;
14 import net.sf.saxon.expr.parser.Location;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.pattern.NodeTest;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.*;
19 import net.sf.saxon.tree.util.FastStringBuffer;
20 import net.sf.saxon.tree.util.NamespaceIterator;
21 import net.sf.saxon.tree.util.Navigator;
22 import net.sf.saxon.type.SchemaType;
23 import net.sf.saxon.type.Type;
24 import net.sf.saxon.type.Untyped;
25 import net.sf.saxon.value.UntypedAtomicValue;
26 import net.sf.saxon.z.IntValuePredicate;
27
28 import javax.xml.transform.SourceLocator;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.function.IntPredicate;
32
33 /**
34 * An element node in the TinyTree that has no attributes or namespace declarations and that
35 * has a single text node child. The element-and-text-node pair are represented by a single
36 * entry in the node arrays, but materialize as two separate objects when turned into node
37 * objects.
38 */
39
40 public class TinyTextualElement extends TinyElementImpl {
41
42 private TinyTextualElementText textNode = null;
43
44 public TinyTextualElement(TinyTree tree, int nodeNr) {
45 super(tree, nodeNr);
46 }
47
48 @Override
49 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
50 return NamespaceBinding.EMPTY_ARRAY;
51 }
52
53 @Override
54 public String getAttributeValue(String uri, String local) {
55 return null;
56 }
57
58 @Override
59 public String getAttributeValue(int fp) {
60 return null;
61 }
62
63 @Override
64 public void copy(Receiver receiver, int copyOptions, Location location) throws XPathException {
65 boolean typed = CopyOptions.includes(copyOptions, CopyOptions.TYPE_ANNOTATIONS);
66 SchemaType type = typed ? getSchemaType() : Untyped.getInstance();
67 boolean disallowNamespaceSensitiveContent =
68 ((copyOptions & CopyOptions.TYPE_ANNOTATIONS) != 0) &&
69 ((copyOptions & CopyOptions.SOME_NAMESPACES) == 0);
70 if (disallowNamespaceSensitiveContent) {
71 try {
72 checkNotNamespaceSensitiveElement(type, nodeNr);
73 } catch (CopyNamespaceSensitiveException e) {
74 int lang = receiver.getPipelineConfiguration().getHostLanguage();
75 e.setErrorCode(lang == Configuration.XSLT ? "XTTE0950" : "XQTY0086");
76 throw e;
77 }
78 }
79
80 CopyInformee informee = (CopyInformee) receiver.getPipelineConfiguration().getComponent(CopyInformee.class.getName());
81 if (informee != null) {
82 Object o = informee.notifyElementNode(this);
83 if (o instanceof Location) {
84 location = (Location) o;
85 }
86 }
87
88 receiver.startElement(NameOfNode.makeName(this), type, location, 0);
89 if ((copyOptions & CopyOptions.ALL_NAMESPACES) != 0) {
90 // Don't bother with LOCAL_NAMESPACES because there aren't any
91 NamespaceIterator.sendNamespaces(this, receiver);
92 }
93 receiver.startContent();
94 receiver.characters(getStringValueCS(), location, 0);
95 receiver.endElement();
96 }
97
98 @Override
99 public boolean hasChildNodes() {
100 return true;
101 }
102
103 @Override
104 public CharSequence getStringValueCS() {
105 return TinyTextImpl.getStringValue(tree, nodeNr);
106 }
107
108 @Override
109 public String getStringValue() {
110 return TinyTextImpl.getStringValue(tree, nodeNr).toString();
111 }
112
113 @Override
114 public AxisIterator iterateAxis(byte axisNumber) {
115 switch (axisNumber) {
116 case AxisInfo.ATTRIBUTE:
117 return EmptyIterator.OfNodes.THE_INSTANCE;
118
119 case AxisInfo.CHILD:
120 case AxisInfo.DESCENDANT:
121 return SingleNodeIterator.makeIterator(getTextNode());
122
123 case AxisInfo.DESCENDANT_OR_SELF:
124 List<NodeInfo> list = new ArrayList<>(2);
125 list.add(this);
126 list.add(getTextNode());
127 return new ListIterator.OfNodes(list);
128
129 default:
130 return super.iterateAxis(axisNumber);
131 }
132 }
133
134 @Override
135 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
136 switch (axisNumber) {
137 case AxisInfo.ATTRIBUTE:
138 return EmptyIterator.OfNodes.THE_INSTANCE;
139
140 case AxisInfo.CHILD:
141 case AxisInfo.DESCENDANT:
142 return Navigator.filteredSingleton(getTextNode(), nodeTest);
143
144 case AxisInfo.DESCENDANT_OR_SELF:
145 List<NodeInfo> list = new ArrayList<>(2);
146 if (nodeTest.matchesNode(this)) {
147 list.add(this);
148 }
149 if (nodeTest.matchesNode(getTextNode())) {
150 list.add(getTextNode());
151 }
152 return new ListIterator.OfNodes(list);
153
154 default:
155 return super.iterateAxis(axisNumber, nodeTest);
156 }
157 }
158
159 @Override
160 public boolean isAncestorOrSelf(TinyNodeImpl d) {
161 return this.equals(d);
162 }
163
164 /**
165 * Make an instance of the text node
166 *
167 * @return the new or existing instance
168 */
169
170 /*@Nullable*/
171 public TinyTextualElementText getTextNode() {
172 if (textNode == null) {
173 textNode = new TinyTextualElementText();
174 }
175 return textNode;
176 }
177
178 /**
179 * Inner class representing the text node; this is created on demand
180 */
181
182 public class TinyTextualElementText implements NodeInfo, SourceLocator {
183
184 /**
185 * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the
186 * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must
187 * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint}
188 * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes
189 * such as text nodes, the result can be either true (in which case getFingerprint() should
190 * return -1) or false (in which case getFingerprint may throw an exception).
191 *
192 * @return true if the implementation of this node provides fingerprints.
193 * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface.
194 */
195 @Override
196 public boolean hasFingerprint() {
197 return true;
198 }
199
200 /**
201 * Get information about the tree to which this NodeInfo belongs
202 *
203 * @return the TreeInfo
204 * @since 9.7
205 */
206 public TreeInfo getTreeInfo() {
207 return TinyTextualElement.this.getTreeInfo();
208 }
209
210 /**
211 * Set the system ID for the entity containing the node.
212 */
213
214 public void setSystemId(String systemId) {
215 }
216
217 /**
218 * Return the type of node.
219 *
220 * @return Type.TEXT (always)
221 */
222
223 public final int getNodeKind() {
224 return Type.TEXT;
225 }
226
227 /**
228 * Get the String Value
229 */
230
231 public String getStringValue() {
232 return getStringValueCS().toString();
233 }
234
235 /**
236 * Get the value of the item as a CharSequence. This is in some cases more efficient than
237 * the version of the method that returns a String.
238 */
239
240 public CharSequence getStringValueCS() {
241 return TinyTextualElement.this.getStringValueCS();
242 }
243
244 /**
245 * Determine whether this is the same node as another node
246 *
247 * @return true if this Node object and the supplied Node object represent the
248 * same node in the tree.
249 */
250
251 public boolean equals(Object other) {
252 return other instanceof TinyTextualElementText &&
253 getParent().equals(((TinyTextualElementText)other).getParent());
254 }
255
256 /**
257 * Get a character string that uniquely identifies this node
258 */
259
260 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
261 TinyTextualElement.this.generateId(buffer);
262 buffer.append("T");
263 }
264
265 /**
266 * Get the system ID for the entity containing the node.
267 */
268
269 /*@Nullable*/
270 public String getSystemId() {
271 return TinyTextualElement.this.getSystemId();
272 }
273
274 /**
275 * Get the base URI for the node. Default implementation for child nodes gets
276 * the base URI of the parent node.
277 */
278
279 public String getBaseURI() {
280 return TinyTextualElement.this.getBaseURI();
281 }
282
283 /**
284 * Determine the relative position of this node and another node, in document order.
285 * The other node will always be in the same document.
286 *
287 * @param other The other node, whose position is to be compared with this node
288 * @return -1 if this node precedes the other node, +1 if it follows the other
289 * node, or 0 if they are the same node. (In this case, isSameNode() will always
290 * return true, and the two nodes will produce the same result for generateId())
291 */
292
293 public int compareOrder(NodeInfo other) {
294 if (other.equals(this)) {
295 return 0;
296 } else if (other.equals(getParent())) {
297 return 1;
298 } else {
299 return getParent().compareOrder(other);
300 }
301 }
302
303 /**
304 * Get the fingerprint of the node, used for matching names
305 */
306
307 public int getFingerprint() {
308 return -1;
309 }
310
311
312 /**
313 * Get the prefix part of the name of this node. This is the name before the ":" if any.
314 *
315 * @return the prefix part of the name. For an unnamed node, return "".
316 */
317
318 /*@NotNull*/
319 public String getPrefix() {
320 return "";
321 }
322
323 /**
324 * Get the URI part of the name of this node. This is the URI corresponding to the
325 * prefix, or the URI of the default namespace if appropriate.
326 *
327 * @return The URI of the namespace of this node. For an unnamed node, or for
328 * an element or attribute in the default namespace, return an empty string.
329 */
330
331 /*@NotNull*/
332 public String getURI() {
333 return "";
334 }
335
336 /**
337 * Get the display name of this node. For elements and attributes this is [prefix:]localname.
338 * For unnamed nodes, it is an empty string.
339 *
340 * @return The display name of this node.
341 * For a node with no name, return an empty string.
342 */
343
344 /*@NotNull*/
345 public String getDisplayName() {
346 return "";
347 }
348
349 /**
350 * Get the local name of this node.
351 *
352 * @return The local name of this node.
353 * For a node with no name, return "".
354 */
355
356 /*@NotNull*/
357 public String getLocalPart() {
358 return "";
359 }
360
361 /**
362 * Determine whether the node has any children.
363 *
364 * @return <code>true</code> if this node has any attributes,
365 * <code>false</code> otherwise.
366 */
367
368 public boolean hasChildNodes() {
369 return false;
370 }
371
372 /**
373 * Get the string value of a given attribute of this node
374 *
375 * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute
376 * that is in no namespace
377 * @param local the local part of the attribute name.
378 * @return the attribute value if it exists, or null if it does not exist. Always returns null
379 * if this node is not an element.
380 * @since 9.4
381 */
382 public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) {
383 return null;
384 }
385
386 /**
387 * Get line number
388 *
389 * @return the line number of the node in its original source document; or
390 * -1 if not available
391 */
392
393 public int getLineNumber() {
394 return getParent().getLineNumber();
395 }
396
397 private IntPredicate isNewline = new IntValuePredicate(10);
398
399 /**
400 * Return the character position where the current document event ends.
401 * <p><strong>Warning:</strong> The return value from the method
402 * is intended only as an approximation for the sake of error
403 * reporting; it is not intended to provide sufficient information
404 * to edit the character content of the original XML document.</p>
405 * <p>The return value is an approximation of the column number
406 * in the document entity or external parsed entity where the
407 * markup that triggered the event appears.</p>
408 *
409 * @return The column number, or -1 if none is available.
410 * @see #getLineNumber
411 */
412 public int getColumnNumber() {
413 return getParent().getColumnNumber();
414 }
415
416
417 /**
418 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
419 * should not be saved for later use. The result of this operation holds the same location information,
420 * but in an immutable form.
421 */
422 public Location saveLocation() {
423 return this;
424 }
425
426 /**
427 * Get the type annotation of this node, if any. The type annotation is represented as
428 * SchemaType object.
429 * <p>Types derived from a DTD are not reflected in the result of this method.</p>
430 *
431 * @return For element and attribute nodes: the type annotation derived from schema
432 * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema
433 * validation). For comments, text nodes, processing instructions, and namespaces: null.
434 * For document nodes, either xs:untyped if the document has not been validated, or
435 * xs:anyType if it has.
436 * @since 9.4
437 */
438 public SchemaType getSchemaType() {
439 return null;
440 }
441
442 /**
443 * Get all namespace undeclarations and undeclarations defined on this element.
444 *
445 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
446 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
447 * @return An array of integers representing the namespace declarations and undeclarations present on
448 * this element. For a node other than an element, return null. Otherwise, the returned array is a
449 * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
450 * top half word of each namespace code represents the prefix, the bottom half represents the URI.
451 * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
452 * The XML namespace is never included in the list. If the supplied array is larger than required,
453 * then the first unused entry will be set to -1.
454 * <p>For a node other than an element, the method returns null.</p>
455 */
456
457 /*@Nullable*/
458 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
459 return null;
460 }
461
462 /**
463 * Get the typed value.
464 *
465 * @return the typed value. If requireSingleton is set to true, the result will always be an
466 * AtomicValue. In other cases it may be a Value representing a sequence whose items are atomic
467 * values.
468 * @since 8.5
469 */
470
471 /*@NotNull*/
472 public AtomicSequence atomize() throws XPathException {
473 return new UntypedAtomicValue(getStringValueCS());
474 }
475
476 /**
477 * Return an enumeration over the nodes reached by the given axis from this node
478 *
479 * @param axisNumber the axis to be iterated over
480 * @return a AxisIterator that scans the nodes reached by the axis in turn.
481 */
482
483 /*@NotNull*/
484 public AxisIterator iterateAxis(byte axisNumber) {
485 switch (axisNumber) {
486 case AxisInfo.ANCESTOR:
487 return TinyTextualElement.this.iterateAxis(AxisInfo.ANCESTOR_OR_SELF);
488
489 case AxisInfo.PRECEDING_OR_ANCESTOR:
490 return new Navigator.PrecedingEnumeration(this, true);
491
492 case AxisInfo.ANCESTOR_OR_SELF:
493 return new PrependAxisIterator(this, getParent().iterateAxis(AxisInfo.ANCESTOR_OR_SELF));
494
495 case AxisInfo.FOLLOWING:
496 return new Navigator.FollowingEnumeration(this);
497
498 case AxisInfo.PRECEDING:
499 return new Navigator.PrecedingEnumeration(this, false);
500
501 case AxisInfo.PARENT:
502 return SingleNodeIterator.makeIterator(getParent());
503
504 case AxisInfo.ATTRIBUTE:
505 case AxisInfo.CHILD:
506 case AxisInfo.DESCENDANT:
507 case AxisInfo.FOLLOWING_SIBLING:
508 case AxisInfo.NAMESPACE:
509 case AxisInfo.PRECEDING_SIBLING:
510 return EmptyIterator.OfNodes.THE_INSTANCE;
511
512 case AxisInfo.SELF:
513 case AxisInfo.DESCENDANT_OR_SELF:
514 return SingleNodeIterator.makeIterator(this);
515
516 default:
517 throw new IllegalArgumentException("Unknown axis number " + axisNumber);
518 }
519 }
520
521
522 /**
523 * Return an enumeration over the nodes reached by the given axis from this node
524 *
525 * @param axisNumber the axis to be iterated over
526 * @param nodeTest A pattern to be matched by the returned nodes
527 * @return a AxisIterator that scans the nodes reached by the axis in turn.
528 */
529
530 /*@NotNull*/
531 public AxisIterator iterateAxis(byte axisNumber, /*@NotNull*/ NodeTest nodeTest) {
532 switch (axisNumber) {
533 case AxisInfo.ANCESTOR:
534 return getParent().iterateAxis(AxisInfo.ANCESTOR_OR_SELF, nodeTest);
535
536 case AxisInfo.PRECEDING_OR_ANCESTOR:
537 return new Navigator.AxisFilter(
538 new Navigator.PrecedingEnumeration(this, true),
539 nodeTest);
540
541 case AxisInfo.ANCESTOR_OR_SELF:
542 return new Navigator.AxisFilter(
543 new PrependAxisIterator(this, getParent().iterateAxis(AxisInfo.ANCESTOR_OR_SELF)),
544 nodeTest);
545
546 case AxisInfo.FOLLOWING:
547 return new Navigator.AxisFilter(
548 new Navigator.FollowingEnumeration(this),
549 nodeTest);
550
551 case AxisInfo.PRECEDING:
552 return new Navigator.AxisFilter(
553 new Navigator.PrecedingEnumeration(this, false),
554 nodeTest);
555
556 case AxisInfo.PARENT:
557 return Navigator.filteredSingleton(getParent(), nodeTest);
558
559 case AxisInfo.ATTRIBUTE:
560 case AxisInfo.CHILD:
561 case AxisInfo.DESCENDANT:
562 case AxisInfo.FOLLOWING_SIBLING:
563 case AxisInfo.NAMESPACE:
564 case AxisInfo.PRECEDING_SIBLING:
565 return EmptyIterator.OfNodes.THE_INSTANCE;
566
567 case AxisInfo.SELF:
568 case AxisInfo.DESCENDANT_OR_SELF:
569 return Navigator.filteredSingleton(this, nodeTest);
570
571 default:
572 throw new IllegalArgumentException("Unknown axis number " + axisNumber);
573 }
574 }
575
576 /**
577 * Find the parent node of this node.
578 *
579 * @return The Node object describing the containing element or root node.
580 */
581
582 /*@NotNull*/
583 public NodeInfo getParent() {
584 return TinyTextualElement.this;
585 }
586
587 /**
588 * Get the root node
589 *
590 * @return the NodeInfo representing the root of this tree
591 */
592
593 /*@NotNull*/
594 public NodeInfo getRoot() {
595 return getParent().getRoot();
596 }
597
598 /**
599 * Copy the node to a given Outputter
600 */
601
602 public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId)
603 throws XPathException {
604 out.characters(getStringValueCS(), locationId, 0);
605 }
606
607 }
608
609
610 }
611