Package com.thoughtworks.xstream.io.path
Class PathTracker
- java.lang.Object
-
- com.thoughtworks.xstream.io.path.PathTracker
-
public class PathTracker extends java.lang.Object
Maintains the currentPath
as a stream is moved through.Can be linked to a
HierarchicalStreamWriter
orHierarchicalStreamReader
by wrapping them with aPathTrackingWriter
orPathTrackingReader
.Example
PathTracker tracker = new PathTracker(); tracker.pushElement("table"); tracker.pushElement("tr"); tracker.pushElement("td"); tracker.pushElement("form"); tracker.popElement("form"); tracker.popElement("td"); tracker.pushElement("td"); tracker.pushElement("div"); Path path = tracker.getPath(); // returns "/table/tr/td[2]/div"
- See Also:
Path
,PathTrackingReader
,PathTrackingWriter
-
-
Field Summary
Fields Modifier and Type Field Description private int
capacity
private Path
currentPath
private java.util.Map[]
indexMapStack
private java.lang.String[]
pathStack
private int
pointer
-
Constructor Summary
Constructors Constructor Description PathTracker()
PathTracker(int initialCapacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
depth()
Get the depth of the stack.Path
getPath()
Current Path in stream.java.lang.String
peekElement()
Get the last path element from the stack.java.lang.String
peekElement(int i)
Get a path element from the stack.void
popElement()
Notify the tracker that the stream has moved out of an element.void
pushElement(java.lang.String name)
Notify the tracker that the stream has moved into a new element.private void
resizeStacks(int newCapacity)
-
-
-
Field Detail
-
pointer
private int pointer
-
capacity
private int capacity
-
pathStack
private java.lang.String[] pathStack
-
indexMapStack
private java.util.Map[] indexMapStack
-
currentPath
private Path currentPath
-
-
Constructor Detail
-
PathTracker
public PathTracker()
-
PathTracker
public PathTracker(int initialCapacity)
- Parameters:
initialCapacity
- Size of the initial stack of nodes (one level per depth in the tree). Note that this is only for optimizations - the stack will resize itself if it exceeds its capacity. If in doubt, use the other constructor.
-
-
Method Detail
-
pushElement
public void pushElement(java.lang.String name)
Notify the tracker that the stream has moved into a new element.- Parameters:
name
- Name of the element
-
popElement
public void popElement()
Notify the tracker that the stream has moved out of an element.
-
peekElement
public java.lang.String peekElement()
Get the last path element from the stack.- Returns:
- the name of the path element
- Since:
- 1.4.2
-
peekElement
public java.lang.String peekElement(int i)
Get a path element from the stack.- Parameters:
i
- path index- Returns:
- the name of the path element
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the index is >= 0 or <= -depth()- Since:
- 1.4.2
-
depth
public int depth()
Get the depth of the stack.- Returns:
- the stack depth
- Since:
- 1.4.2
-
resizeStacks
private void resizeStacks(int newCapacity)
-
getPath
public Path getPath()
Current Path in stream.
-
-