use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class PredicatedNodeTest method executePredicates.
/**
* Process the predicates.
*
* @param context The current context node.
* @param xctxt The XPath runtime context.
*
* @return the result of executing the predicate expressions.
*
* @throws javax.xml.transform.TransformerException
*/
boolean executePredicates(int context, XPathContext xctxt) throws javax.xml.transform.TransformerException {
int nPredicates = getPredicateCount();
// System.out.println("nPredicates: "+nPredicates);
if (nPredicates == 0)
return true;
PrefixResolver savedResolver = xctxt.getNamespaceContext();
try {
m_predicateIndex = 0;
xctxt.pushSubContextList(this);
xctxt.pushNamespaceContext(m_lpi.getPrefixResolver());
xctxt.pushCurrentNode(context);
for (int i = 0; i < nPredicates; i++) {
// System.out.println("Executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
XObject pred = m_predicates[i].execute(xctxt);
// System.out.println("pred.getType(): "+pred.getType());
if (XObject.CLASS_NUMBER == pred.getType()) {
if (DEBUG_PREDICATECOUNTING) {
System.out.flush();
System.out.println("\n===== start predicate count ========");
System.out.println("m_predicateIndex: " + m_predicateIndex);
// System.out.println("getProximityPosition(m_predicateIndex): "
// + getProximityPosition(m_predicateIndex));
System.out.println("pred.num(): " + pred.num());
}
int proxPos = this.getProximityPosition(m_predicateIndex);
int predIndex = (int) pred.num();
if (proxPos != predIndex) {
if (DEBUG_PREDICATECOUNTING) {
System.out.println("\nnode context: " + nodeToString(context));
System.out.println("index predicate is false: " + proxPos);
System.out.println("\n===== end predicate count ========");
}
return false;
} else if (DEBUG_PREDICATECOUNTING) {
System.out.println("\nnode context: " + nodeToString(context));
System.out.println("index predicate is true: " + proxPos);
System.out.println("\n===== end predicate count ========");
}
// only sets m_foundLast if on the last predicate
if (m_predicates[i].isStableNumber() && i == nPredicates - 1) {
m_foundLast = true;
}
} else if (!pred.bool())
return false;
countProximityPosition(++m_predicateIndex);
}
} finally {
xctxt.popCurrentNode();
xctxt.popNamespaceContext();
xctxt.popSubContextList();
m_predicateIndex = -1;
}
return true;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class StepPattern method getProximityPosition.
/**
* Get the proximity position index of the current node based on this
* node test.
*
*
* @param xctxt XPath runtime context.
* @param predPos Which predicate we're evaluating of foo[1][2][3].
* @param findLast If true, don't terminate when the context node is found.
*
* @return the proximity position index of the current node based on the
* node test.
*/
private final int getProximityPosition(XPathContext xctxt, int predPos, boolean findLast) {
int pos = 0;
int context = xctxt.getCurrentNode();
DTM dtm = xctxt.getDTM(context);
int parent = dtm.getParent(context);
try {
DTMAxisTraverser traverser = dtm.getAxisTraverser(Axis.CHILD);
for (int child = traverser.first(parent); DTM.NULL != child; child = traverser.next(parent, child)) {
try {
xctxt.pushCurrentNode(child);
if (NodeTest.SCORE_NONE != super.execute(xctxt, child)) {
boolean pass = true;
try {
xctxt.pushSubContextList(this);
for (int i = 0; i < predPos; i++) {
xctxt.pushPredicatePos(i);
try {
XObject pred = m_predicates[i].execute(xctxt);
try {
if (XObject.CLASS_NUMBER == pred.getType()) {
if ((pos + 1) != (int) pred.numWithSideEffects()) {
pass = false;
break;
}
} else if (!pred.boolWithSideEffects()) {
pass = false;
break;
}
} finally {
pred.detach();
}
} finally {
xctxt.popPredicatePos();
}
}
} finally {
xctxt.popSubContextList();
}
if (pass)
pos++;
if (!findLast && child == context) {
return pos;
}
}
} finally {
xctxt.popCurrentNode();
}
}
} catch (javax.xml.transform.TransformerException se) {
// TODO: should keep throw sax exception...
throw new java.lang.RuntimeException(se.getMessage());
}
return pos;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class StepPattern method executeRelativePathPattern.
/**
* Execute the match pattern step relative to another step.
*
*
* @param xctxt The XPath runtime context.
* @param dtm The DTM of the current node.
* @param currentNode The current node context.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
protected final XObject executeRelativePathPattern(XPathContext xctxt, DTM dtm, int currentNode) throws javax.xml.transform.TransformerException {
XObject score = NodeTest.SCORE_NONE;
int context = currentNode;
DTMAxisTraverser traverser;
traverser = dtm.getAxisTraverser(m_axis);
for (int relative = traverser.first(context); DTM.NULL != relative; relative = traverser.next(context, relative)) {
try {
xctxt.pushCurrentNode(relative);
score = execute(xctxt);
if (score != NodeTest.SCORE_NONE)
break;
} finally {
xctxt.popCurrentNode();
}
}
return score;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class StepPattern method getMatchScore.
/**
* Get the match score of the given node.
*
* @param xctxt The XPath runtime context.
* @param context The node to be tested.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
public double getMatchScore(XPathContext xctxt, int context) throws javax.xml.transform.TransformerException {
xctxt.pushCurrentNode(context);
xctxt.pushCurrentExpressionNode(context);
try {
XObject score = execute(xctxt);
return score.num();
} finally {
xctxt.popCurrentNode();
xctxt.popCurrentExpressionNode();
}
// return XPath.MATCH_SCORE_NONE;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class UnionPattern method execute.
/**
* Test a node to see if it matches any of the patterns in the union.
*
* @param xctxt XPath runtime context.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
XObject bestScore = null;
int n = m_patterns.length;
for (int i = 0; i < n; i++) {
XObject score = m_patterns[i].execute(xctxt);
if (score != NodeTest.SCORE_NONE) {
if (null == bestScore)
bestScore = score;
else if (score.num() > bestScore.num())
bestScore = score;
}
}
if (null == bestScore) {
bestScore = NodeTest.SCORE_NONE;
}
return bestScore;
}
Aggregations