use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class XPathImpl method evaluate.
/**
* <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
* and return the result as the specified type.</p>
*
* <p>This method builds a data model for the {@link InputSource} and calls
* {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
*
* <p>See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
* for context item evaluation,
* variable, function and QName resolution and return type conversion.</p>
*
* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
* then an <code>IllegalArgumentException</code> is thrown.</p>
*
* <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
* then a <code>NullPointerException</code> is thrown.</p>
*
* @param expression The XPath expression.
* @param source The input source of the document to evaluate over.
* @param returnType The desired return type.
*
* @return The <code>Object</code> that encapsulates the result of evaluating the expression.
*
* @throws XPathExpressionException If expression cannot be evaluated.
* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
* @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
* is <code>null</code>.
*/
public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException {
// Checking validity of different parameters
if (source == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "source" });
throw new NullPointerException(fmsg);
}
if (expression == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "XPath expression" });
throw new NullPointerException(fmsg);
}
if (returnType == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "returnType" });
throw new NullPointerException(fmsg);
}
//returnType need to be defined in XPathConstants
if (!isSupported(returnType)) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, new Object[] { returnType.toString() });
throw new IllegalArgumentException(fmsg);
}
try {
Document document = getParser().parse(source);
XObject resultObject = eval(expression, document);
return getResultAsType(resultObject, returnType);
} catch (SAXException e) {
throw new XPathExpressionException(e);
} catch (IOException e) {
throw new XPathExpressionException(e);
} catch (javax.xml.transform.TransformerException te) {
Throwable nestedException = te.getException();
if (nestedException instanceof javax.xml.xpath.XPathFunctionException) {
throw (javax.xml.xpath.XPathFunctionException) nestedException;
} else {
throw new XPathExpressionException(te);
}
}
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class Operation method execute.
/**
* Execute a binary operation by calling execute on each of the operands,
* and then calling the operate method on the derived class.
*
*
* @param xctxt The runtime execution context.
*
* @return The XObject result of the operation.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
XObject left = m_left.execute(xctxt, true);
XObject right = m_right.execute(xctxt, true);
XObject result = operate(left, right);
left.detach();
right.detach();
return result;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class ContextMatchStepPattern method executeRelativePathPattern.
/**
* Execute the match pattern step relative to another step.
*
*
* @param xctxt The XPath runtime context.
* NEEDSDOC @param prevStep
*
* @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 executeRelativePathPattern(XPathContext xctxt, StepPattern prevStep) throws javax.xml.transform.TransformerException {
XObject score = NodeTest.SCORE_NONE;
int context = xctxt.getCurrentNode();
DTM dtm = xctxt.getDTM(context);
if (null != dtm) {
int predContext = xctxt.getCurrentNode();
DTMAxisTraverser traverser;
int axis = m_axis;
boolean needToTraverseAttrs = WalkerFactory.isDownwardAxisOfMany(axis);
boolean iterRootIsAttr = (dtm.getNodeType(xctxt.getIteratorRoot()) == DTM.ATTRIBUTE_NODE);
if ((Axis.PRECEDING == axis) && iterRootIsAttr) {
axis = Axis.PRECEDINGANDANCESTOR;
}
traverser = dtm.getAxisTraverser(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) {
// predContext, relative);
if (executePredicates(xctxt, dtm, context))
return score;
score = NodeTest.SCORE_NONE;
}
if (needToTraverseAttrs && iterRootIsAttr && (DTM.ELEMENT_NODE == dtm.getNodeType(relative))) {
int xaxis = Axis.ATTRIBUTE;
for (int i = 0; i < 2; i++) {
DTMAxisTraverser atraverser = dtm.getAxisTraverser(xaxis);
for (int arelative = atraverser.first(relative); DTM.NULL != arelative; arelative = atraverser.next(relative, arelative)) {
try {
xctxt.pushCurrentNode(arelative);
score = execute(xctxt);
if (score != NodeTest.SCORE_NONE) {
if (score != NodeTest.SCORE_NONE)
return score;
}
} finally {
xctxt.popCurrentNode();
}
}
xaxis = Axis.NAMESPACE;
}
}
} finally {
xctxt.popCurrentNode();
}
}
}
return score;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class XPathExpressionImpl method eval.
private XObject eval(Object contextItem) throws javax.xml.transform.TransformerException {
org.apache.xpath.XPathContext xpathSupport = null;
// expressions.
if (functionResolver != null) {
JAXPExtensionsProvider jep = new JAXPExtensionsProvider(functionResolver, featureSecureProcessing);
xpathSupport = new org.apache.xpath.XPathContext(jep, false);
} else {
xpathSupport = new org.apache.xpath.XPathContext(false);
}
xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
XObject xobj = null;
Node contextNode = (Node) contextItem;
// dummy Document as Context Node
if (contextNode == null) {
contextNode = getDummyDocument();
}
xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);
return xobj;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class PredicatedNodeTest method acceptNode.
//=============== NodeFilter Implementation ===============
/**
* Test whether a specified node is visible in the logical view of a
* TreeWalker or NodeIterator. This function will be called by the
* implementation of TreeWalker and NodeIterator; it is not intended to
* be called directly from user code.
* @param n The node to check to see if it passes the filter or not.
* @return a constant to determine whether the node is accepted,
* rejected, or skipped, as defined above .
*/
public short acceptNode(int n) {
XPathContext xctxt = m_lpi.getXPathContext();
try {
xctxt.pushCurrentNode(n);
XObject score = execute(xctxt, n);
// System.out.println("\n::acceptNode - score: "+score.num()+"::");
if (score != NodeTest.SCORE_NONE) {
if (getPredicateCount() > 0) {
countProximityPosition(0);
if (!executePredicates(n, xctxt))
return DTMIterator.FILTER_SKIP;
}
return DTMIterator.FILTER_ACCEPT;
}
} catch (javax.xml.transform.TransformerException se) {
// TODO: Fix this.
throw new RuntimeException(se.getMessage());
} finally {
xctxt.popCurrentNode();
}
return DTMIterator.FILTER_SKIP;
}
Aggregations