use of org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr 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.XPathExpr in project webtools.sourceediting by eclipse.
the class Normalizer method make_function.
private XPathExpr make_function(QName name, Collection args) {
FunctionCall fc = new FunctionCall(name, args);
FilterExpr fe = new FilterExpr(fc, new ArrayList());
return new XPathExpr(0, fe);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr 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.XPathExpr 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.XPathExpr in project webtools.sourceediting by eclipse.
the class Normalizer method visit.
/**
* @param e
* is the xpath expression.
* @return result.
*/
public Object visit(XPathExpr e) {
XPathExpr xp = e;
// indicates how many / we traversed
int depth = 0;
XPathExpr result = e;
while (xp != null) {
int slashes = xp.slashes();
StepExpr se = xp.expr();
if (slashes == 1) {
// this is a single slash and nothing else...
if (se == null)
return make_root_self_node();
// /RelativePathExpr
if (depth == 0) {
XPathExpr xpe = make_root_self_node();
xpe.set_next(e);
result = xpe;
}
}
if (slashes == 2) {
// //RelativePathExpr
if (depth == 0) {
XPathExpr desc = make_descendant_or_self();
desc.set_slashes(1);
e.set_slashes(1);
desc.set_next(e);
XPathExpr root_self = make_root_self_node();
root_self.set_next(desc);
return root_self;
}
}
if (se != null)
se.accept(this);
XPathExpr next = xp.next();
// peek if the next guy will have 2 slashes...
if (next != null) {
// StepExpr//StepExpr
if (next.slashes() == 2) {
// create the node to stick between the
// slashes
XPathExpr desc = make_descendant_or_self();
desc.set_slashes(1);
// current node / desc / next
xp.set_next(desc);
desc.set_next(next);
next.set_slashes(1);
}
}
xp = next;
depth++;
}
return result;
}
Aggregations