Search in sources :

Example 1 with XPath2Expression

use of org.eclipse.wst.xml.xpath2.api.XPath2Expression in project webtools.sourceediting by eclipse.

the class FilteringPerformanceTest method evalXPath2.

protected Object evalXPath2(String xpath, Node doc, Class resultClass) {
    StaticContext sc = new StaticContextBuilder();
    XPath2Expression path = new Engine().parseExpression(xpath, sc);
    DynamicContext dynamicContext = new DynamicContextBuilder(sc);
    long before = System.nanoTime();
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    Object result = rs.value(0);
    long after = System.nanoTime();
    System.out.println("XPath2 " + xpath + " evaluated to " + result + " in " + (after - before) / 1000 + " μs");
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return resultClass.cast(result);
}
Also used : StaticContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) StaticContext(org.eclipse.wst.xml.xpath2.api.StaticContext) DynamicContext(org.eclipse.wst.xml.xpath2.api.DynamicContext)

Example 2 with XPath2Expression

use of org.eclipse.wst.xml.xpath2.api.XPath2Expression in project webservices-axiom by apache.

the class PsychoPathTest method evaluate.

private static ResultSequence evaluate(String xpath) throws Exception {
    InputStream is = PsychoPathTest.class.getResourceAsStream("test.xml");
    try {
        OMFactory factory = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory();
        Document doc = (Document) OMXMLBuilderFactory.createOMBuilder(factory, is).getDocument();
        StaticContextBuilder scb = new StaticContextBuilder();
        XPath2Expression expr = new Engine().parseExpression(xpath, scb);
        return expr.evaluate(new DynamicContextBuilder(scb), new Object[] { doc });
    } finally {
        is.close();
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StaticContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder) InputStream(java.io.InputStream) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) Document(org.w3c.dom.Document) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine)

Example 3 with XPath2Expression

use of org.eclipse.wst.xml.xpath2.api.XPath2Expression in project webtools.sourceediting by eclipse.

the class CompleteNewApiTest method evaluateSimpleXPath.

protected Object evaluateSimpleXPath(String xpath, StaticContext sc, Document doc, Class resultClass) {
    XPath2Expression path = new Engine().parseExpression(xpath, sc);
    DynamicContext dynamicContext = new DynamicContextBuilder(sc);
    org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    Object result = rs.value(0);
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return result;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) DynamicContext(org.eclipse.wst.xml.xpath2.api.DynamicContext)

Example 4 with XPath2Expression

use of org.eclipse.wst.xml.xpath2.api.XPath2Expression in project webtools.sourceediting by eclipse.

the class Engine method parseExpression.

public XPath2Expression parseExpression(String expression, StaticContext context) {
    XPath xPath = new JFlexCupParser().parse(expression);
    xPath.setStaticContext(context);
    StaticNameResolver name_check = new StaticNameResolver(context);
    name_check.check(xPath);
    xPath.setAxes(name_check.getAxes());
    xPath.setFreeVariables(name_check.getFreeVariables());
    xPath.setResolvedFunctions(name_check.getResolvedFunctions());
    xPath.setRootUsed(name_check.isRootUsed());
    return xPath;
}
Also used : XPath(org.eclipse.wst.xml.xpath2.processor.ast.XPath)

Aggregations

XPath2Expression (org.eclipse.wst.xml.xpath2.api.XPath2Expression)3 Engine (org.eclipse.wst.xml.xpath2.processor.Engine)3 DynamicContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder)3 DynamicContext (org.eclipse.wst.xml.xpath2.api.DynamicContext)2 StaticContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder)2 InputStream (java.io.InputStream)1 OMFactory (org.apache.axiom.om.OMFactory)1 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)1 StaticContext (org.eclipse.wst.xml.xpath2.api.StaticContext)1 XPath (org.eclipse.wst.xml.xpath2.processor.ast.XPath)1 Document (org.w3c.dom.Document)1