Search in sources :

Example 1 with Pointer

use of org.apache.commons.jxpath.Pointer 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 2 with Pointer

use of org.apache.commons.jxpath.Pointer 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 3 with Pointer

use of org.apache.commons.jxpath.Pointer 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

JSONObject (net.sf.json.JSONObject)3 JXPathContext (org.apache.commons.jxpath.JXPathContext)3 Pointer (org.apache.commons.jxpath.Pointer)3 Test (org.junit.Test)2 Date (java.util.Date)1 JXPathException (org.apache.commons.jxpath.JXPathException)1 Resource (org.opennms.netmgt.collection.support.builder.Resource)1 XmlGroup (org.opennms.protocols.xml.config.XmlGroup)1 XmlObject (org.opennms.protocols.xml.config.XmlObject)1