Search in sources :

Example 1 with RoundBracketNode

use of com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.RoundBracketNode in project service-proxy by membrane.

the class SimpleXPathAnalyzer method getElement.

/**
 * Checks whether a given expression requires the existence of a named element.
 * @return The named element (including its namespace) or null if there is no such element.
 */
public QName getElement(ContainerNode intersectExceptExpr) {
    for (String op : new String[] { "intersect", "except", "instance", "treat", "castable", "cast", "+", "-" }) for (Node n : intersectExceptExpr.nodes) if (n instanceof UnparsedStringNode)
        if (indexOfOperand(((UnparsedStringNode) n).s, op) != -1)
            return null;
    // check whether intersectExceptExpr starts with '//' + name
    if (intersectExceptExpr.nodes.length == 0)
        return null;
    if (!(intersectExceptExpr.nodes[0] instanceof UnparsedStringNode))
        return null;
    Marker m = new Marker(((UnparsedStringNode) intersectExceptExpr.nodes[0]).s);
    skipWhitespace(m);
    if (eatChar(m) != '/')
        return null;
    if (eatChar(m) != '/')
        return null;
    skipWhitespace(m);
    String elementName = getName(m);
    if (elementName == null) {
        if (m.s.codePointAt(m.p) != '*')
            return null;
        elementName = "*";
        m.p++;
    }
    if (elementName.contains(":"))
        throw new RuntimeException("Namespace prefixes are not allowed in XPath expressions here, since they are not declared. Use '//*[local-name()='foo' and namespace-uri()='http://bar.com/'] instead");
    skipWhitespace(m);
    // if '[' + expr + ']' follows
    if (m.isAtEnd() && intersectExceptExpr.nodes.length > 1 && intersectExceptExpr.nodes[1] instanceof SquareBracketNode) {
        ContainerNode predicate = ((SquareBracketNode) intersectExceptExpr.nodes[1]).node;
        // expr matches "local-name() = 'foo'"
        if (predicate.nodes.length == 4 && predicate.nodes[0] instanceof UnparsedStringNode && predicate.nodes[1] instanceof RoundBracketNode && ((RoundBracketNode) predicate.nodes[1]).node.nodes.length == 0 && predicate.nodes[2] instanceof UnparsedStringNode && predicate.nodes[3] instanceof StringNode) {
            Marker m2 = new Marker(((UnparsedStringNode) predicate.nodes[0]).s);
            String string1 = ((StringNode) predicate.nodes[3]).s;
            skipWhitespace(m2);
            String function = getName(m2);
            if (m2.isAtEnd()) {
                if ("namespace-uri".equals(function))
                    return new QName(string1, elementName);
                if ("local-name".equals(function) && elementName.equals("*"))
                    return new QName(string1);
            }
        }
        // expr matches "local-name() = 'foo' and namespace-uri
        if ("*".equals(elementName) && predicate.nodes.length == 8 && predicate.nodes[0] instanceof UnparsedStringNode && predicate.nodes[1] instanceof RoundBracketNode && ((RoundBracketNode) predicate.nodes[1]).node.nodes.length == 0 && predicate.nodes[2] instanceof UnparsedStringNode && predicate.nodes[3] instanceof StringNode && predicate.nodes[4] instanceof UnparsedStringNode && predicate.nodes[5] instanceof RoundBracketNode && ((RoundBracketNode) predicate.nodes[5]).node.nodes.length == 0 && predicate.nodes[6] instanceof UnparsedStringNode && predicate.nodes[7] instanceof StringNode) {
            Marker m2 = new Marker(((UnparsedStringNode) predicate.nodes[0]).s);
            String string1 = ((StringNode) predicate.nodes[3]).s;
            skipWhitespace(m2);
            String function = getName(m2);
            if (m2.isAtEnd() && "local-name".equals(function)) {
                Marker m3 = new Marker(((UnparsedStringNode) predicate.nodes[4]).s);
                String string2 = ((StringNode) predicate.nodes[7]).s;
                skipWhitespace(m3);
                String and = getName(m3);
                if ("and".equals(and)) {
                    skipWhitespace(m3);
                    String function2 = getName(m3);
                    if (m3.isAtEnd() && "namespace-uri".equals(function2)) {
                        return new QName(string2, string1);
                    }
                }
            }
        }
    }
    return "*".equals(elementName) ? null : new QName(elementName);
}
Also used : SquareBracketNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.SquareBracketNode) UnparsedStringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode) QName(javax.xml.namespace.QName) SquareBracketNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.SquareBracketNode) UnparsedStringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode) RoundBracketNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.RoundBracketNode) Node(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.Node) StringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.StringNode) ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode) UnparsedStringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode) StringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.StringNode) ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode) RoundBracketNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.RoundBracketNode)

Aggregations

ContainerNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode)1 Node (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.Node)1 RoundBracketNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.RoundBracketNode)1 SquareBracketNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.SquareBracketNode)1 StringNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.StringNode)1 UnparsedStringNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode)1 QName (javax.xml.namespace.QName)1