Debian Codegraph @

net/sf/saxon/event/PathMaintainer.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.event;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.om.AbsolutePath;
12 import net.sf.saxon.om.NodeName;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.type.SchemaType;
16 import net.sf.saxon.type.Type;
17
18 import java.util.HashMap;
19 import java.util.Stack;
20
21
22 /**
23 * This class sits in a receiver (push) pipeline and maintains the current path.
24 */
25
26
27 public class PathMaintainer extends ProxyReceiver {
28
29 private Stack<AbsolutePath.PathElement> path = new Stack<>();
30 private Stack<HashMap<NodeName, Integer>> siblingCounters = new Stack<>();
31
32 public PathMaintainer(/*@NotNull*/ Receiver next) {
33 super(next);
34 siblingCounters.push(new HashMap<>());
35 }
36
37 public void startElement(NodeName elemName, SchemaType type, Location location, int properties) throws XPathException {
38 // System.err.println("startElement " + nameCode);
39 nextReceiver.startElement(elemName, type, location, properties);
40 HashMap<NodeName, Integer> counters = siblingCounters.peek();
41 int index = 1;
42 Integer preceding = counters.get(elemName);
43 if (preceding != null) {
44 index = preceding + 1;
45 counters.put(elemName, index);
46 } else {
47 counters.put(elemName, 1);
48 }
49 path.push(new AbsolutePath.PathElement(Type.ELEMENT, elemName, index));
50 siblingCounters.push(new HashMap<>());
51 }
52
53 /**
54 * Handle an end-of-element event
55 */
56
57 public void endElement() throws XPathException {
58 nextReceiver.endElement();
59 siblingCounters.pop();
60 path.pop();
61 }
62
63 /**
64 * Get the path to the current location in the stream
65 *
66 * @param useURIs set to true if namespace URIs are to appear in the path;
67 * false if prefixes are to be used instead. The prefix will be the one
68 * that is used in the source document, and is potentially ambiguous.
69 * @return the path to the current location, as a string.
70 */
71
72 public String getPath(boolean useURIs) {
73 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
74 for (AbsolutePath.PathElement pe : path) {
75 fsb.append('/');
76 if (useURIs) {
77 String uri = pe.getName().getURI();
78 if (!uri.isEmpty()) {
79 fsb.append('"');
80 fsb.append(uri);
81 fsb.append('"');
82 }
83 } else {
84 String prefix = pe.getName().getPrefix();
85 if (!prefix.isEmpty()) {
86 fsb.append(prefix);
87 fsb.append(':');
88 }
89 }
90 fsb.append(pe.getName().getLocalPart());
91 fsb.append('[');
92 fsb.append(pe.getIndex() + "");
93 fsb.append(']');
94 }
95 return fsb.toString();
96 }
97
98 public AbsolutePath getAbsolutePath() {
99 return new AbsolutePath(path);
100 }
101
102
103 }
104