Search in sources :

Example 86 with AnyType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathWTPTest method buildXMLResultString.

protected String buildXMLResultString(ResultSequence rs) throws Exception {
    DOMImplementationLS domLS = (DOMImplementationLS) domDoc.getImplementation().getFeature("LS", "3.0");
    LSOutput outputText = domLS.createLSOutput();
    LSSerializer serializer = domLS.createLSSerializer();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputText.setByteStream(outputStream);
    String actual = new String();
    Iterator iterator = rs.iterator();
    boolean queueSpace = false;
    while (iterator.hasNext()) {
        AnyType aat = (AnyType) iterator.next();
        if (aat instanceof NodeType) {
            NodeType nodeType = (NodeType) aat;
            Node node = nodeType.node_value();
            serializer.write(node, outputText);
            queueSpace = false;
        } else {
            if (queueSpace)
                outputText.getByteStream().write(32);
            outputText.getByteStream().write(aat.string_value().getBytes("UTF-8"));
            queueSpace = true;
        }
    }
    actual = outputStream.toString("UTF-8");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", "");
    outputStream.close();
    return actual.trim();
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) LSSerializer(org.w3c.dom.ls.LSSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LSOutput(org.w3c.dom.ls.LSOutput) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 87 with AnyType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType in project webtools.sourceediting by eclipse.

the class TestWTPDOMXPath2 method testSimpleWTPDOM.

public void testSimpleWTPDOM() throws Exception {
    // Test for the fix, for xpathDefaultNamespace
    bundle = Platform.getBundle("org.eclipse.wst.xml.xpath2.wtptypes.tests");
    URL fileURL = bundle.getEntry("/bugTestFiles/bug273719.xml");
    super.domDoc = load(fileURL);
    // set up XPath default namespace in Dynamic Context
    DynamicContext dc = setupDynamicContext(null);
    String xpath = "string-length(x) > 2";
    XPath path = compileXPath(dc, xpath);
    Evaluator eval = new DefaultEvaluator(dc, domDoc);
    ResultSequence rs = eval.evaluate(path);
    AnyType result = rs.first();
    String actual = result.string_value();
    assertEquals("true", actual);
}
Also used : XPath(org.eclipse.wst.xml.xpath2.processor.ast.XPath) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) DefaultEvaluator(org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator) Evaluator(org.eclipse.wst.xml.xpath2.processor.Evaluator) DefaultEvaluator(org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType) URL(java.net.URL) DynamicContext(org.eclipse.wst.xml.xpath2.processor.DynamicContext)

Example 88 with AnyType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method evaluateSimpleXPath.

protected AnyType evaluateSimpleXPath(String xpath, Document doc, Class resultClass) {
    try {
        compileXPath(xpath);
    } catch (XPathParserException e) {
        throw new RuntimeException("XPath parse: " + e.getMessage(), e);
    } catch (StaticError e) {
        throw new RuntimeException("Static error: " + e.getMessage(), e);
    }
    ResultSequence rs;
    try {
        rs = evaluate(domDoc);
    } catch (DynamicError e) {
        throw new RuntimeException("Evaluation error: " + e.getMessage(), e);
    }
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    AnyType result = rs.first();
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return result;
}
Also used : XPathParserException(org.eclipse.wst.xml.xpath2.processor.XPathParserException) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) StaticError(org.eclipse.wst.xml.xpath2.processor.StaticError) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 89 with AnyType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method buildResultString.

protected String buildResultString(ResultSequence rs) {
    String actual = new String();
    Iterator iterator = rs.iterator();
    while (iterator.hasNext()) {
        AnyType anyType = (AnyType) iterator.next();
        actual = actual + anyType.getStringValue() + " ";
    }
    return actual.trim();
}
Also used : Iterator(java.util.Iterator) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 90 with AnyType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType 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

AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)162 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)127 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)123 XPathParserException (org.eclipse.wst.xml.xpath2.processor.XPathParserException)123 URL (java.net.URL)122 XSModel (org.apache.xerces.xs.XSModel)121 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)40 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)38 Iterator (java.util.Iterator)37 Item (org.eclipse.wst.xml.xpath2.api.Item)26 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)14 NodeType (org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType)14 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)11 AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)9 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)9 Collection (java.util.Collection)8 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)8 ListIterator (java.util.ListIterator)7 ArrayList (java.util.ArrayList)6 NumericType (org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType)6