public PathTrackingReader(HierarchicalStreamReader reader, PathTracker pathTracker) { super(reader); this.pathTracker = pathTracker; pathTracker.pushElement(getNodeName()); }
/** * Get the last path element from the stack. * * @return the name of the path element * @since 1.4.2 */ public String peekElement() { return peekElement(0); }
protected String createReference(Path currentPath, Object existingReferenceKey) { Path existingPath = (Path)existingReferenceKey; Path referencePath = (mode & ReferenceByXPathMarshallingStrategy.ABSOLUTE) > 0 ? existingPath : currentPath.relativeTo(existingPath); return (mode & ReferenceByXPathMarshallingStrategy.SINGLE_NODE) > 0 ? referencePath.explicit() : referencePath.toString(); }
protected Object getReferenceKey(String reference) { final Path path = new Path(isNameEncoding ? ((AbstractReader)reader.underlyingReader()).decodeNode(reference) : reference); // We have absolute references, if path starts with '/' return reference.charAt(0) != '/' ? pathTracker.getPath().apply(path) : path; }
public Path relativeTo(Path that) { int depthOfPathDivergence = depthOfPathDivergence(chunks, that.chunks); String[] result = new String[chunks.length + that.chunks.length - 2 * depthOfPathDivergence]; int count = 0; for (int i = depthOfPathDivergence; i < chunks.length; i++) { result[count++] = ".."; } for (int j = depthOfPathDivergence; j < that.chunks.length; j++) { result[count++] = that.chunks[j]; } if (count == 0) { return DOT; } else { return new Path(result); } }
/** * Current Path in stream. */ public Path getPath() { if (currentPath == null) { String[] chunks = new String[pointer + 1]; chunks[0] = ""; for (int i = -pointer; ++i <= 0; ) { final String name = peekElement(i); chunks[i + pointer] = name; } currentPath = new Path(chunks); } return currentPath; } }
/** * @deprecated As of 1.4.2 */ public Path currentPath() { return pathTracker.getPath(); }
public void endNode() { super.endNode(); pathTracker.popElement(); } }
public void startNode(String name, Class clazz) { pathTracker.pushElement(isNameEncoding ? ((AbstractWriter)wrapped.underlyingWriter()).encodeNode(name) : name); super.startNode(name, clazz); }
public AbstractReferenceMarshaller(HierarchicalStreamWriter writer, ConverterLookup converterLookup, Mapper mapper) { super(writer, converterLookup, mapper); this.writer = new PathTrackingWriter(writer, pathTracker); }
public ReferencedImplicitElementException(final Object item, final Path path) { super("Cannot reference implicit element"); add("implicit-element", item.toString()); add("referencing-element", path.toString()); } }
public Path apply(Path relativePath) { FastStack absoluteStack = new FastStack(16); for (int i = 0; i < chunks.length; i++) { absoluteStack.push(chunks[i]); } for (int i = 0; i < relativePath.chunks.length; i++) { String relativeChunk = relativePath.chunks[i]; if (relativeChunk.equals("..")) { absoluteStack.pop(); } else if (!relativeChunk.equals(".")) { absoluteStack.push(relativeChunk); } } String[] result = new String[absoluteStack.size()]; for (int i = 0; i < result.length; i++) { result[i] = (String) absoluteStack.get(i); } return new Path(result); }
public ReferenceByXPathUnmarshaller(Object root, HierarchicalStreamReader reader, ConverterLookup converterLookup, Mapper mapper) { super(root, reader, converterLookup, mapper); this.reader = new PathTrackingReader(reader, pathTracker); isNameEncoding = reader.underlyingReader() instanceof AbstractReader; }
/** * Notify the tracker that the stream has moved into a new element. * * @param name Name of the element */ public void pushElement(String name) { if (pointer + 1 >= capacity) { resizeStacks(capacity * 2); } pathStack[pointer] = name; Map indexMap = indexMapStack[pointer]; if (indexMap == null) { indexMap = new HashMap(); indexMapStack[pointer] = indexMap; } if (indexMap.containsKey(name)) { indexMap.put(name, new Integer(((Integer) indexMap.get(name)).intValue() + 1)); } else { indexMap.put(name, new Integer(1)); } pointer++; currentPath = null; }
public Path(String pathAsString) { // String.split() too slow. StringTokenizer too crappy. List result = new ArrayList(); int currentIndex = 0; int nextSeparator; this.pathAsString = pathAsString; while ((nextSeparator = pathAsString.indexOf('/', currentIndex)) != -1) { // normalize explicit paths result.add(normalize(pathAsString, currentIndex, nextSeparator)); currentIndex = nextSeparator + 1; } result.add(normalize(pathAsString,currentIndex,pathAsString.length())); String[] arr = new String[result.size()]; result.toArray(arr); chunks = arr; }
public void moveDown() { super.moveDown(); pathTracker.pushElement(getNodeName()); }
protected Object getCurrentReferenceKey() { return pathTracker.getPath(); }
public void moveUp() { super.moveUp(); pathTracker.popElement(); }
public void startNode(String name) { pathTracker.pushElement(isNameEncoding ? ((AbstractWriter)wrapped.underlyingWriter()).encodeNode(name) : name); super.startNode(name); }