Search in sources :

Example 21 with NodeType

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

the class DefaultEvaluator method visit.

/**
 * visit a name test expression
 *
 * @param e
 *            is thename test.
 * @return a result sequence
 */
public Object visit(NameTest e) {
    QName name = e.name();
    // get the arguments
    Pair arg = (Pair) _param;
    String type = (String) arg._one;
    ResultSequence rs = (ResultSequence) arg._two;
    ResultBuffer rb = new ResultBuffer();
    for (Iterator i = rs.iterator(); i.hasNext(); ) {
        NodeType nt = (NodeType) i.next();
        // check if node passes name test
        if (name_test(nt, name, type))
            rb.add(nt);
    }
    rs = rb.getSequence();
    arg._two = rs;
    return rs;
}
Also used : ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) VarExprPair(org.eclipse.wst.xml.xpath2.processor.internal.ast.VarExprPair)

Example 22 with NodeType

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

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

Example 24 with NodeType

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

the class DefaultEvaluator method do_step.

// this will evaluate the step expression for the whole focus and return
// the result.
// 
// i.e. It will execute the step expression for each item in the focus
// [each time changing the context item].
private ResultSequence do_step(StepExpr se) {
    ResultBuffer rs = new ResultBuffer();
    ArrayList results = new ArrayList();
    // 0: don't know yet
    int type = 0;
    // 1: atomic
    // 2: node
    Focus focus = focus();
    int original_pos = focus.position();
    // execute step for all items in focus
    while (true) {
        results.add(se.accept(this));
        // go to next
        if (!focus.advance_cp())
            break;
    }
    // make sure we didn't change focus
    focus.set_position(original_pos);
    boolean node_types = false;
    // check the results
    for (Iterator i = results.iterator(); i.hasNext(); ) {
        ResultSequence result = (ResultSequence) i.next();
        // make sure results are of same type, and add them in
        for (Iterator j = result.iterator(); j.hasNext(); ) {
            AnyType item = (AnyType) j.next();
            // first item
            if (type == 0) {
                if (item instanceof AnyAtomicType)
                    type = 1;
                else if (item instanceof NodeType)
                    type = 2;
                else
                    assert false;
            }
            // make sure we got coherent types
            switch(type) {
                // atomic... just concat
                case 1:
                    if (!(item instanceof AnyAtomicType))
                        report_error(TypeError.mixed_vals(null));
                    rs.add(item);
                    break;
                case 2:
                    node_types = true;
                    if (!(item instanceof NodeType))
                        report_error(TypeError.mixed_vals(null));
                    rs.add(item);
                    break;
                default:
                    assert false;
            }
        }
    }
    // XXX lame
    if (node_types) {
        rs = NodeType.linarize(rs);
    }
    return rs.getSequence();
}
Also used : ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) Focus(org.eclipse.wst.xml.xpath2.processor.internal.Focus) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 25 with NodeType

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

the class DefaultEvaluator method derivesFrom.

private boolean derivesFrom(NodeType at, QName et) {
    TypeDefinition td = _sc.getTypeModel().getType(at.node_value());
    short method = TypeDefinition.DERIVATION_EXTENSION | TypeDefinition.DERIVATION_RESTRICTION;
    return td != null && td.derivedFrom(et.namespace(), et.local(), method);
}
Also used : TypeDefinition(org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition)

Aggregations

NodeType (org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType)39 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)23 Iterator (java.util.Iterator)21 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)15 Collection (java.util.Collection)12 Node (org.w3c.dom.Node)12 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)11 ListIterator (java.util.ListIterator)10 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)9 ElementType (org.eclipse.wst.xml.xpath2.processor.internal.types.ElementType)9 VarExprPair (org.eclipse.wst.xml.xpath2.processor.internal.ast.VarExprPair)7 TypeDefinition (org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition)6 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)5 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)5 ArrayList (java.util.ArrayList)4 Attr (org.w3c.dom.Attr)4 ForwardAxis (org.eclipse.wst.xml.xpath2.processor.internal.ForwardAxis)3 ParentAxis (org.eclipse.wst.xml.xpath2.processor.internal.ParentAxis)3 ReverseAxis (org.eclipse.wst.xml.xpath2.processor.internal.ReverseAxis)3 AttrType (org.eclipse.wst.xml.xpath2.processor.internal.types.AttrType)3