InputStream in = getTestResource(testResource);
Set/*<String>*/ prefixes = new HashSet();
Set/*<String>*/ namespaceURIs = new HashSet();
prefixes.add("");
try {
ScopedNamespaceContext refNc = new ScopedNamespaceContext();
XMLStreamReader reader = factory.createXMLStreamReader(in);
int depth = 0;
int eventType;
while ((eventType = reader.getEventType()) != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_ELEMENT) {
refNc.startScope();
for (int i=0; i<reader.getNamespaceCount(); i++) {
String prefix = reader.getNamespacePrefix(i);
String uri = reader.getNamespaceURI(i);
refNc.setPrefix(prefix == null ? "" : prefix,
uri == null ? "" : uri);
if (prefix != null) {
prefixes.add(prefix);
}
if (uri != null && uri.length() > 0) {
namespaceURIs.add(uri);
}
}
depth++;
}
if (depth > 0) {
NamespaceContext nc = reader.getNamespaceContext();
for (Iterator it = prefixes.iterator(); it.hasNext(); ) {
String prefix = (String)it.next();
String expectedUri = refNc.getNamespaceURI(prefix);
String actualUri = nc.getNamespaceURI(prefix);
assertEquals("Namespace URI for prefix '" + prefix + "'", expectedUri, actualUri);
}
for (Iterator it = namespaceURIs.iterator(); it.hasNext(); ) {
String namespaceURI = (String)it.next();
assertEquals(
"Prefix for namespace URI '" + namespaceURI + "'",
refNc.getPrefix(namespaceURI),
nc.getPrefix(namespaceURI));
assertEquals(
"Prefixes for namespace URI '" + namespaceURI + "'",
toPrefixSet(refNc.getPrefixes(namespaceURI)),
toPrefixSet(nc.getPrefixes(namespaceURI)));
}
}
if (eventType == XMLStreamReader.END_ELEMENT) {
refNc.endScope();
depth--;
}
reader.next();
}
} finally {