Search in sources :

Example 6 with StaticContext

use of org.eclipse.wst.xml.xpath2.api.StaticContext in project webtools.sourceediting by eclipse.

the class AttributeTest method createAttrType.

private AnyType createAttrType(Item at, StaticContext sc) {
    anyType = new AttrType();
    NodeType nodeType = (NodeType) at;
    Node node = nodeType.node_value();
    if (node == null) {
        return anyType;
    }
    String nodeName = node.getLocalName();
    if (wild()) {
        if (type() != null) {
            anyType = createAttrForXSDType(node, sc);
        }
    } else if (nodeName.equals(name().local())) {
        if (type() != null) {
            anyType = createAttrForXSDType(node, sc);
        } else {
            anyType = new AttrType((Attr) node, sc.getTypeModel());
        }
    }
    return anyType;
}
Also used : AttrType(org.eclipse.wst.xml.xpath2.processor.internal.types.AttrType) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) Node(org.w3c.dom.Node)

Example 7 with StaticContext

use of org.eclipse.wst.xml.xpath2.api.StaticContext in project webtools.sourceediting by eclipse.

the class FilteringPerformanceTest method evalXPath2.

protected Object evalXPath2(String xpath, Node doc, Class resultClass) {
    StaticContext sc = new StaticContextBuilder();
    XPath2Expression path = new Engine().parseExpression(xpath, sc);
    DynamicContext dynamicContext = new DynamicContextBuilder(sc);
    long before = System.nanoTime();
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    Object result = rs.value(0);
    long after = System.nanoTime();
    System.out.println("XPath2 " + xpath + " evaluated to " + result + " in " + (after - before) / 1000 + " μs");
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return resultClass.cast(result);
}
Also used : StaticContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) StaticContext(org.eclipse.wst.xml.xpath2.api.StaticContext) DynamicContext(org.eclipse.wst.xml.xpath2.api.DynamicContext)

Example 8 with StaticContext

use of org.eclipse.wst.xml.xpath2.api.StaticContext in project webtools.sourceediting by eclipse.

the class TestBugs method testNulVarNamespacePrefix.

public void testNulVarNamespacePrefix() throws Exception {
    // Bug 345326
    String xpath = "for $a in //* return $a";
    new Engine().parseExpression(xpath, new StaticContext() {

        public boolean isXPath1Compatible() {
            return false;
        }

        public StaticVariableResolver getInScopeVariables() {
            return null;
        }

        public TypeDefinition getInitialContextType() {
            return null;
        }

        public Map getFunctionLibraries() {
            return null;
        }

        public CollationProvider getCollationProvider() {
            return null;
        }

        public URI getBaseUri() {
            return null;
        }

        public ItemType getDocumentType(URI documentUri) {
            return null;
        }

        public NamespaceContext getNamespaceContext() {
            return new NamespaceContext() {

                public String getNamespaceURI(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return XMLConstants.NULL_NS_URI;
                }

                public String getPrefix(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return XMLConstants.DEFAULT_NS_PREFIX;
                }

                public Iterator getPrefixes(String arg0) {
                    if (arg0 == null)
                        throw new IllegalArgumentException("#fail");
                    return Collections.emptyList().iterator();
                }
            };
        }

        public String getDefaultNamespace() {
            return XMLConstants.NULL_NS_URI;
        }

        public String getDefaultFunctionNamespace() {
            return null;
        }

        public TypeModel getTypeModel() {
            return null;
        }

        public Function resolveFunction(QName name, int arity) {
            return null;
        }

        public TypeDefinition getCollectionType(String collectionName) {
            return null;
        }

        public TypeDefinition getDefaultCollectionType() {
            return null;
        }
    });
}
Also used : StaticVariableResolver(org.eclipse.wst.xml.xpath2.api.StaticVariableResolver) QName(javax.xml.namespace.QName) ItemType(org.eclipse.wst.xml.xpath2.api.typesystem.ItemType) TypeModel(org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) URI(java.net.URI) TypeDefinition(org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition) Function(org.eclipse.wst.xml.xpath2.api.Function) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator) CollationProvider(org.eclipse.wst.xml.xpath2.api.CollationProvider) Map(java.util.Map) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) StaticContext(org.eclipse.wst.xml.xpath2.api.StaticContext)

Example 9 with StaticContext

use of org.eclipse.wst.xml.xpath2.api.StaticContext in project webtools.sourceediting by eclipse.

the class CompleteNewApiTest method evaluateSimpleXPath.

protected Object evaluateSimpleXPath(String xpath, StaticContext sc, Document doc, Class resultClass) {
    XPath2Expression path = new Engine().parseExpression(xpath, sc);
    DynamicContext dynamicContext = new DynamicContextBuilder(sc);
    org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    Object result = rs.value(0);
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return result;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) DynamicContext(org.eclipse.wst.xml.xpath2.api.DynamicContext)

Example 10 with StaticContext

use of org.eclipse.wst.xml.xpath2.api.StaticContext in project webtools.sourceediting by eclipse.

the class FnQName method resolve_QName.

/**
 * Resolve the QName of the given arguments.
 *
 * @param args
 *            Result from teh expressions evaluation.
 * @param sc
 *            Result of static context operation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the fn:QName operation.
 */
public static ResultSequence resolve_QName(Collection args, StaticContext sc) throws DynamicError {
    Collection cargs = Function.convert_arguments(args, expected_args());
    // get args
    Iterator argiter = cargs.iterator();
    ResultSequence arg1 = (ResultSequence) argiter.next();
    String ns = null;
    if (!arg1.empty())
        ns = ((XSString) arg1.first()).value();
    ResultSequence arg2 = (ResultSequence) argiter.next();
    String name = ((XSString) arg2.first()).value();
    QName qn = QName.parse_QName(name);
    if (qn == null)
        throw DynamicError.lexical_error(null);
    qn.set_namespace(ns);
    return qn;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) Iterator(java.util.Iterator) Collection(java.util.Collection) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)

Aggregations

ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)5 Collection (java.util.Collection)4 Iterator (java.util.Iterator)4 TypeDefinition (org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition)3 TypeModel (org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel)3 Engine (org.eclipse.wst.xml.xpath2.processor.Engine)3 ElementType (org.eclipse.wst.xml.xpath2.processor.internal.types.ElementType)3 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)3 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)3 Map (java.util.Map)2 DynamicContext (org.eclipse.wst.xml.xpath2.api.DynamicContext)2 StaticContext (org.eclipse.wst.xml.xpath2.api.StaticContext)2 XPath2Expression (org.eclipse.wst.xml.xpath2.api.XPath2Expression)2 AttrType (org.eclipse.wst.xml.xpath2.processor.internal.types.AttrType)2 NodeType (org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType)2 DynamicContextBuilder (org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 URI (java.net.URI)1 Calendar (java.util.Calendar)1