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);
}
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();
}
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);
}
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>> ---
}
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"));
}
Aggregations