Search in sources :

Example 1 with XmlObject

use of org.opennms.protocols.xml.config.XmlObject in project opennms by OpenNMS.

the class AbstractVTDXmlCollectionHandler method fillCollectionSet.

/**
     * Fill collection set.
     *
     * @param agent the agent
     * @param collectionSet the collection set
     * @param source the source
     * @param document the document
     * @throws ParseException the parse exception
     * @throws XPathParseException the x-path parse exception
     * @throws XPathEvalException the x-path evaluation exception
     * @throws NavException the navigation exception
     */
protected void fillCollectionSet(CollectionAgent agent, CollectionSetBuilder builder, XmlSource source, VTDNav document) throws ParseException, XPathParseException, XPathEvalException, NavException {
    AutoPilot resAP = new AutoPilot(document);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(document, group);
        resAP.selectXPath(group.getResourceXpath());
        while (resAP.evalXPath() != -1) {
            String resourceName = getResourceName(document, group);
            LOG.debug("fillCollectionSet: processing XML resource {}", resourceName);
            final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
            LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
            for (XmlObject object : group.getXmlObjects()) {
                document.push();
                AutoPilot ap = new AutoPilot();
                ap.bind(document);
                ap.selectXPath(object.getXpath());
                String value = ap.evalXPathToString();
                document.pop();
                builder.withAttribute(collectionResource, group.getName(), object.getName(), value, object.getDataType());
            }
            processXmlResource(builder, collectionResource, resourceName, group.getName());
        }
    }
}
Also used : XmlGroup(org.opennms.protocols.xml.config.XmlGroup) AutoPilot(com.ximpleware.AutoPilot) Resource(org.opennms.netmgt.collection.support.builder.Resource) XmlObject(org.opennms.protocols.xml.config.XmlObject) Date(java.util.Date)

Example 2 with XmlObject

use of org.opennms.protocols.xml.config.XmlObject in project opennms by OpenNMS.

the class HttpCollectionHandler method fillCollectionSet.

@Override
protected void fillCollectionSet(String urlString, Request request, CollectionAgent agent, CollectionSetBuilder builder, XmlSource source) throws Exception {
    Document doc = getJsoupDocument(urlString, request);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using selector {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(doc, group);
        Elements elements = doc.select(group.getResourceXpath());
        LOG.debug("fillCollectionSet: {} => {}", group.getResourceXpath(), elements);
        String resourceName = getResourceName(elements, group);
        LOG.debug("fillCollectionSet: processing XML resource {}", resourceName);
        final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
        LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
        for (XmlObject object : group.getXmlObjects()) {
            Elements el = elements.select(object.getXpath());
            if (el == null) {
                LOG.info("No value found for object named '{}'. Skipping.", object.getName());
            }
            builder.withAttribute(collectionResource, group.getName(), object.getName(), el.html(), object.getDataType());
        }
        processXmlResource(builder, collectionResource, resourceName, group.getName());
    }
}
Also used : XmlGroup(org.opennms.protocols.xml.config.XmlGroup) Resource(org.opennms.netmgt.collection.support.builder.Resource) XmlObject(org.opennms.protocols.xml.config.XmlObject) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date)

Example 3 with XmlObject

use of org.opennms.protocols.xml.config.XmlObject in project opennms by OpenNMS.

the class AbstractXmlCollectionHandler method fillCollectionSet.

/**
     * Fill collection set.
     *
     * @param agent the agent
     * @param collectionSet the collection set
     * @param source the source
     * @param doc the doc
     * @throws XPathExpressionException the x path expression exception
     * @throws ParseException the parse exception
     */
protected void fillCollectionSet(CollectionAgent agent, CollectionSetBuilder builder, XmlSource source, Document doc) throws XPathExpressionException, ParseException {
    NamespaceContext nc = new DocumentNamespaceResolver(doc);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(nc);
    for (XmlGroup group : source.getXmlGroups()) {
        LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath());
        Date timestamp = getTimeStamp(doc, xpath, group);
        NodeList resourceList = (NodeList) xpath.evaluate(group.getResourceXpath(), doc, XPathConstants.NODESET);
        for (int j = 0; j < resourceList.getLength(); j++) {
            Node resource = resourceList.item(j);
            String resourceName = getResourceName(xpath, group, resource);
            final Resource collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp);
            LOG.debug("fillCollectionSet: processing resource {}", collectionResource);
            for (XmlObject object : group.getXmlObjects()) {
                String value = (String) xpath.evaluate(object.getXpath(), resource, XPathConstants.STRING);
                builder.withAttribute(collectionResource, group.getName(), object.getName(), value, object.getDataType());
            }
            processXmlResource(builder, collectionResource, resourceName, group.getName());
        }
    }
    LOG.debug("fillCollectionSet: finishing collection set with {} resources and {} attributes on {}", builder.getNumResources(), builder.getNumAttributes(), agent);
}
Also used : XPath(javax.xml.xpath.XPath) XmlGroup(org.opennms.protocols.xml.config.XmlGroup) NamespaceContext(javax.xml.namespace.NamespaceContext) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) XmlObject(org.opennms.protocols.xml.config.XmlObject) Date(java.util.Date)

Example 4 with XmlObject

use of org.opennms.protocols.xml.config.XmlObject 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

Date (java.util.Date)4 Resource (org.opennms.netmgt.collection.support.builder.Resource)4 XmlGroup (org.opennms.protocols.xml.config.XmlGroup)4 XmlObject (org.opennms.protocols.xml.config.XmlObject)4 AutoPilot (com.ximpleware.AutoPilot)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XPath (javax.xml.xpath.XPath)1 JSONObject (net.sf.json.JSONObject)1 JXPathContext (org.apache.commons.jxpath.JXPathContext)1 JXPathException (org.apache.commons.jxpath.JXPathException)1 Pointer (org.apache.commons.jxpath.Pointer)1 Document (org.jsoup.nodes.Document)1 Elements (org.jsoup.select.Elements)1 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)1 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1