org.apache.commons.jxpath.ri.axes
Class SimplePathInterpreter

java.lang.Object
  extended by org.apache.commons.jxpath.ri.axes.SimplePathInterpreter

public class SimplePathInterpreter
extends java.lang.Object

An evaluation mechanism for simple XPaths, which is much faster than the usual process. It is only used for xpaths which have no context-dependent parts, consist entirely of child::name and self::node() steps with predicates that either integer or have the form [@name = ...].

Version:
$Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
Author:
Dmitri Plotnikov

Field Summary
private static int PERFECT_MATCH
           
private static QName QNAME_NAME
           
 
Constructor Summary
SimplePathInterpreter()
           
 
Method Summary
private static int computeQuality(NodePointer pointer)
          For a pointer that matches an actual node, returns 0.
private static NodePointer createChildPointerForStep(PropertyOwnerPointer parentPointer, Step step)
          Create the child pointer for a given step.
static NodePointer createNullPointer(EvalContext context, NodePointer parent, Step[] steps, int currentStep)
          Creates a "null pointer" that a) represents the requested path and b) can be used for creation of missing nodes in the path.
private static NodePointer createNullPointerForPredicates(EvalContext context, NodePointer parent, Step[] steps, int currentStep, Expression[] predicates, int currentPredicate)
          Creates a "null pointer" that starts with predicates.
private static NodePointer doPredicate(EvalContext context, NodePointer parent, Step[] steps, int currentStep, Expression[] predicates, int currentPredicate)
          Evaluates predicates and proceeds with the subsequent steps of the path.
private static NodePointer doPredicateIndex(EvalContext context, NodePointer parent, Step[] steps, int currentStep, Expression[] predicates, int currentPredicate)
          Evaluate a subscript predicate: see if the node is a collection and if the index is inside the collection.
private static NodePointer doPredicateName(EvalContext context, NodePointer parent, Step[] steps, int currentStep, Expression[] predicates, int currentPredicate)
          Execute a NameAttributeTest predicate
private static NodePointer doPredicatesStandard(EvalContext context, java.util.List parents, Step[] steps, int currentStep, Expression[] predicates, int currentPredicate)
          Called exclusively for standard InfoSet nodes, e.g.
private static NodePointer doStep(EvalContext context, NodePointer parent, Step[] steps, int currentStep)
          Recursive evaluation of a path.
private static NodePointer doStepNoPredicatesPropertyOwner(EvalContext context, PropertyOwnerPointer parentPointer, Step[] steps, int currentStep)
          We have a step that starts with a property owner (bean, map, etc) and has no predicates.
private static NodePointer doStepNoPredicatesStandard(EvalContext context, NodePointer parentPointer, Step[] steps, int currentStep)
          A path that starts with a standard InfoSet node (e.g.
private static NodePointer doStepPredicatesPropertyOwner(EvalContext context, PropertyOwnerPointer parentPointer, Step[] steps, int currentStep)
          A path that starts with a property owner.
private static NodePointer doStepPredicatesStandard(EvalContext context, NodePointer parent, Step[] steps, int currentStep)
          A path that starts with a standard InfoSet node, e.g.
private static NodeIterator getNodeIterator(EvalContext context, NodePointer pointer, Step step)
          Get a NodeIterator.
private static int indexFromPredicate(EvalContext context, Expression predicate)
          Extract an integer from a subscript predicate.
static NodePointer interpretSimpleExpressionPath(EvalContext context, NodePointer root, Expression[] predicates, Step[] steps)
          Interpret the steps of a simple expression path that starts with the given root, which is the result of evaluation of the root expression of the expression path, applies the given predicates to it and then follows the given steps.
static NodePointer interpretSimpleLocationPath(EvalContext context, NodePointer root, Step[] steps)
          Interpret a simple path that starts with the given root and follows the given steps.
private static boolean isCollectionElement(NodePointer pointer, int index)
          Returns true if the pointer is a collection and the index is withing the bounds of the collection.
private static boolean isLangAttribute(QName name)
          Learn whether name is a lang attribute.
private static boolean isNameAttributeEqual(NodePointer pointer, java.lang.String name)
          Returns true if the pointer has an attribute called "name" and its value is equal to the supplied string.
private static java.lang.String keyFromPredicate(EvalContext context, Expression predicate)
          Extracts the string value of the expression from a predicate like [@name=expression].
private static NodePointer valuePointer(NodePointer pointer)
          For an intermediate pointer (e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

QNAME_NAME

private static final QName QNAME_NAME

PERFECT_MATCH

private static final int PERFECT_MATCH
See Also:
Constant Field Values
Constructor Detail

SimplePathInterpreter

public SimplePathInterpreter()
Method Detail

interpretSimpleLocationPath

public static NodePointer interpretSimpleLocationPath(EvalContext context,
                                                      NodePointer root,
                                                      Step[] steps)
Interpret a simple path that starts with the given root and follows the given steps. All steps must have the axis "child::" and a name test. They can also optionally have predicates of type [@name=expression] or simply [expression] interpreted as an index.

Parameters:
context - evaluation context
root - root pointer
steps - path steps
Returns:
NodePointer

interpretSimpleExpressionPath

public static NodePointer interpretSimpleExpressionPath(EvalContext context,
                                                        NodePointer root,
                                                        Expression[] predicates,
                                                        Step[] steps)
Interpret the steps of a simple expression path that starts with the given root, which is the result of evaluation of the root expression of the expression path, applies the given predicates to it and then follows the given steps. All steps must have the axis "child::" or "attribute::" and a name test. They can also optionally have predicates of type [@name=...] or simply [...] interpreted as an index.

Parameters:
context - evaluation context
root - root pointer
predicates - predicates corresponding to steps
steps - path steps
Returns:
NodePointer

doStep

private static NodePointer doStep(EvalContext context,
                                  NodePointer parent,
                                  Step[] steps,
                                  int currentStep)
Recursive evaluation of a path. The general plan is: Look at the current step, find nodes that match it, iterate over those nodes and for each of them call doStep again for subsequent steps.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

doStepNoPredicatesPropertyOwner

private static NodePointer doStepNoPredicatesPropertyOwner(EvalContext context,
                                                           PropertyOwnerPointer parentPointer,
                                                           Step[] steps,
                                                           int currentStep)
We have a step that starts with a property owner (bean, map, etc) and has no predicates. The name test of the step may map to a scalar property or to a collection. If it is a collection, we should apply the tail of the path to each element until we find a match. If we don't find a perfect match, we should return the "best quality" pointer, which has the longest chain of steps mapping to existing nodes and the shortes tail of Null* pointers.

Parameters:
context - evaluation context
parentPointer - property owner pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

doStepNoPredicatesStandard

private static NodePointer doStepNoPredicatesStandard(EvalContext context,
                                                      NodePointer parentPointer,
                                                      Step[] steps,
                                                      int currentStep)
A path that starts with a standard InfoSet node (e.g. DOM Node) and has no predicates. Get a child iterator and apply the tail of the path to each element until we find a match. If we don't find a perfect match, we should return the "best quality" pointer, which has the longest chain of steps mapping to existing nodes and the shortes tail of Null* pointers.

Parameters:
context - evaluation context
parentPointer - parent pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

doStepPredicatesPropertyOwner

private static NodePointer doStepPredicatesPropertyOwner(EvalContext context,
                                                         PropertyOwnerPointer parentPointer,
                                                         Step[] steps,
                                                         int currentStep)
A path that starts with a property owner. The method evaluates the first predicate in a special way and then forwards to a general predicate processing method.

Parameters:
context - evaluation context
parentPointer - parent pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

createChildPointerForStep

private static NodePointer createChildPointerForStep(PropertyOwnerPointer parentPointer,
                                                     Step step)
Create the child pointer for a given step.

Parameters:
parentPointer - parent pointer
step - associated step
Returns:
NodePointer

doStepPredicatesStandard

private static NodePointer doStepPredicatesStandard(EvalContext context,
                                                    NodePointer parent,
                                                    Step[] steps,
                                                    int currentStep)
A path that starts with a standard InfoSet node, e.g. a DOM Node. The method evaluates the first predicate in a special way and then forwards to a general predicate processing method.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

doPredicate

private static NodePointer doPredicate(EvalContext context,
                                       NodePointer parent,
                                       Step[] steps,
                                       int currentStep,
                                       Expression[] predicates,
                                       int currentPredicate)
Evaluates predicates and proceeds with the subsequent steps of the path.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
predicates - predicate expressions
currentPredicate - int predicate number
Returns:
NodePointer

doPredicateName

private static NodePointer doPredicateName(EvalContext context,
                                           NodePointer parent,
                                           Step[] steps,
                                           int currentStep,
                                           Expression[] predicates,
                                           int currentPredicate)
Execute a NameAttributeTest predicate

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - int step number
predicates - predicates
currentPredicate - int predicate number
Returns:
NodePointer

doPredicatesStandard

private static NodePointer doPredicatesStandard(EvalContext context,
                                                java.util.List parents,
                                                Step[] steps,
                                                int currentStep,
                                                Expression[] predicates,
                                                int currentPredicate)
Called exclusively for standard InfoSet nodes, e.g. DOM nodes to evaluate predicate sequences like [@name=...][@name=...][index].

Parameters:
context - evaluation context
parents - List of parent pointers
steps - path steps
currentStep - step number
predicates - predicates
currentPredicate - int predicate number
Returns:
NodePointer

doPredicateIndex

private static NodePointer doPredicateIndex(EvalContext context,
                                            NodePointer parent,
                                            Step[] steps,
                                            int currentStep,
                                            Expression[] predicates,
                                            int currentPredicate)
Evaluate a subscript predicate: see if the node is a collection and if the index is inside the collection.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
predicates - predicates
currentPredicate - int predicate number
Returns:
NodePointer

indexFromPredicate

private static int indexFromPredicate(EvalContext context,
                                      Expression predicate)
Extract an integer from a subscript predicate. The returned index starts with 0, even though the subscript starts with 1.

Parameters:
context - evaluation context
predicate - to evaluate
Returns:
calculated index

keyFromPredicate

private static java.lang.String keyFromPredicate(EvalContext context,
                                                 Expression predicate)
Extracts the string value of the expression from a predicate like [@name=expression].

Parameters:
context - evaluation context
predicate - predicate to evaluate
Returns:
String key extracted

computeQuality

private static int computeQuality(NodePointer pointer)
For a pointer that matches an actual node, returns 0. For a pointer that does not match an actual node, but whose parent pointer does returns -1, etc.

Parameters:
pointer - input pointer
Returns:
int match quality code

isNameAttributeEqual

private static boolean isNameAttributeEqual(NodePointer pointer,
                                            java.lang.String name)
Returns true if the pointer has an attribute called "name" and its value is equal to the supplied string.

Parameters:
pointer - input pointer
name - name to check
Returns:
boolean

isCollectionElement

private static boolean isCollectionElement(NodePointer pointer,
                                           int index)
Returns true if the pointer is a collection and the index is withing the bounds of the collection.

Parameters:
pointer - input pointer
index - to check
Returns:
boolean

valuePointer

private static NodePointer valuePointer(NodePointer pointer)
For an intermediate pointer (e.g. PropertyPointer, ContainerPointer) returns a pointer for the contained value.

Parameters:
pointer - input pointer
Returns:
NodePointer

createNullPointer

public static NodePointer createNullPointer(EvalContext context,
                                            NodePointer parent,
                                            Step[] steps,
                                            int currentStep)
Creates a "null pointer" that a) represents the requested path and b) can be used for creation of missing nodes in the path.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
Returns:
NodePointer

createNullPointerForPredicates

private static NodePointer createNullPointerForPredicates(EvalContext context,
                                                          NodePointer parent,
                                                          Step[] steps,
                                                          int currentStep,
                                                          Expression[] predicates,
                                                          int currentPredicate)
Creates a "null pointer" that starts with predicates.

Parameters:
context - evaluation context
parent - parent pointer
steps - path steps
currentStep - step number
predicates - predicates
currentPredicate - int predicate number
Returns:
NodePointer

getNodeIterator

private static NodeIterator getNodeIterator(EvalContext context,
                                            NodePointer pointer,
                                            Step step)
Get a NodeIterator.

Parameters:
context - evaluation context
pointer - owning pointer
step - triggering step
Returns:
NodeIterator

isLangAttribute

private static boolean isLangAttribute(QName name)
Learn whether name is a lang attribute.

Parameters:
name - to compare
Returns:
boolean