Search in sources :

Example 1 with Step

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

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);
}
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 3 with Step

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;
}
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 4 with Step

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);
}
Also used : XPathParserException(org.eclipse.wst.xml.xpath2.processor.XPathParserException) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) XSModel(org.apache.xerces.xs.XSModel) StaticError(org.eclipse.wst.xml.xpath2.processor.StaticError) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError) URL(java.net.URL)

Example 5 with Step

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;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Focus(org.eclipse.wst.xml.xpath2.processor.internal.Focus) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) StepExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType) XPathExpr(org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)

Aggregations

ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)5 AxisStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep)4 ForwardStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep)4 ReverseStep (org.eclipse.wst.xml.xpath2.processor.internal.ast.ReverseStep)4 Step (org.eclipse.wst.xml.xpath2.processor.internal.ast.Step)4 XPathExpr (org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr)4 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)4 NodeType (org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 ListIterator (java.util.ListIterator)3 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)3 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)3 XPathParserException (org.eclipse.wst.xml.xpath2.processor.XPathParserException)3 Focus (org.eclipse.wst.xml.xpath2.processor.internal.Focus)3 AnyKindTest (org.eclipse.wst.xml.xpath2.processor.internal.ast.AnyKindTest)3 StepExpr (org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr)3 URL (java.net.URL)2 Collection (java.util.Collection)2 XSModel (org.apache.xerces.xs.XSModel)2