use of javax.xml.namespace.NamespaceContext in project ddf by codice.
the class NamespaceResolver method getNamespaceContexts.
private void getNamespaceContexts() {
// Determine the OSGi bundle context for the NamespaceResolver
if (this.bundleContext == null) {
LOGGER.debug("Setting bundleContext");
this.bundleContext = BundleReference.class.cast(this.getClass().getClassLoader()).getBundle().getBundleContext();
}
namespaceContexts = new ArrayList<NamespaceContext>();
if (bundleContext != null) {
ServiceReference[] refs = null;
try {
// Retrieve all of the namespace mappings from the OSGi Service Registry
refs = bundleContext.getServiceReferences(NamespaceContext.class.getName(), null);
LOGGER.debug("num NamespaceContexts service refs found = {}", refs.length);
} catch (InvalidSyntaxException e) {
LOGGER.debug("Invalid NamespaceContext syntax", e);
}
// If no NamespaceMaps found, nothing further to be done
if (refs == null || refs.length == 0) {
LOGGER.debug("No NamespaceContext services found");
} else {
// maintained for prefix-to-uri and uri=to-prefix
for (ServiceReference ref : refs) {
NamespaceContext namespaceContext = (NamespaceContext) bundleContext.getService(ref);
if (namespaceContext != null) {
namespaceContexts.add(namespaceContext);
} else {
LOGGER.debug("NamespaceContext for ServiceReference was null");
}
}
}
} else {
LOGGER.debug("BundleContext is NULL");
}
}
use of javax.xml.namespace.NamespaceContext in project ddf by codice.
the class NamespaceResolver method getNamespaceURI.
/**
* Retrieve the namespace URI for the given namespace prefix. Prefix is retrieved from the list
* of namespace URIs and prefixes mapped from NamespaceMap entries in the OSGi Service Registry.
*
* @parameter prefix the namespace prefix to look up the URI for
*
* @return the namespace URI for the given namespace prefix
*/
public String getNamespaceURI(String prefix) {
String methodName = "getNamespaceURI";
LOGGER.trace("ENTERING: {}", methodName);
getNamespaceContexts();
String namespaceUri = null;
for (NamespaceContext nc : namespaceContexts) {
namespaceUri = nc.getNamespaceURI(prefix);
if (namespaceUri != null) {
break;
}
}
LOGGER.trace("EXITING: {} (namespaceUri = {})", methodName, namespaceUri);
return namespaceUri;
}
use of javax.xml.namespace.NamespaceContext in project camel by apache.
the class XAdESSignaturePropertiesTest method getXpath.
static XPathExpression getXpath(String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException {
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
NamespaceContext nc = new NamespaceContext() {
@SuppressWarnings("rawtypes")
@Override
public Iterator getPrefixes(String namespaceURI) {
return null;
}
@Override
public String getPrefix(String namespaceURI) {
return null;
}
@Override
public String getNamespaceURI(String prefix) {
return prefix2Namespace.get(prefix);
}
};
xpath.setNamespaceContext(nc);
XPathExpression expr = xpath.compile(xpathString);
return expr;
}
use of javax.xml.namespace.NamespaceContext in project camel by apache.
the class XmlSignatureTest method checkXpath.
private Object checkXpath(MockEndpoint mock, String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
Message mess = getMessage(mock);
InputStream body = mess.getBody(InputStream.class);
assertNotNull(body);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
NamespaceContext nc = new NamespaceContext() {
@SuppressWarnings("rawtypes")
@Override
public Iterator getPrefixes(String namespaceURI) {
return null;
}
@Override
public String getPrefix(String namespaceURI) {
return null;
}
@Override
public String getNamespaceURI(String prefix) {
return prefix2Namespace.get(prefix);
}
};
xpath.setNamespaceContext(nc);
XPathExpression expr = xpath.compile(xpathString);
Object result = expr.evaluate(XmlSignatureHelper.newDocumentBuilder(true).parse(body), XPathConstants.NODE);
assertNotNull("The xpath " + xpathString + " returned a null value", result);
return result;
}
use of javax.xml.namespace.NamespaceContext in project openhab1-addons by openhab.
the class IhcResourceInteractionService method parseList.
private NodeList parseList(String xml, String xpathExpression) throws XPathExpressionException, UnsupportedEncodingException {
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource inputSource = new InputSource(is);
xpath.setNamespaceContext(new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new NullPointerException("Null prefix");
} else if ("SOAP-ENV".equals(prefix)) {
return "http://schemas.xmlsoap.org/soap/envelope/";
} else if ("ns1".equals(prefix)) {
return "utcs";
} else if ("ns2".equals(prefix)) {
return "utcs.values";
}
return null;
}
@Override
public String getPrefix(String uri) {
return null;
}
@Override
@SuppressWarnings("rawtypes")
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
});
return (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
}
Aggregations