Update of /cvsroot/graphl/graphl/src/org/jaxen/expr
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv16361/src/org/jaxen/expr
Added Files:
DefaultRelativeLocationPath.java
Log Message:
backup commit - first implementation of xpath-based structural filters
--- NEW FILE: DefaultRelativeLocationPath.java ---
/*
* Created on 10.07.2006
*/
package org.jaxen.expr;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jaxen.Context;
import org.jaxen.ContextSupport;
import org.jaxen.JaxenException;
import org.jaxen.util.SingletonList;
import org.mediavirus.graphl.jaxen.DocumentNavigator;
import org.mediavirus.graphl.jaxen.GraphlContext;
public class DefaultRelativeLocationPath extends DefaultLocationPath {
public String toString()
{
return "[(DefaultRelativeLocationPath): " + super.toString() + "]";
}
public void accept(Visitor visitor)
{
visitor.visit(this);
}
public Object evaluate(Context context) throws JaxenException
{
List nodeSet = context.getNodeSet();
List contextNodeSet = new ArrayList(nodeSet);
ContextSupport support = context.getContextSupport();
Context stepContext = new Context(support);
if (!(context instanceof GraphlContext)) {
Iterator stepIter = getSteps().iterator();
while ( stepIter.hasNext() )
{
Step eachStep = (Step) stepIter.next();
List results = new ArrayList();
for (Object o : contextNodeSet) {
stepContext.setNodeSet(new SingletonList(o));
List result = eachStep.evaluate(stepContext);
results.addAll(result);
if (support.getNavigator() instanceof org.mediavirus.graphl.jaxen.DocumentNavigator) {
((DocumentNavigator)support.getNavigator()).visit(eachStep, o, result);
}
}
contextNodeSet = results;
// now we need to reverse the list if this is a reverse axis
if (isReverseAxis(eachStep)) {
Collections.reverse(contextNodeSet);
}
}
if (getSteps().size() > 1) {
Collections.sort(contextNodeSet, new NodeComparator(support.getNavigator()));
}
return contextNodeSet;
}
else {
stepContext.setNodeSet(contextNodeSet);
return doStep(getSteps(), 0, stepContext);
}
}
/**
* Recursively performs stepping through the path, returning all paths that were leading to an end node.
* @param pathSoFar
* @param stepIter
* @param stepContext
* @return A List of Lists, containing the successful paths (the result node is at the end)
*/
protected List doStep(List steps, int stepnum, Context stepContext) throws JaxenException{
if (stepnum < steps.size()) {
Step currentStep = (Step) steps.get(stepnum++);
List results = new ArrayList();
List nextContext = currentStep.evaluate(stepContext);
for (Object o : nextContext) {
// this selects the nodes for the next step
stepContext.setNodeSet(new SingletonList(o));
// this returns a list of lists, the paths to end nodes
List result = doStep(steps, stepnum, stepContext);
for (Object o2 : result) {
List path = (List)o2;
path.add(0,o);
}
results.addAll(result);
}
return results;
}
else {
// we are at the end of the path - just create the list for filling in the results from the callers
List results = new ArrayList();
results.add(new ArrayList());
return results;
}
}
private boolean isReverseAxis(Step step) {
int axis = step.getAxis();
return axis == org.jaxen.saxpath.Axis.PRECEDING
|| axis == org.jaxen.saxpath.Axis.PRECEDING_SIBLING
|| axis == org.jaxen.saxpath.Axis.ANCESTOR
|| axis == org.jaxen.saxpath.Axis.ANCESTOR_OR_SELF;
}
}
|