use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.
the class TestGetNamespaceContext method runTest.
@Override
protected void runTest() throws Throwable {
OMElement element = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<a xmlns='urn:ns1' xmlns:ns2='urn:ns2'><b xmlns:ns3='urn:ns3'/></a>");
XMLStreamReader stream = cache ? element.getXMLStreamReader() : element.getXMLStreamReaderWithoutCaching();
stream.next();
assertEquals(XMLStreamReader.START_ELEMENT, stream.next());
assertEquals("b", stream.getLocalName());
NamespaceContext context = stream.getNamespaceContext();
assertEquals("urn:ns1", context.getNamespaceURI(""));
assertEquals("urn:ns2", context.getNamespaceURI("ns2"));
assertEquals("urn:ns3", context.getNamespaceURI("ns3"));
assertEquals("ns2", context.getPrefix("urn:ns2"));
element.close(false);
}
use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.
the class OMSerializerUtil method isAssociated.
/**
* @param prefix
* @param namespace
* @param writer
* @return true if the prefix is associated with the namespace in the current context
*/
public static boolean isAssociated(String prefix, String namespace, XMLStreamWriter writer) throws XMLStreamException {
// of this issue.
if ("xml".equals(prefix)) {
return true;
}
// NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
// Please use other writer methods first.
// For consistency, convert null arguments.
// This helps get around the parser implementation differences.
// In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
prefix = (prefix == null) ? "" : prefix;
namespace = (namespace == null) ? "" : namespace;
if (namespace.length() > 0) {
// QUALIFIED NAMESPACE
// Get the namespace associated with the prefix
String writerPrefix = writer.getPrefix(namespace);
if (prefix.equals(writerPrefix)) {
return true;
}
// So try getting the namespace as a second step.
if (writerPrefix != null) {
NamespaceContext nsContext = writer.getNamespaceContext();
if (nsContext != null) {
String writerNS = nsContext.getNamespaceURI(prefix);
return namespace.equals(writerNS);
}
}
return false;
} else {
// Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
if (prefix.length() > 0) {
throw new OMException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.");
}
// protected
try {
String writerPrefix = writer.getPrefix("");
if (writerPrefix != null && writerPrefix.length() == 0) {
return true;
}
} catch (Throwable t) {
if (log.isDebugEnabled()) {
log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
}
}
// Fallback to using the namespace context
NamespaceContext nsContext = writer.getNamespaceContext();
if (nsContext != null) {
String writerNS = nsContext.getNamespaceURI("");
if (writerNS != null && writerNS.length() > 0) {
return false;
}
}
return true;
}
}
use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.
the class TestGetXMLStreamReaderWithPreserveNamespaceContext method runTest.
@Override
protected void runTest() throws Throwable {
InputStream in = TestGetXMLStreamReaderWithPreserveNamespaceContext.class.getResourceAsStream("AXIOM-114.xml");
OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocumentElement();
root.declareNamespace("http://example.org", "p");
OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
configuration.setPreserveNamespaceContext(preserveNamespaceContext);
XMLStreamReader reader = root.getFirstElement().getFirstElement().getXMLStreamReader(cache, configuration);
assertThat(reader.next()).isEqualTo(XMLStreamReader.START_ELEMENT);
Set<String> prefixes = new HashSet<>();
for (int i = 0; i < reader.getNamespaceCount(); i++) {
prefixes.add(reader.getNamespacePrefix(i));
}
if (preserveNamespaceContext) {
assertThat(prefixes).containsExactly("soapenv", "xsd", "xsi", "ns", "p");
} else {
assertThat(prefixes).containsExactly("ns");
}
// Make sure that we start pulling events directly from the underlying parser.
reader.nextTag();
// The following assertions are true irrespective of the value of preserveNamespaceContext.
assertThat(reader.getNamespaceURI("xsd")).isEqualTo("http://www.w3.org/2001/XMLSchema");
// Namespace declarations added programmatically on an ancestor should also be visible.
assertThat(reader.getNamespaceURI("p")).isEqualTo("http://example.org");
NamespaceContext nc = reader.getNamespaceContext();
assertThat(nc.getPrefix("http://www.w3.org/2001/XMLSchema")).isEqualTo("xsd");
assertThat(nc.getPrefix("http://example.org")).isEqualTo("p");
}
use of javax.xml.namespace.NamespaceContext in project ddf by codice.
the class MockNamespaceResolver method getNamespaceURI.
public String getNamespaceURI(String prefix) {
String methodName = "getNamespaceURI";
LOGGER.trace("ENTERING: {}", methodName);
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 ddf by codice.
the class MockNamespaceResolver method getPrefix.
public String getPrefix(String namespace) {
String methodName = "getPrefix";
LOGGER.trace("ENTERING: {} namespace = {}", methodName, namespace);
String prefix = null;
for (NamespaceContext nc : namespaceContexts) {
prefix = nc.getPrefix(namespace);
if (prefix != null) {
break;
}
}
LOGGER.trace("EXITING: {} (prefix = {} )", methodName, prefix);
return prefix;
}
Aggregations