| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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( 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 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
public void endElement() throws XPathException { |
| 58 |
nextReceiver.endElement(); |
| 59 |
siblingCounters.pop(); |
| 60 |
path.pop(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|