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());
}
}
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);
}
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;
}
});
}
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());
}
}
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;
}
}
Aggregations