Search in sources :

Example 16 with NamespaceContext

use of javax.xml.namespace.NamespaceContext 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 17 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project camel by apache.

the class MockEndpointFixture method assertMessageReceived.

protected void assertMessageReceived(Document aExpectedDoc, Document aActual) throws Exception, XPathExpressionException {
    Document noTime = XmlFixture.stripTimestamp(aActual);
    Document noUUID = XmlFixture.stripUUID(noTime);
    XmlFixture.assertXMLIgnorePrefix("failed to match", aExpectedDoc, noUUID);
    // assert that we have a timestamp and datetime
    // can't rely on the datetime being the same due to timezone differences
    // instead, we'll assert that the values exist.
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    xp.setNamespaceContext(new NamespaceContext() {

        public String getNamespaceURI(String aArg0) {
            return "urn:org.apache.camel.component:jmx";
        }

        public String getPrefix(String aArg0) {
            return "jmx";
        }

        public Iterator<Object> getPrefixes(String aArg0) {
            return null;
        }
    });
    assertEquals("1", xp.evaluate("count(//jmx:timestamp)", aActual));
    assertEquals("1", xp.evaluate("count(//jmx:dateTime)", aActual));
    resetMockEndpoint();
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator) Document(org.w3c.dom.Document)

Example 18 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project uPortal by Jasig.

the class PortletWindowRegistryImpl method addPortletWindowId.

protected StartElement addPortletWindowId(StartElement element, IPortletWindowId portletWindowId) {
    final Attribute windowIdAttribute = xmlEventFactory.createAttribute(PORTLET_WINDOW_ID_ATTR_NAME, portletWindowId.getStringId());
    //Clone the start element to add the new attribute
    final QName name = element.getName();
    final String prefix = name.getPrefix();
    final String namespaceURI = name.getNamespaceURI();
    final String localPart = name.getLocalPart();
    @SuppressWarnings("unchecked") final Iterator<Attribute> attributes = element.getAttributes();
    @SuppressWarnings("unchecked") final Iterator<Namespace> namespaces = element.getNamespaces();
    final NamespaceContext namespaceContext = element.getNamespaceContext();
    //Create a new iterator of the existing attributes + the new window id attribute
    final Iterator<Attribute> newAttributes = Iterators.concat(attributes, Iterators.forArray(windowIdAttribute));
    return xmlEventFactory.createStartElement(prefix, namespaceURI, localPart, newAttributes, namespaces, namespaceContext);
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) NamespaceContext(javax.xml.namespace.NamespaceContext) Namespace(javax.xml.stream.events.Namespace)

Example 19 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project Tundra by Permafrost.

the class xml method parse.

public static final void parse(IData pipeline) throws ServiceException {
    // --- <<IS-START(parse)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [o] record:0:optional $document
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        if (node != null)
            IDataHelper.put(cursor, "$document", NodeHelper.parse(node, namespace, true));
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset)

Example 20 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.

the class TestGetNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = TestGetNamespaceContext.class.getResourceAsStream("namespacecontext.xml");
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocumentElement();
    OMElement inner = root.getFirstElement().getFirstElement();
    NamespaceContext context = inner.getNamespaceContext(detached);
    assertEquals("urn:test2", context.getNamespaceURI("p"));
    assertEquals("urn:test3", context.getNamespaceURI("q"));
    assertEquals("urn:test3", context.getNamespaceURI("r"));
    assertEquals("urn:test4", context.getNamespaceURI(""));
    assertEquals("", context.getNamespaceURI("unbound"));
    assertNull(context.getPrefix("urn:test1"));
    assertEquals("p", context.getPrefix("urn:test2"));
    String prefix = context.getPrefix("urn:test3");
    assertTrue(prefix.equals("q") || prefix.equals("r"));
    assertEquals("", context.getPrefix("urn:test4"));
    assertNull(context.getPrefix("unbound"));
    Iterator<?> it = context.getPrefixes("urn:test1");
    assertFalse(it.hasNext());
    it = context.getPrefixes("urn:test2");
    assertTrue(it.hasNext());
    assertEquals("p", it.next());
    assertFalse(it.hasNext());
    it = context.getPrefixes("urn:test3");
    Set<String> prefixes = new HashSet<>();
    while (it.hasNext()) {
        prefixes.add((String) it.next());
    }
    assertEquals(2, prefixes.size());
    assertTrue(prefixes.contains("q"));
    assertTrue(prefixes.contains("r"));
}
Also used : InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement) HashSet(java.util.HashSet)

Aggregations

NamespaceContext (javax.xml.namespace.NamespaceContext)23 XPath (javax.xml.xpath.XPath)7 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 XPathExpression (javax.xml.xpath.XPathExpression)5 InputSource (org.xml.sax.InputSource)5 OMElement (org.apache.axiom.om.OMElement)4 Node (org.w3c.dom.Node)4 Charset (java.nio.charset.Charset)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 XPathFactory (javax.xml.xpath.XPathFactory)3 IDataNamespaceContext (permafrost.tundra.xml.namespace.IDataNamespaceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashSet (java.util.HashSet)2 OMXMLStreamReaderConfiguration (org.apache.axiom.om.OMXMLStreamReaderConfiguration)2 NodeList (org.w3c.dom.NodeList)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 QName (javax.xml.namespace.QName)1