Search in sources :

Example 1 with DynamicContextBuilder

use of org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder 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 DynamicContextBuilder

use of org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method setupDynamicContext.

protected DefaultDynamicContext setupDynamicContext(XSModel schema) {
    XercesTypeModel typeModel = schema != null ? new XercesTypeModel(schema) : null;
    if (useNewApi) {
        staticContextBuilder.withTypeModel(typeModel);
        staticContextBuilder.withNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        staticContextBuilder.withNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
        staticContextBuilder.withNamespace("fn", "http://www.w3.org/2005/xpath-functions");
        staticContextBuilder.withNamespace("xml", "http://www.w3.org/XML/1998/namespace");
        dynamicContextBuilder = new DynamicContextBuilder(staticContextBuilder);
        setupVariables(dynamicContext);
        try {
            dynamicContextBuilder.withTimezoneOffset(DatatypeFactory.newInstance().newDuration(false, /*i.e. negative*/
            0, 0, 0, 5, 0, 0));
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException("Shouldn't fail here", e);
        }
        return null;
    }
    DefaultDynamicContext dc = new DefaultDynamicContext(typeModel);
    dynamicContext = dc;
    dc.add_namespace("xs", "http://www.w3.org/2001/XMLSchema");
    dc.add_namespace("xsd", "http://www.w3.org/2001/XMLSchema");
    dc.add_namespace("fn", "http://www.w3.org/2005/xpath-functions");
    dc.add_namespace("xml", "http://www.w3.org/XML/1998/namespace");
    dc.add_function_library(new FnFunctionLibrary());
    dc.add_function_library(new XSCtrLibrary());
    setupVariables(dc);
    return dc;
}
Also used : DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) FnFunctionLibrary(org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) DefaultDynamicContext(org.eclipse.wst.xml.xpath2.processor.DefaultDynamicContext) XercesTypeModel(org.eclipse.wst.xml.xpath2.processor.internal.types.xerces.XercesTypeModel) XSCtrLibrary(org.eclipse.wst.xml.xpath2.processor.function.XSCtrLibrary)

Example 3 with DynamicContextBuilder

use of org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder 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 4 with DynamicContextBuilder

use of org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder 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)

Aggregations

DynamicContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder)4 XPath2Expression (org.eclipse.wst.xml.xpath2.api.XPath2Expression)3 Engine (org.eclipse.wst.xml.xpath2.processor.Engine)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 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)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 DefaultDynamicContext (org.eclipse.wst.xml.xpath2.processor.DefaultDynamicContext)1 FnFunctionLibrary (org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary)1 XSCtrLibrary (org.eclipse.wst.xml.xpath2.processor.function.XSCtrLibrary)1 XercesTypeModel (org.eclipse.wst.xml.xpath2.processor.internal.types.xerces.XercesTypeModel)1 Document (org.w3c.dom.Document)1