Debian Codegraph @

net/sf/saxon/om/AbsolutePath.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.om;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11 import net.sf.saxon.trans.Err;
12 import net.sf.saxon.tree.util.FastStringBuffer;
13 import net.sf.saxon.tree.util.Navigator;
14 import net.sf.saxon.type.Type;
15
16 import java.util.ArrayList;
17 import java.util.LinkedList;
18 import java.util.List;
19
20 /**
21 * Represents the path from the root of an XDM tree to a specific node, as a sequence of (name, position) pairs
22 */
23
24 public class AbsolutePath {
25
26 private List<PathElement> path;
27 private String systemId;
28
29
30 /**
31 * Create an absolute path given a list of path elements
32 *
33 * @param path the list of path elements, starting from the root. It is not necessary to include a path element
34 * for the document node.
35 */
36
37 public AbsolutePath(List<PathElement> path) {
38 this.path = new ArrayList<>(path);
39 }
40
41 /**
42 * Append an attribute name to the path
43 * @param attributeName the name of the attribute
44 */
45
46 public void appendAttributeName(NodeName attributeName) {
47 PathElement last = path.get(path.size()-1);
48 if (last.getNodeKind() == Type.ATTRIBUTE) {
49 path.remove(path.size()-1);
50 }
51 PathElement att = new PathElement(Type.ATTRIBUTE, attributeName, 1);
52 path.add(att);
53 }
54
55 /**
56 * Create an absolute path given a Node
57 * @param node the node whose path is required
58 */
59
60 public static AbsolutePath pathToNode(NodeInfo node) {
61 LinkedList<PathElement> list = new LinkedList<>();
62 while (node != null && node.getNodeKind() != Type.DOCUMENT) {
63 PathElement pe = new PathElement(node.getNodeKind(), NameOfNode.makeName(node), Navigator.getNumberSimple(node, null));
64 list.addFirst(pe);
65 node = node.getParent();
66 }
67 return new AbsolutePath(list);
68 }
69
70 /**
71 * Get a string representing the path using namespace prefixes to represent QNames
72 *
73 * @return the path in the form <code>/prefix:local[n]/prefix:local[m]/...</code>
74 */
75
76 public String getPathUsingPrefixes() {
77 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
78 for (AbsolutePath.PathElement pe : path) {
79 fsb.append('/');
80 pe.toString(fsb, 'p');
81 }
82 return fsb.toString();
83 }
84
85 /**
86 * Get a string representing the path using namespace URIs to represent QNames
87 *
88 * @return the path in the form <code>/Q{uri}local[n]/Q{uri}local[m]/...</code>
89 */
90
91 public String getPathUsingUris() {
92 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
93 for (AbsolutePath.PathElement pe : path) {
94 fsb.append('/');
95 pe.toString(fsb, 'u');
96 }
97 return fsb.toString();
98 }
99
100 /**
101 * Get a string representing the path using abbreviated namespace URIs to represent QNames
102 *
103 * @return the path in the form <code>/Q{uri}local[n]/Q{uri}local[m]/...</code>, with the URIs shortened
104 */
105
106 public String getPathUsingAbbreviatedUris() {
107 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
108 for (AbsolutePath.PathElement pe : path) {
109 fsb.append('/');
110 pe.toString(fsb, 's');
111 }
112 return fsb.toString();
113 }
114
115 @Override
116 public String toString() {
117 return getPathUsingUris();
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 return obj instanceof AbsolutePath && obj.toString().equals(toString());
123 }
124
125 @Override
126 public int hashCode() {
127 return toString().hashCode();
128 }
129
130 public void setSystemId(String systemId) {
131 this.systemId = systemId;
132 }
133
134 public String getSystemId() {
135 return systemId;
136 }
137
138
139 /**
140 * Inner class representing one step in the path
141 */
142
143 public static class PathElement {
144 int nodeKind;
145 NodeName name;
146 int index;
147
148 /**
149 * Create a path element
150 *
151 * @param nodeKind the kind of node
152 * @param name the name of the node
153 * @param index the position of the node relative to siblings of the same node kind and name.
154 * The value -1 indicates "not known", which will typically be the case for streamed nodes.
155 */
156 public PathElement(int nodeKind, NodeName name, int index) {
157 this.nodeKind = nodeKind;
158 this.name = name;
159 this.index = index;
160 }
161
162 /**
163 * Get the node kind
164 *
165 * @return the node kind, as a constant from {@link net.sf.saxon.type.Type}
166 */
167 public int getNodeKind() {
168 return nodeKind;
169 }
170
171 /**
172 * Get the name of the node
173 *
174 * @return the node name
175 */
176
177 public NodeName getName() {
178 return name;
179 }
180
181 /**
182 * Get the position of the node
183 *
184 * @return the position relative to siblings of the same node kind and name.
185 * The value -1 indicates "not known", which will typically be the case for streamed nodes.
186 */
187
188 public int getIndex() {
189 return index;
190 }
191
192 /**
193 * Get a string representation of the path
194 *
195 * @param fsb buffer into which the string representation will be written
196 * @param option for representing namespaces:
197 * 'p': use namepace prefix. 'u': use full URI. 's': use abbreviated URI
198 */
199
200 public void toString(FastStringBuffer fsb, char option) {
201 switch (nodeKind) {
202 case Type.DOCUMENT:
203 fsb.append("(/)");
204 break;
205 case Type.ATTRIBUTE:
206 fsb.append('@');
207 if (!name.getURI().isEmpty()) {
208 if (option == 'u') {
209 fsb.append("Q{");
210 fsb.append(name.getURI());
211 fsb.append("}");
212 } else if (option == 'p') {
213 String prefix = name.getPrefix();
214 if (!prefix.isEmpty()) {
215 fsb.append(prefix);
216 fsb.append(':');
217 }
218 } else if (option == 's') {
219 fsb.append("Q{");
220 fsb.append(Err.abbreviateURI(name.getURI()));
221 fsb.append("}");
222 }
223 }
224 fsb.append(getName().getLocalPart());
225 break;
226 case Type.ELEMENT:
227 if (option == 'u') {
228 fsb.append("Q{");
229 fsb.append(name.getURI());
230 fsb.append("}");
231 } else if (option == 'p') {
232 String prefix = name.getPrefix();
233 if (!prefix.isEmpty()) {
234 fsb.append(prefix);
235 fsb.append(':');
236 }
237 } else if (option == 's') {
238 if (!name.getURI().isEmpty()) {
239 fsb.append("Q{");
240 fsb.append(Err.abbreviateURI(name.getURI()));
241 fsb.append("}");
242 }
243 }
244 fsb.append(name.getLocalPart());
245 appendPredicate(fsb);
246 break;
247 case Type.TEXT:
248 fsb.append("text()");
249 break;
250 case Type.COMMENT:
251 fsb.append("comment()");
252 appendPredicate(fsb);
253 break;
254 case Type.PROCESSING_INSTRUCTION:
255 fsb.append("processing-instruction(");
256 fsb.append(name.getLocalPart());
257 fsb.append(")");
258 appendPredicate(fsb);
259 break;
260 case Type.NAMESPACE:
261 fsb.append("namespace::");
262 if (name.getLocalPart().isEmpty()) {
263 fsb.append("*[Q{" + NamespaceConstant.FN + "}local-name()=\"\"]");
264 } else {
265 fsb.append(name.getLocalPart());
266 }
267 break;
268 default:
269 }
270 }
271
272 private void appendPredicate(FastStringBuffer fsb) {
273 int index = getIndex();
274 if (index != -1) {
275 fsb.append('[');
276 fsb.append(getIndex() + "");
277 fsb.append(']');
278 }
279 }
280 }
281
282
283
284
285 }
286