use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.
the class TestGetXMLStreamReaderWithNamespaceURIInterning method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
// Use "new String" to create String objects that are not interned
OMNamespace ns1 = factory.createOMNamespace(new String("urn:ns1"), "p");
OMNamespace ns2 = factory.createOMNamespace(new String("urn:ns2"), "q");
OMElement root = factory.createOMElement("root", ns1);
root.addAttribute("attr", "value", ns2);
factory.createOMElement("child", ns2, root);
OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
configuration.setNamespaceURIInterning(true);
XMLStreamReader reader = root.getXMLStreamReader(true, configuration);
reader.nextTag();
assertInterned(reader.getNamespaceURI());
assertInterned(reader.getAttributeNamespace(0));
for (int i = 0; i < reader.getNamespaceCount(); i++) {
assertInterned(reader.getNamespaceURI(i));
}
reader.nextTag();
assertInterned(reader.getNamespaceURI("p"));
NamespaceContext nc = reader.getNamespaceContext();
assertInterned(nc.getNamespaceURI("p"));
}
use of javax.xml.namespace.NamespaceContext in project simba-os by cegeka.
the class Utils method query.
/**
* Extracts a node from the DOMDocument
*
* @param dom The DOMDocument
* @param query Xpath Expresion
* @param context Context Node (DomElement)
* @return DOMNodeList The queried node
* @throws XPathExpressionException
*/
public static NodeList query(Document dom, String query, Node context) throws XPathExpressionException {
NodeList nodeList;
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
String result = null;
switch(prefix) {
case "samlp":
case "samlp2":
result = SAMLConstants.NS_SAMLP;
break;
case "saml":
case "saml2":
result = SAMLConstants.NS_SAML;
break;
case "ds":
result = SAMLConstants.NS_DS;
break;
case "xenc":
result = SAMLConstants.NS_XENC;
break;
}
return result;
}
public String getPrefix(String namespaceURI) {
return null;
}
@SuppressWarnings("rawtypes")
public Iterator getPrefixes(String namespaceURI) {
return null;
}
});
if (context == null)
nodeList = (NodeList) xpath.evaluate(query, dom, XPathConstants.NODESET);
else
nodeList = (NodeList) xpath.evaluate(query, context, XPathConstants.NODESET);
return nodeList;
}
use of javax.xml.namespace.NamespaceContext in project ddf by codice.
the class NamespaceResolver method getPrefix.
/**
* Retrieve the namespace prefix for the given namespace URI. URI is retrieved from the list of
* namespace URIs and prefixes mapped from NamespaceMap entries in the OSGi Service Registry.
*
* @parameter namespace the namespace URI to look up the prefix for
*
* @return the namespace prefix for the given namespace URI
*/
public String getPrefix(String namespace) {
String methodName = "getPrefix";
LOGGER.trace("ENTERING: {}, namespace = {}", methodName, namespace);
getNamespaceContexts();
String prefix = null;
for (NamespaceContext nc : namespaceContexts) {
prefix = nc.getPrefix(namespace);
if (prefix != null) {
break;
}
}
LOGGER.trace("EXITING: {} (prefix = {})", methodName, prefix);
return prefix;
}
Aggregations