Search in sources :

Example 1 with ContainerNode

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

the class XMLContentFilter method createElementFinder.

/**
 * Constructs an XMLElementFinder which can make a first decision whether a
 * given XPath expression has any chance of succeeding.
 *
 * This only works if the XPath expression is simple enough. (The XPath
 * expression must be a UnionExpr consisting of PathExprs, which start with
 * "//foo", optionally followed by "[namespace-uri()='http://bar/']").
 *
 * @return the xmlElementFinder as described above, or null if the XPath
 *         expression is too complex.
 */
static XMLElementFinder createElementFinder(String xPath) {
    SimpleXPathAnalyzer a = new SimpleXPathAnalyzer();
    List<ContainerNode> intersectExceptExprs = a.getIntersectExceptExprs(xPath);
    if (intersectExceptExprs == null)
        return null;
    List<QName> rootElements = new ArrayList<QName>();
    for (ContainerNode node : intersectExceptExprs) {
        QName n = a.getElement(node);
        if (n == null)
            return null;
        rootElements.add(n);
    }
    return new XMLElementFinder(rootElements);
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode)

Example 2 with ContainerNode

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

the class SimpleXPathParserTest method valid2.

@Test
public void valid2() {
    ContainerNode n = p.parse("//a[@b='c'] (: comment (: nested comment :) :) and (//d)");
    Assert.assertNotNull(n);
    Assert.assertEquals(6, n.nodes.length);
}
Also used : ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode) Test(org.junit.Test)

Example 3 with ContainerNode

use of com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode 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)

Example 4 with ContainerNode

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

the class SimpleXPathAnalyzer method splitUnionExprIntoIntersectExceptExprs.

private List<ContainerNode> splitUnionExprIntoIntersectExceptExprs(ContainerNode node) {
    List<ContainerNode> res = new ArrayList<ContainerNode>();
    List<Node> intersectExceptExprParts = new ArrayList<Node>();
    for (Node n : node.nodes) if (n instanceof UnparsedStringNode) {
        List<String> parts = new ArrayList<String>();
        for (String part : splitOnOperand(((UnparsedStringNode) n).s, "|")) for (String part2 : splitOnOperand(part, "union")) parts.add(part2);
        for (int i = 0; i < parts.size(); i++) {
            if (i >= 1) {
                // next IntersectExceptExpr
                res.add(new ContainerNode(intersectExceptExprParts.toArray(new Node[0])));
                intersectExceptExprParts = new ArrayList<Node>();
            }
            intersectExceptExprParts.add(new UnparsedStringNode(parts.get(i)));
        }
    } else {
        intersectExceptExprParts.add(n);
    }
    if (intersectExceptExprParts.size() > 0)
        res.add(new ContainerNode(intersectExceptExprParts.toArray(new Node[0])));
    return res;
}
Also used : UnparsedStringNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode) 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) ArrayList(java.util.ArrayList) ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ContainerNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode)4 Node (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.Node)2 RoundBracketNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.RoundBracketNode)2 SquareBracketNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.SquareBracketNode)2 StringNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.StringNode)2 UnparsedStringNode (com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.UnparsedStringNode)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 List (java.util.List)1 Test (org.junit.Test)1