Class PathParser

java.lang.Object
org.simpleframework.xml.core.PathParser
All Implemented Interfaces:
Iterable<String>, Expression

class PathParser extends Object implements 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/@attribute 
    
 
If 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 Classes
    Modifier and Type
    Class
    Description
    private class 
    The PathSection represents a section of a path that is extracted.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    This is used to determine if the path is an attribute.
    protected Cache<String>
    This is used to cache the attributes created by this path.
    protected StringBuilder
    This is used to build a fully qualified path expression.
    protected String
    This is the the cached canonical representation of the path.
    protected int
    This represents the number of characters in the source path.
    protected char[]
    This is a copy of the source data that is to be parsed.
    protected Cache<String>
    This is used to cache the elements created by this path.
    protected List<Integer>
    This contains a list of the indexes for each path segment.
    protected String
    This is the fully qualified path expression for this.
    protected List<String>
    This contains a list of the path segments that were parsed.
    protected int
    This is the current seek position for the parser.
    protected String
    This is a cache of the canonical path representation.
    protected List<String>
    This is used to store the path prefixes for the parsed path.
    protected int
    This is the start offset that skips any root references.
    protected Style
    This is the format used to style the path segments.
    protected Type
    This is the type the expressions are to be parsed for.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PathParser(String path, Type type, Format format)
    Constructor for the PathParser object.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    This is used to add a default index to a segment or attribute extracted from the source expression.
    private void
    This is used to extract an attribute from the path expression.
    private void
    attribute(int start, int count)
    This will add a path segment to the list of segments.
    private void
    attribute(String segment)
    This will insert the path segment provided.
    private void
    This method is used to build a fully qualified path that has each segment index.
    private void
    This is used to extract an element from the path expression.
    private void
    element(int start, int count)
    This will add a path segment to the list of segments.
    private void
    element(String segment)
    This will insert the path segment provided.
    This is used to acquire the attribute path using this XPath expression.
    protected String
    This is used to acquire the attribute path using this XPath expression.
    This is used to acquire the element path using this XPath expression.
    protected String
    This is used to acquire the element path using this XPath expression.
    This can be used to acquire the first path segment within the expression.
    int
    If the first path segment contains an index it is provided by this method.
    This can be used to acquire the last path segment within the expression.
    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 void
    This is used to extract an index from an element.
    boolean
    This is used to determine if the expression points to an attribute value.
    private boolean
    isDigit(char value)
    This is used to determine if the provided character is a digit.
    boolean
    This method is used to determine if this expression is an empty path.
    private boolean
    This is used to determine if a string is empty.
    private boolean
    isLetter(char value)
    This is used to determine if the provided character is an alpha numeric character.
    boolean
    This is used to determine if the expression is a path.
    private boolean
    isSpecial(char value)
    This is used to determine if the provided character is a legal XML element character.
    private boolean
    isValid(char value)
    This is used to determine if the provided character is a legal XML element character.
    This is used to iterate over the path segments that have been extracted from the source XPath expression.
    private void
    parse(String path)
    This method is used to parse the provided XPath expression.
    private void
    This method is used to parse the provided XPath expression.
    private void
    This method is used to extract a path segment from the source expression.
    private void
    This is used to skip any root prefix for the path.
    Provides a canonical XPath expression.
    private void
    This method is used to trim any trailing characters at the end of the path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • attributes

      protected Cache<String> attributes
      This is used to cache the attributes created by this path.
    • elements

      protected Cache<String> elements
      This is used to cache the elements created by this path.
    • indexes

      protected List<Integer> indexes
      This contains a list of the indexes for each path segment.
    • prefixes

      protected List<String> prefixes
      This is used to store the path prefixes for the parsed path.
    • names

      protected List<String> names
      This contains a list of the path segments that were parsed.
    • builder

      protected StringBuilder builder
      This is used to build a fully qualified path expression.
    • location

      protected String location
      This is the fully qualified path expression for this.
    • cache

      protected String cache
      This is the the cached canonical representation of the path.
    • path

      protected String path
      This is a cache of the canonical path representation.
    • style

      protected Style style
      This is the format used to style the path segments.
    • type

      protected Type type
      This is the type the expressions are to be parsed for.
    • attribute

      protected boolean attribute
      This is used to determine if the path is an attribute.
    • data

      protected char[] data
      This is a copy of the source data that is to be parsed.
    • count

      protected int count
      This represents the number of characters in the source path.
    • start

      protected int start
      This is the start offset that skips any root references.
    • off

      protected int off
      This is the current seek position for the parser.
  • Constructor Details

    • PathParser

      public PathParser(String path, Type type, Format format) throws Exception
      Constructor for the PathParser object. 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 parsed
      type - this is the type the expressions are parsed for
      format - 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:
      isEmpty in interface Expression
      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:
      isPath in interface Expression
      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:
      isAttribute in interface Expression
      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:
      getIndex in interface Expression
      Returns:
      this returns the index of this path expression
    • getPrefix

      public String 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:
      getPrefix in interface Expression
      Returns:
      this returns the prefix for the path expression
    • getFirst

      public String 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:
      getFirst in interface Expression
      Returns:
      this returns the parent element for the path
    • getLast

      public String 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:
      getLast in interface Expression
      Returns:
      this returns the leaf element for the path
    • getPath

      public String 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:
      getPath in interface Expression
      Returns:
      this returns an expanded version of the path
    • getElement

      public String getElement(String name)
      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:
      getElement in interface Expression
      Parameters:
      name - this is the name of the element to be used
      Returns:
      a fully qualified path for the specified name
    • getElementPath

      protected String getElementPath(String path, String name)
      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 used
      name - this is the name of the element to be used
      Returns:
      a fully qualified path for the specified name
    • getAttribute

      public String getAttribute(String name)
      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:
      getAttribute in interface Expression
      Parameters:
      name - this is the name of the attribute to be used
      Returns:
      a fully qualified path for the specified name
    • getAttributePath

      protected String getAttributePath(String path, String name)
      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 used
      name - this is the name of the attribute to be used
      Returns:
      a fully qualified path for the specified name
    • iterator

      public Iterator<String> 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.
      Specified by:
      iterator in interface Iterable<String>
      Returns:
      this returns an iterator for the path segments
    • getPath

      public Expression getPath(int from)
      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:
      getPath in interface Expression
      Parameters:
      from - this is the number of segments to skip to
      Returns:
      this returns an expression from this one
    • getPath

      public Expression getPath(int from, int trim)
      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:
      getPath in interface Expression
      Parameters:
      from - this is the number of segments to skip to
      trim - the number of segments to trim from the end
      Returns:
      this returns an expression from this one
    • parse

      private void parse(String path) throws Exception
      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

      private void path() throws Exception
      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.
      Throws:
      Exception
    • 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

      private void skip() throws Exception
      This is used to skip any root prefix for the path. Skipping the root prefix ensures that it is not considered as a valid path segment and so is not returned as part of the iterator nor is it considered with building a string representation.
      Throws:
      Exception
    • segment

      private void segment() throws Exception
      This method is used to extract a path segment from the source expression. Before extracting the segment this validates the input to ensure it represents a valid path. If the path is not valid then this will thrown an exception.
      Throws:
      Exception
    • element

      private void element() throws Exception
      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

      private void attribute() throws Exception
      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

      private void index() throws Exception
      This is used to extract an index from an element. An index is a numerical value that identifies the position of the path within the XML document. If the index can not be extracted from the expression an exception is thrown.
      Throws:
      Exception
    • truncate

      private void truncate() throws Exception
      This method is used to trim any trailing characters at the end of the path. Trimming will remove any trailing legal characters at the end of the path that we do not want in a canonical string representation of the path expression.
      Throws:
      Exception
    • align

      private void align() throws Exception
      This is used to add a default index to a segment or attribute extracted from the source expression. In the event that a segment does not contain an index, the default index of 1 is assigned to the element for consistency.
      Throws:
      Exception
    • isEmpty

      private boolean isEmpty(String text)
      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 segment
      count - 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 segment
      count - this is the number of characters in the segment
    • element

      private void element(String segment)
      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

      private void attribute(String segment)
      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

      public String 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:
      toString in interface Expression
      Overrides:
      toString in class Object
      Returns:
      this returns the string format for the XPath