Search in sources :

Example 6 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project freud by LMAX-Exchange.

the class CssRuleJdom method parseDeclarations.

@SuppressWarnings("unchecked")
private void parseDeclarations() {
    cssDeclarationList = new ArrayList<CssDeclaration>();
    JXPathContext context = JXPathContext.newContext(ruleElement);
    List<Element> cssSelectorElementList = (List<Element>) context.selectNodes("/" + CssTokenType.PROPERTY.name());
    for (Element element : cssSelectorElementList) {
        cssDeclarationList.add(new CssDeclarationJdom(this, element));
    }
}
Also used : CssDeclaration(org.freud.analysed.css.rule.declaration.CssDeclaration) JXPathContext(org.apache.commons.jxpath.JXPathContext) Element(org.jdom.Element) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList)

Example 7 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project opennms by OpenNMS.

the class JsonCollectorSolarisZonesIT method testXpath.

/**
     * Test to verify XPath content.
     *
     * @throws Exception the exception
     */
@Test
@SuppressWarnings("unchecked")
public void testXpath() throws Exception {
    JSONObject json = MockDocumentBuilder.getJSONDocument();
    JXPathContext context = JXPathContext.newContext(json);
    Iterator<Pointer> itr = context.iteratePointers("/zones/zone");
    while (itr.hasNext()) {
        Pointer resPtr = itr.next();
        JXPathContext relativeContext = context.getRelativeContext(resPtr);
        String resourceName = (String) relativeContext.getValue("@name");
        Assert.assertNotNull(resourceName);
        String value = (String) relativeContext.getValue("parameter[@key='nproc']/@value");
        Assert.assertNotNull(Integer.valueOf(value));
    }
}
Also used : JSONObject(net.sf.json.JSONObject) JXPathContext(org.apache.commons.jxpath.JXPathContext) Pointer(org.apache.commons.jxpath.Pointer) Test(org.junit.Test)

Example 8 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project galley by Commonjava.

the class JXPathContextAncestryTest method basicJXPathTest.

@Test
@Ignore
public void basicJXPathTest() throws Exception {
    final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jxpath/simple.pom.xml");
    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
    final JXPathContext ctx = JXPathContext.newContext(document);
    document.getDocumentElement().removeAttribute("xmlns");
    final String projectGroupIdPath = "ancestor::project/groupId";
    // NOT what's failing...just populating the node set to traverse in order to feed the ancestor:: axis test.
    final List<?> nodes = ctx.selectNodes("/project/dependencies/dependency");
    for (final Object object : nodes) {
        final Node node = (Node) object;
        dump(node);
        final Stack<Node> revPath = new Stack<Node>();
        Node parent = node;
        while (parent != null) {
            revPath.push(parent);
            parent = parent.getParentNode();
        }
        JXPathContext nodeCtx = null;
        while (!revPath.isEmpty()) {
            final Node part = revPath.pop();
            if (nodeCtx == null) {
                nodeCtx = JXPathContext.newContext(part);
            } else {
                nodeCtx = JXPathContext.newContext(nodeCtx, part);
            }
        }
        System.out.println("Path derived from context: '" + nodeCtx.getNamespaceContextPointer().asPath() + "'");
        // brute-force approach...try to force population of the parent pointers by painstakingly constructing contexts for all intermediate nodes.
        System.out.println("Selecting groupId for declaring project using path-derived context...");
        System.out.println(nodeCtx.getValue(projectGroupIdPath));
        // Naive approach...this has all the context info it needs to get parent contexts up to and including the document!
        System.out.println("Selecting groupId for declaring project using non-derived context...");
        System.out.println(JXPathContext.newContext(node).getValue(projectGroupIdPath));
    }
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Stack(java.util.Stack) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project camel by apache.

the class JXPathExpression method evaluate.

public <T> T evaluate(Exchange exchange, Class<T> tClass) {
    try {
        JXPathContext context = JXPathContext.newContext(exchange);
        context.setLenient(lenient);
        Object result = getJXPathExpression().getValue(context, type);
        assertResultType(exchange, result);
        return exchange.getContext().getTypeConverter().convertTo(tClass, result);
    } catch (JXPathException e) {
        throw new ExpressionEvaluationException(this, exchange, e);
    }
}
Also used : ExpressionEvaluationException(org.apache.camel.ExpressionEvaluationException) JXPathContext(org.apache.commons.jxpath.JXPathContext) JXPathException(org.apache.commons.jxpath.JXPathException)

Example 10 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project freud by LMAX-Exchange.

the class AnnotationJdom method getAnnotationValueForElement.

private String getAnnotationValueForElement(final Element element) {
    final JXPathContext context = JXPathContext.newContext(element);
    final Element expr = (Element) context.selectSingleNode("/" + JavaSourceTokenType.EXPR.name() + "/*");
    if (expr != null) {
        return expr.getText();
    } else {
        final List<Element> exprList = context.selectNodes("//" + JavaSourceTokenType.EXPR.name() + "/*");
        StringBuilder sb = new StringBuilder("{");
        for (Element item : exprList) {
            sb.append(item.getText()).append(",");
        }
        sb.setCharAt(sb.length() - 1, '}');
        return sb.toString();
    }
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) Element(org.jdom.Element)

Aggregations

JXPathContext (org.apache.commons.jxpath.JXPathContext)19 Element (org.jdom.Element)12 List (java.util.List)6 JXPathException (org.apache.commons.jxpath.JXPathException)6 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)3 JSONObject (net.sf.json.JSONObject)3 Pointer (org.apache.commons.jxpath.Pointer)3 Test (org.junit.Test)3 Collections.emptyList (java.util.Collections.emptyList)2 JavaSourceTokenType (org.freud.analysed.javasource.parser.JavaSourceTokenType)2 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Stack (java.util.Stack)1 ExpressionEvaluationException (org.apache.camel.ExpressionEvaluationException)1 ClassFunctions (org.apache.commons.jxpath.ClassFunctions)1 CssRule (org.freud.analysed.css.rule.CssRule)1 CssDeclaration (org.freud.analysed.css.rule.declaration.CssDeclaration)1 Annotation (org.freud.analysed.javasource.Annotation)1 ClassDeclaration (org.freud.analysed.javasource.ClassDeclaration)1