use of org.eclipse.wst.xml.xpath2.processor.internal.ast.Step 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.internal.ast.Step in project webtools.sourceediting by eclipse.
the class Normalizer method make_descendant_or_self.
private XPathExpr make_descendant_or_self() {
Step desc_self_node = new ForwardStep(ForwardStep.DESCENDANT_OR_SELF, new AnyKindTest());
StepExpr se = new AxisStep(desc_self_node, new ArrayList());
return new XPathExpr(0, se);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.ast.Step in project webtools.sourceediting by eclipse.
the class Normalizer method make_root_self_node.
private XPathExpr make_root_self_node() {
// self::node()
Step self_node = new ForwardStep(ForwardStep.SELF, new AnyKindTest());
StepExpr self_node_expr = new AxisStep(self_node, new ArrayList());
XPathExpr self_node_xpath = new XPathExpr(0, self_node_expr);
// fn:root(self::node())
Collection args = new ArrayList();
args.add(self_node_xpath);
XPathExpr xpe = make_function(new QName("fn", "root", FnFunctionLibrary.XPATH_FUNCTIONS_NS), args);
return xpe;
}
use of org.eclipse.wst.xml.xpath2.processor.internal.ast.Step in project webtools.sourceediting by eclipse.
the class AxesTest method test_Axes088.
// Use of // to get all elements of a given name.
// Not a valid XPath 2 script
// public void test_Axes085() throws Exception {
// String inputFile = "/TestSources/nw_Customers.xml";
// String xqFile = "/Queries/XQuery/Expressions/PathExpr/Steps/Axes/Axes085.xq";
// String resultFile = "/ExpectedTestResults/Expressions/PathExpr/Steps/Axes/Axes085.xml";
// String expectedResult = getExpectedResult(resultFile);
// URL fileURL = bundle.getEntry(inputFile);
// loadDOMDocument(fileURL);
//
// // Get XML Schema Information for the Document
// XSModel schema = getGrammar();
//
// setupDynamicContext(schema);
//
// String xpath = extractXPathExpression(xqFile, inputFile);
// String actual = null;
// try {
// XPath path = compileXPath(xpath);
//
// Evaluator eval = new DefaultEvaluator(dc, domDoc);
// ResultSequence rs = eval.evaluate(path);
//
// actual = buildResultString(rs);
//
// } catch (XPathParserException ex) {
// actual = ex.code();
// } catch (StaticError ex) {
// actual = ex.code();
// } catch (DynamicError ex) {
// actual = ex.code();
// }
//
// assertEquals("XPath Result Error " + xqFile + ":", expectedResult, actual);
//
//
// }
// Parent of attribute node.
// public void test_Axes086() throws Exception {
// String inputFile = "/TestSources/Tree1Text.xml";
// String xqFile = "/Queries/XQuery/Expressions/PathExpr/Steps/Axes/Axes086.xq";
// String resultFile = "/ExpectedTestResults/Expressions/PathExpr/Steps/Axes/Axes086.xml";
// String expectedResult = getExpectedResult(resultFile);
// URL fileURL = bundle.getEntry(inputFile);
// loadDOMDocument(fileURL);
//
// // Get XML Schema Information for the Document
// XSModel schema = getGrammar();
//
// setupDynamicContext(schema);
//
// String xpath = extractXPathExpression(xqFile, inputFile);
// String actual = null;
// try {
// XPath path = compileXPath(xpath);
//
// Evaluator eval = new DefaultEvaluator(dc, domDoc);
// ResultSequence rs = eval.evaluate(path);
//
// actual = buildResultString(rs);
//
// } catch (XPathParserException ex) {
// actual = ex.code();
// } catch (StaticError ex) {
// actual = ex.code();
// } catch (DynamicError ex) {
// actual = ex.code();
// }
//
// assertEquals("XPath Result Error " + xqFile + ":", expectedResult, actual);
//
//
// }
// Parent of text nodes.
// public void test_Axes087() throws Exception {
// String inputFile = "/TestSources/xq311B.xml";
// String xqFile = "/Queries/XQuery/Expressions/PathExpr/Steps/Axes/Axes087.xq";
// String resultFile = "/ExpectedTestResults/Expressions/PathExpr/Steps/Axes/Axes087.xml";
// String expectedResult = getExpectedResult(resultFile);
// URL fileURL = bundle.getEntry(inputFile);
// loadDOMDocument(fileURL);
//
// // Get XML Schema Information for the Document
// XSModel schema = getGrammar();
//
// setupDynamicContext(schema);
//
// String xpath = extractXPathExpression(xqFile, inputFile);
// String actual = null;
// try {
// XPath path = compileXPath(xpath);
//
// Evaluator eval = new DefaultEvaluator(dc, domDoc);
// ResultSequence rs = eval.evaluate(path);
//
// actual = buildResultString(rs);
//
// } catch (XPathParserException ex) {
// actual = ex.code();
// } catch (StaticError ex) {
// actual = ex.code();
// } catch (DynamicError ex) {
// actual = ex.code();
// }
//
// assertEquals("XPath Result Error " + xqFile + ":", expectedResult, actual);
//
//
// }
// Empty step should result in parse error.
public void test_Axes088() throws Exception {
String inputFile = "/TestSources/emptydoc.xml";
String xqFile = "/Queries/XQuery/Expressions/PathExpr/Steps/Axes/Axes088.xq";
String expectedResult = "XPST0003";
URL fileURL = bundle.getEntry(inputFile);
loadDOMDocument(fileURL);
// Get XML Schema Information for the Document
XSModel schema = getGrammar();
setupDynamicContext(schema);
String xpath = extractXPathExpression(xqFile, inputFile);
String actual = null;
try {
compileXPath(xpath);
ResultSequence rs = evaluate(domDoc);
actual = buildResultString(rs);
} catch (XPathParserException ex) {
actual = ex.code();
} catch (StaticError ex) {
actual = ex.code();
} catch (DynamicError ex) {
actual = ex.code();
}
assertEquals("XPath Result Error " + xqFile + ":", expectedResult, actual);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.ast.Step in project webtools.sourceediting by eclipse.
the class DefaultEvaluator method visit.
/**
* visit XPath expression
*
* @param e
* is the XPath expression.
* @return a new function
*/
public Object visit(XPathExpr e) {
XPathExpr xp = e;
ResultSequence rs = null;
Focus original_focus = focus();
// do all the steps
while (xp != null) {
StepExpr se = xp.expr();
if (se != null) {
// this is not the first step
if (rs != null) {
// results...
if (rs.size() == 0)
break;
// nodes!
for (Iterator i = rs.iterator(); i.hasNext(); ) {
AnyType item = (AnyType) i.next();
if (!(item instanceof NodeType)) {
report_error(TypeError.step_conatins_atoms(null));
// unreach
return null;
}
}
// check if we got a //
if (xp.slashes() == 2) {
rs = descendant_or_self_node(rs);
if (rs.size() == 0)
break;
}
// make result of previous step the new
// focus
set_focus(new Focus(rs));
// do the step for all item in context
rs = do_step(se);
} else // this is first step...
// note... we may be called from upstream...
// like in the expression sorbo/*[2] ... we may
// be called to evaluate the 2... the caller
// will iterate through the whole outer focus
// for us
{
// XXX ???
if (xp.slashes() == 1) {
rs = root_self_node();
set_focus(new Focus(rs));
rs = do_step(se);
} else if (xp.slashes() == 2) {
rs = root_self_node();
rs = descendant_or_self_node(rs);
set_focus(new Focus(rs));
rs = do_step(se);
} else
rs = (ResultSequence) se.accept(this);
}
} else // the expression is "/"
{
assert xp.slashes() == 1;
rs = root_self_node();
}
xp = xp.next();
}
// restore focus
set_focus(original_focus);
return rs;
}
Aggregations