Class PathParser
java.lang.Object
org.simpleframework.xml.core.PathParser
- All Implemented Interfaces:
Iterable<String>, Expression
The
PathParser object is used to parse XPath paths.
This will parse a subset of the XPath expression syntax, such
that the path can be used to identify and navigate various XML
structures. Example paths that this can parse are as follows.
./example/path ./example[2]/path/ example/path example/path/@attribute ./path/@attributeIf the parsed path does not match an XPath expression similar to the above then an exception is thrown. Once parsed the segments of the path can be used to traverse data structures modelled on an XML document or fragment.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThePathSectionrepresents a section of a path that is extracted. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanThis is used to determine if the path is an attribute.This is used to cache the attributes created by this path.protected StringBuilderThis is used to build a fully qualified path expression.protected StringThis is the the cached canonical representation of the path.protected intThis represents the number of characters in the source path.protected char[]This is a copy of the source data that is to be parsed.This is used to cache the elements created by this path.This contains a list of the indexes for each path segment.protected StringThis is the fully qualified path expression for this.This contains a list of the path segments that were parsed.protected intThis is the current seek position for the parser.protected StringThis is a cache of the canonical path representation.This is used to store the path prefixes for the parsed path.protected intThis is the start offset that skips any root references.protected StyleThis is the format used to style the path segments.protected TypeThis is the type the expressions are to be parsed for. -
Constructor Summary
ConstructorsConstructorDescriptionPathParser(String path, Type type, Format format) Constructor for thePathParserobject. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidalign()This is used to add a default index to a segment or attribute extracted from the source expression.private voidThis is used to extract an attribute from the path expression.private voidattribute(int start, int count) This will add a path segment to the list of segments.private voidThis will insert the path segment provided.private voidbuild()This method is used to build a fully qualified path that has each segment index.private voidelement()This is used to extract an element from the path expression.private voidelement(int start, int count) This will add a path segment to the list of segments.private voidThis will insert the path segment provided.getAttribute(String name) This is used to acquire the attribute path using this XPath expression.protected StringgetAttributePath(String path, String name) This is used to acquire the attribute path using this XPath expression.getElement(String name) This is used to acquire the element path using this XPath expression.protected StringgetElementPath(String path, String name) This is used to acquire the element path using this XPath expression.getFirst()This can be used to acquire the first path segment within the expression.intgetIndex()If the first path segment contains an index it is provided by this method.getLast()This can be used to acquire the last path segment within the expression.getPath()This location contains the full path expression with all of the indexes explicitly shown for each path segment.getPath(int from) This allows an expression to be extracted from the current context.getPath(int from, int trim) This allows an expression to be extracted from the current context.This is used to extract a namespace prefix from the path expression.private voidindex()This is used to extract an index from an element.booleanThis is used to determine if the expression points to an attribute value.private booleanisDigit(char value) This is used to determine if the provided character is a digit.booleanisEmpty()This method is used to determine if this expression is an empty path.private booleanThis is used to determine if a string is empty.private booleanisLetter(char value) This is used to determine if the provided character is an alpha numeric character.booleanisPath()This is used to determine if the expression is a path.private booleanisSpecial(char value) This is used to determine if the provided character is a legal XML element character.private booleanisValid(char value) This is used to determine if the provided character is a legal XML element character.iterator()This is used to iterate over the path segments that have been extracted from the source XPath expression.private voidThis method is used to parse the provided XPath expression.private voidpath()This method is used to parse the provided XPath expression.private voidsegment()This method is used to extract a path segment from the source expression.private voidskip()This is used to skip any root prefix for the path.toString()Provides a canonical XPath expression.private voidtruncate()This method is used to trim any trailing characters at the end of the path.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
attributes
-
elements
-
indexes
-
prefixes
-
names
-
builder
This is used to build a fully qualified path expression. -
location
This is the fully qualified path expression for this. -
cache
This is the the cached canonical representation of the path. -
path
This is a cache of the canonical path representation. -
style
This is the format used to style the path segments. -
type
This is the type the expressions are to be parsed for. -
attribute
protected boolean attributeThis is used to determine if the path is an attribute. -
data
protected char[] dataThis is a copy of the source data that is to be parsed. -
count
protected int countThis represents the number of characters in the source path. -
start
protected int startThis is the start offset that skips any root references. -
off
protected int offThis is the current seek position for the parser.
-
-
Constructor Details
-
PathParser
Constructor for thePathParserobject. This must be given a valid XPath expression. Currently only a subset of the XPath syntax is supported by this parser. Once finished the parser will contain all the extracted path segments.- Parameters:
path- this is the XPath expression to be parsedtype- this is the type the expressions are parsed forformat- this is the format used to style the path- Throws:
Exception
-
-
Method Details
-
isEmpty
public boolean isEmpty()This method is used to determine if this expression is an empty path. An empty path can be represented by a single period, '.'. It identifies the current path.- Specified by:
isEmptyin interfaceExpression- Returns:
- returns true if this represents an empty path
-
isPath
public boolean isPath()This is used to determine if the expression is a path. An expression represents a path if it contains more than one segment. If only one segment exists it is an element name.- Specified by:
isPathin interfaceExpression- Returns:
- true if this contains more than one segment
-
isAttribute
public boolean isAttribute()This is used to determine if the expression points to an attribute value. An attribute value contains an '@' character before the last segment name. Such expressions distinguish element references from attribute references.- Specified by:
isAttributein interfaceExpression- Returns:
- this returns true if the path has an attribute
-
getIndex
public int getIndex()If the first path segment contains an index it is provided by this method. There may be several indexes within a path, however only the index at the first segment is issued by this method. If there is no index this will return 1.- Specified by:
getIndexin interfaceExpression- Returns:
- this returns the index of this path expression
-
getPrefix
This is used to extract a namespace prefix from the path expression. A prefix is used to qualify the XML element name and does not form part of the actual path structure. This can be used to add the namespace in addition to the name.- Specified by:
getPrefixin interfaceExpression- Returns:
- this returns the prefix for the path expression
-
getFirst
This can be used to acquire the first path segment within the expression. The first segment represents the parent XML element of the path. All segments returned do not contain any slashes and so represents the real element name.- Specified by:
getFirstin interfaceExpression- Returns:
- this returns the parent element for the path
-
getLast
This can be used to acquire the last path segment within the expression. The last segment represents the leaf XML element of the path. All segments returned do not contain any slashes and so represents the real element name.- Specified by:
getLastin interfaceExpression- Returns:
- this returns the leaf element for the path
-
getPath
This location contains the full path expression with all of the indexes explicitly shown for each path segment. This is used to create a uniform representation that can be used for comparisons of different path expressions.- Specified by:
getPathin interfaceExpression- Returns:
- this returns an expanded version of the path
-
getElement
This is used to acquire the element path using this XPath expression. The element path is simply the fully qualified path for this expression with the provided name appended. If this is an empty path, the provided name is returned.- Specified by:
getElementin interfaceExpression- Parameters:
name- this is the name of the element to be used- Returns:
- a fully qualified path for the specified name
-
getElementPath
This is used to acquire the element path using this XPath expression. The element path is simply the fully qualified path for this expression with the provided name appended. If this is an empty path, the provided name is returned.- Parameters:
path- this is the path expression to be usedname- this is the name of the element to be used- Returns:
- a fully qualified path for the specified name
-
getAttribute
This is used to acquire the attribute path using this XPath expression. The attribute path is simply the fully qualified path for this expression with the provided name appended. If this is an empty path, the provided name is returned.- Specified by:
getAttributein interfaceExpression- Parameters:
name- this is the name of the attribute to be used- Returns:
- a fully qualified path for the specified name
-
getAttributePath
This is used to acquire the attribute path using this XPath expression. The attribute path is simply the fully qualified path for this expression with the provided name appended. If this is an empty path, the provided name is returned.- Parameters:
path- this is the path expression to be usedname- this is the name of the attribute to be used- Returns:
- a fully qualified path for the specified name
-
iterator
This is used to iterate over the path segments that have been extracted from the source XPath expression. Iteration over the segments is done in the order they were parsed from the source path. -
getPath
This allows an expression to be extracted from the current context. Extracting expressions in this manner makes it more convenient for navigating structures representing the XML document. If an expression can not be extracted with the given criteria an exception will be thrown.- Specified by:
getPathin interfaceExpression- Parameters:
from- this is the number of segments to skip to- Returns:
- this returns an expression from this one
-
getPath
This allows an expression to be extracted from the current context. Extracting expressions in this manner makes it more convenient for navigating structures representing the XML document. If an expression can not be extracted with the given criteria an exception will be thrown.- Specified by:
getPathin interfaceExpression- Parameters:
from- this is the number of segments to skip totrim- the number of segments to trim from the end- Returns:
- this returns an expression from this one
-
parse
This method is used to parse the provided XPath expression. When parsing the expression this will trim any references to the root context, also any trailing slashes are removed. An exception is thrown if the path is invalid.- Parameters:
path- this is the XPath expression to be parsed- Throws:
Exception
-
path
-
build
private void build()This method is used to build a fully qualified path that has each segment index. Building a path in this manner ensures that a parsed path can have a unique string that identifies the exact XML element the expression points to. -
skip
-
segment
-
element
This is used to extract an element from the path expression. An element value is one that contains only alphanumeric values or any special characters allowed within an XML element name. If an illegal character is found an exception is thrown.- Throws:
Exception
-
attribute
This is used to extract an attribute from the path expression. An attribute value is one that contains only alphanumeric values or any special characters allowed within an XML attribute name. If an illegal character is found an exception is thrown.- Throws:
Exception
-
index
-
truncate
-
align
-
isEmpty
This is used to determine if a string is empty. A string is considered empty if it is null or of zero length.- Parameters:
text- this is the text to check if it is empty- Returns:
- this returns true if the string is empty or null
-
isDigit
private boolean isDigit(char value) This is used to determine if the provided character is a digit. Only digits can be used within a segment index, so this is used when parsing the index to ensure all characters are valid.- Parameters:
value- this is the value of the character- Returns:
- this returns true if the provide character is a digit
-
isValid
private boolean isValid(char value) This is used to determine if the provided character is a legal XML element character. This is used to ensure all extracted element names conform to legal element names.- Parameters:
value- this is the value of the character- Returns:
- this returns true if the provided character is legal
-
isSpecial
private boolean isSpecial(char value) This is used to determine if the provided character is a legal XML element character. This is used to ensure all extracted element and attribute names conform to the XML specification.- Parameters:
value- this is the value of the character- Returns:
- this returns true if the provided character is legal
-
isLetter
private boolean isLetter(char value) This is used to determine if the provided character is an alpha numeric character. This is used to ensure all extracted element and attribute names conform to the XML specification.- Parameters:
value- this is the value of the character- Returns:
- this returns true if the provided character is legal
-
element
private void element(int start, int count) This will add a path segment to the list of segments. A path segment is added only if it has at least one character. All segments can be iterated over when parsing has completed.- Parameters:
start- this is the start offset for the path segmentcount- this is the number of characters in the segment
-
attribute
private void attribute(int start, int count) This will add a path segment to the list of segments. A path segment is added only if it has at least one character. All segments can be iterated over when parsing has completed.- Parameters:
start- this is the start offset for the path segmentcount- this is the number of characters in the segment
-
element
This will insert the path segment provided. A path segment is represented by an optional namespace prefix and an XML element name. If there is no prefix then a null is entered this will ensure that the names and segments are kept aligned by index.- Parameters:
segment- this is the path segment to be inserted
-
attribute
This will insert the path segment provided. A path segment is represented by an optional namespace prefix and an XML element name. If there is no prefix then a null is entered this will ensure that the names and segments are kept aligned by index.- Parameters:
segment- this is the path segment to be inserted
-
toString
Provides a canonical XPath expression. This is used for both debugging and reporting. The path returned represents the original path that has been parsed to form the expression.- Specified by:
toStringin interfaceExpression- Overrides:
toStringin classObject- Returns:
- this returns the string format for the XPath
-