Debian Codegraph @

net/sf/saxon/tree/wrapper/VirtualUntypedCopy.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.wrapper;
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.CopyOptions;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.BuiltInAtomicType;
18 import net.sf.saxon.type.SchemaType;
19 import net.sf.saxon.type.Type;
20 import net.sf.saxon.type.Untyped;
21 import net.sf.saxon.value.UntypedAtomicValue;
22
23 /**
24 * This class represents a virtual copy of a node with type annotations stripped
25 */
26 public class VirtualUntypedCopy extends VirtualCopy {
27
28 /**
29 * Public factory method: create a new untyped virtual tree as a copy of a node
30 *
31 * @param original the node (in the original tree) to be copied
32 * @param root the node in the original tree corresponding to the root node of the virtual copy
33 * @return the virtual copy.
34 */
35
36 public static VirtualCopy makeVirtualUntypedTree(NodeInfo original, NodeInfo root) {
37 VirtualCopy vc;
38 // Don't allow copies of copies of copies: define the new copy in terms of the original
39 while (original instanceof VirtualUntypedCopy && original.getParent() == null) {
40 original = ((VirtualUntypedCopy) original).original;
41 root = ((VirtualUntypedCopy) root).original;
42 }
43
44 vc = new VirtualUntypedCopy(original, root);
45
46 Configuration config = original.getConfiguration();
47 VirtualTreeInfo doc = new VirtualTreeInfo(config, vc);
48 vc.tree = doc;
49
50 return vc;
51 }
52
53 /**
54 * Protected constructor: create a virtual copy of a node
55 *
56 * @param base the node to be copied
57 */
58
59 protected VirtualUntypedCopy(NodeInfo base, NodeInfo root) {
60 super(base, root);
61 }
62
63 /**
64 * Get the type annotation of this node, if any. The type annotation is represented as
65 * SchemaType object.
66 * <p>Types derived from a DTD are not reflected in the result of this method.</p>
67 *
68 * @return For element and attribute nodes: the type annotation derived from schema
69 * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema
70 * validation). For comments, text nodes, processing instructions, and namespaces: null.
71 * For document nodes, either xs:untyped if the document has not been validated, or
72 * xs:anyType if it has.
73 * @since 9.4
74 */
75 @Override
76 public SchemaType getSchemaType() {
77 switch (getNodeKind()) {
78 case Type.ELEMENT:
79 return Untyped.getInstance();
80 case Type.ATTRIBUTE:
81 return BuiltInAtomicType.UNTYPED_ATOMIC;
82 default:
83 return super.getSchemaType();
84 }
85 }
86
87 /**
88 * Get the typed value.
89 *
90 * @return the typed value.
91 * @since 8.5
92 */
93
94 public AtomicSequence atomize() throws XPathException {
95 switch (getNodeKind()) {
96 case Type.ELEMENT:
97 case Type.ATTRIBUTE:
98 return new UntypedAtomicValue(getStringValueCS());
99 default:
100 return super.atomize();
101 }
102 }
103
104
105 public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException {
106 super.copy(out, copyOptions & ~CopyOptions.TYPE_ANNOTATIONS, locationId);
107 }
108
109 /**
110 * Method to create the virtual copy of a node encountered when navigating. This method
111 * is separated out so that it can be overridden in a subclass.
112 */
113
114 protected VirtualCopy wrap(NodeInfo node) {
115 VirtualUntypedCopy vc = new VirtualUntypedCopy(node, root);
116 vc.tree = tree;
117 return vc;
118 }
119
120 /**
121 * Determine whether the node has the is-nilled property
122 *
123 * @return true if the node has the is-nilled property
124 */
125
126 public boolean isNilled() {
127 return false;
128 }
129 }
130