Search in sources :

Example 1 with XPathExpr

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

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);
}
Also used : FilterExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.FilterExpr) ArrayList(java.util.ArrayList) FunctionCall(org.eclipse.wst.xml.xpath2.processor.internal.ast.FunctionCall) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Example 3 with XPathExpr

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);
}
Also used : ForwardStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep) StepExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr) ArrayList(java.util.ArrayList) AxisStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep) ForwardStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep) ReverseStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ReverseStep) Step(org.eclipse.wst.xml.xpath2.processor.internal.ast.Step) AxisStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep) AnyKindTest(org.eclipse.wst.xml.xpath2.processor.internal.ast.AnyKindTest) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Example 4 with XPathExpr

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;
}
Also used : ForwardStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) StepExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr) ArrayList(java.util.ArrayList) Collection(java.util.Collection) AxisStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep) ForwardStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep) ReverseStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.ReverseStep) Step(org.eclipse.wst.xml.xpath2.processor.internal.ast.Step) AxisStep(org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep) AnyKindTest(org.eclipse.wst.xml.xpath2.processor.internal.ast.AnyKindTest) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Example 5 with XPathExpr

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;
}
Also used : StepExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Aggregations

XPathExpr (org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)9 StepExpr (org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr)6 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)3 Iterator (java.util.Iterator)2 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)2 Focus (org.eclipse.wst.xml.xpath2.processor.internal.Focus)2 AnyKindTest (org.eclipse.wst.xml.xpath2.processor.internal.ast.AnyKindTest)2 AxisStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep)2 FilterExpr (org.eclipse.wst.xml.xpath2.processor.internal.ast.FilterExpr)2 ForwardStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep)2 ReverseStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.ReverseStep)2 Step (org.eclipse.wst.xml.xpath2.processor.internal.ast.Step)2 StringReader (java.io.StringReader)1 ListIterator (java.util.ListIterator)1 Symbol (java_cup.runtime.Symbol)1 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)1 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)1 XPathParserException (org.eclipse.wst.xml.xpath2.processor.XPathParserException)1 XPath (org.eclipse.wst.xml.xpath2.processor.ast.XPath)1