Search in sources :

Example 1 with Engine

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

the class InternalXPathParser method parse.

/**
 * Tries to parse the xpath expression
 *
 * @param xpath
 *            is the xpath string.
 * @param isRootlessAccess
 *            if 'true' then PsychoPath engine can't parse xpath expressions starting with / or //.
 * @throws XPathParserException.
 * @return the xpath value.
 * @since 2.0
 */
public XPath parse(String xpath, boolean isRootlessAccess) throws XPathParserException {
    XPathFlex lexer = new XPathFlex(new StringReader(xpath));
    try {
        XPathCup p = new XPathCup(lexer);
        Symbol res = p.parse();
        XPath xPath2 = (XPath) res.value;
        if (isRootlessAccess) {
            xPath2.accept(new DefaultVisitor() {

                public Object visit(XPathExpr e) {
                    if (e.slashes() > 0) {
                        throw new XPathParserException("Access to root node is not allowed (set by caller)");
                    }
                    do {
                        // check the single step (may have filter with root access)
                        e.expr().accept(this);
                        // follow singly linked list of the path, it's all relative past the first one
                        e = e.next();
                    } while (e != null);
                    return null;
                }
            });
        }
        return xPath2;
    } catch (JFlexError e) {
        throw new XPathParserException("JFlex lexer error: " + e.reason());
    } catch (CupError e) {
        throw new XPathParserException("CUP parser error: " + e.reason());
    } catch (StaticError e) {
        throw new XPathParserException(e.code(), e.getMessage());
    } catch (Exception e) {
        throw new XPathParserException(e.getMessage());
    }
}
Also used : XPath(org.eclipse.wst.xml.xpath2.processor.ast.XPath) XPathParserException(org.eclipse.wst.xml.xpath2.processor.XPathParserException) Symbol(java_cup.runtime.Symbol) XPathParserException(org.eclipse.wst.xml.xpath2.processor.XPathParserException) StringReader(java.io.StringReader) StaticError(org.eclipse.wst.xml.xpath2.processor.StaticError) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Example 2 with Engine

use of org.eclipse.wst.xml.xpath2.processor.Engine 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 3 with Engine

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

the class TestBugs method testNulVarNamespacePrefix.

public void testNulVarNamespacePrefix() throws Exception {
    // Bug 345326
    String xpath = "for $a in //* return $a";
    new Engine().parseExpression(xpath, new StaticContext() {

        public boolean isXPath1Compatible() {
            return false;
        }

        public StaticVariableResolver getInScopeVariables() {
            return null;
        }

        public TypeDefinition getInitialContextType() {
            return null;
        }

        public Map getFunctionLibraries() {
            return null;
        }

        public CollationProvider getCollationProvider() {
            return null;
        }

        public URI getBaseUri() {
            return null;
        }

        public ItemType getDocumentType(URI documentUri) {
            return null;
        }

        public NamespaceContext getNamespaceContext() {
            return new NamespaceContext() {

                public String getNamespaceURI(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return XMLConstants.NULL_NS_URI;
                }

                public String getPrefix(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return XMLConstants.DEFAULT_NS_PREFIX;
                }

                public Iterator getPrefixes(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return Collections.emptyList().iterator();
                }
            };
        }

        public String getDefaultNamespace() {
            return XMLConstants.NULL_NS_URI;
        }

        public String getDefaultFunctionNamespace() {
            return null;
        }

        public TypeModel getTypeModel() {
            return null;
        }

        public Function resolveFunction(QName name, int arity) {
            return null;
        }

        public TypeDefinition getCollectionType(String collectionName) {
            return null;
        }

        public TypeDefinition getDefaultCollectionType() {
            return null;
        }
    });
}
Also used : StaticVariableResolver(org.eclipse.wst.xml.xpath2.api.StaticVariableResolver) QName(javax.xml.namespace.QName) ItemType(org.eclipse.wst.xml.xpath2.api.typesystem.ItemType) TypeModel(org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) URI(java.net.URI) TypeDefinition(org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition) Function(org.eclipse.wst.xml.xpath2.api.Function) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator) CollationProvider(org.eclipse.wst.xml.xpath2.api.CollationProvider) Map(java.util.Map) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) StaticContext(org.eclipse.wst.xml.xpath2.api.StaticContext)

Example 4 with Engine

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

the class XPathComputer method evaluateXPath.

protected IStatus evaluateXPath(SimpleXPathEngine engine) throws XPathExpressionException {
    IDOMDocument doc = null;
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (IDOMDocument) node;
    } else {
        doc = (IDOMDocument) node.getOwnerDocument();
    }
    updateNamespaces(engine, doc);
    try {
        this.nodeList = (NodeList) engine.execute(node);
        return Status.OK_STATUS;
    } catch (DynamicError de) {
        return new Status(IStatus.CANCEL, XPathCorePlugin.PLUGIN_ID, de.getMessage() + " (" + de.code() + ")");
    } catch (Throwable t) {
        return new Status(IStatus.ERROR, XPathCorePlugin.PLUGIN_ID, t.getMessage());
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError)

Example 5 with Engine

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

the class AbstractPsychoPathTest method compileXPath.

protected XPath compileXPath(String xpath) throws XPathParserException, StaticError {
    if (useNewApi) {
        newXPath = new Engine().parseExpression(xpath, staticContextBuilder);
        return null;
    } else {
        XPathParser xpp = new JFlexCupParser();
        XPath path = oldXPath = xpp.parse(xpath);
        StaticChecker name_check = new StaticNameResolver(dynamicContext);
        name_check.check(path);
        return path;
    }
}
Also used : XPath(org.eclipse.wst.xml.xpath2.processor.ast.XPath) StaticChecker(org.eclipse.wst.xml.xpath2.processor.StaticChecker) XPathParser(org.eclipse.wst.xml.xpath2.processor.XPathParser) StaticNameResolver(org.eclipse.wst.xml.xpath2.processor.StaticNameResolver) JFlexCupParser(org.eclipse.wst.xml.xpath2.processor.JFlexCupParser) Engine(org.eclipse.wst.xml.xpath2.processor.Engine)

Aggregations

Engine (org.eclipse.wst.xml.xpath2.processor.Engine)5 XPath2Expression (org.eclipse.wst.xml.xpath2.api.XPath2Expression)3 DynamicContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder)3 DynamicContext (org.eclipse.wst.xml.xpath2.api.DynamicContext)2 StaticContext (org.eclipse.wst.xml.xpath2.api.StaticContext)2 XPath (org.eclipse.wst.xml.xpath2.processor.ast.XPath)2 StaticContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Symbol (java_cup.runtime.Symbol)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 QName (javax.xml.namespace.QName)1 OMFactory (org.apache.axiom.om.OMFactory)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)1 CollationProvider (org.eclipse.wst.xml.xpath2.api.CollationProvider)1