Search in sources :

Example 21 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 22 with JXPathContext

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

the class JsonCollectorArrayIT method testXpath.

@Test
@SuppressWarnings("unchecked")
public void testXpath() throws Exception {
    JSONObject json = MockDocumentBuilder.getJSONDocument();
    JXPathContext context = JXPathContext.newContext(json);
    Iterator<Pointer> itr = context.iteratePointers("/elements[4]/it");
    Assert.assertTrue(itr.hasNext());
    Assert.assertEquals(itr.next().getValue(), "works");
    Assert.assertFalse(itr.hasNext());
}
Also used : JSONObject(net.sf.json.JSONObject) JXPathContext(org.apache.commons.jxpath.JXPathContext) Pointer(org.apache.commons.jxpath.Pointer) Test(org.junit.Test)

Example 23 with JXPathContext

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

the class JXPathUtils method newContext.

public static JXPathContext newContext(final Node node) {
    final JXPathContext ctx = JXPathContext.newContext(node);
    ctx.setLenient(true);
    ctx.setFunctions(new ClassFunctions(ResolveFunctions.class, "ext"));
    return ctx;
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) ClassFunctions(org.apache.commons.jxpath.ClassFunctions)

Example 24 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 25 with JXPathContext

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

the class AbstractJsonCollectionHandler method fillCollectionSet.

/**
 * Fill collection set.
 *
 * @param agent the agent
 * @param collectionSet the collection set
 * @param source the source
 * @param json the JSON Object
 * @throws ParseException the parse exception
 */
@SuppressWarnings("unchecked")
protected void fillCollectionSet(CollectionAgent agent, CollectionSetBuilder builder, XmlSource source, JSONObject json) throws ParseException {
    JXPathContext context = JXPathContext.newContext(json);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(context, group);
        Iterator<Pointer> itr = context.iteratePointers(group.getResourceXpath());
        while (itr.hasNext()) {
            JXPathContext relativeContext = context.getRelativeContext(itr.next());
            String resourceName = getResourceName(relativeContext, group);
            LOG.debug("fillCollectionSet: processing XML resource {} of type {}", resourceName, group.getResourceType());
            final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
            LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
            for (XmlObject object : group.getXmlObjects()) {
                try {
                    Object obj = relativeContext.getValue(object.getXpath());
                    if (obj != null) {
                        builder.withAttribute(collectionResource, group.getName(), object.getName(), obj.toString(), object.getDataType());
                    }
                } catch (JXPathException ex) {
                    LOG.warn("Unable to get value for {}: {}", object.getXpath(), ex.getMessage());
                }
            }
            processXmlResource(builder, collectionResource, resourceName, group.getName());
        }
    }
}
Also used : XmlGroup(org.opennms.protocols.xml.config.XmlGroup) JXPathContext(org.apache.commons.jxpath.JXPathContext) Resource(org.opennms.netmgt.collection.support.builder.Resource) JXPathException(org.apache.commons.jxpath.JXPathException) Pointer(org.apache.commons.jxpath.Pointer) XmlObject(org.opennms.protocols.xml.config.XmlObject) XmlObject(org.opennms.protocols.xml.config.XmlObject) JSONObject(net.sf.json.JSONObject) Date(java.util.Date)

Aggregations

JXPathContext (org.apache.commons.jxpath.JXPathContext)37 Element (org.jdom.Element)12 JXPathException (org.apache.commons.jxpath.JXPathException)7 List (java.util.List)6 ArrayList (java.util.ArrayList)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 JXPathInvalidSyntaxException (org.apache.commons.jxpath.JXPathInvalidSyntaxException)3 Pointer (org.apache.commons.jxpath.Pointer)3 HttpResponse (org.apache.http.HttpResponse)3 StatusLine (org.apache.http.StatusLine)3 HttpClient (org.apache.http.client.HttpClient)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)3 Test (org.junit.Test)3 ModelJXPathContext (org.openforis.idm.model.expression.internal.ModelJXPathContext)3