protected void fixupInsertedNamespaces(boolean inherit) {
if (parent.getNodeKind() == Type.DOCUMENT) {
return;
}
IntSet childNamespaces = new IntHashSet();
if (namespaceList != null) {
for (int i=0; i<namespaceList.length; i++) {
childNamespaces.add(namespaceList[i]);
}
}
NamespaceResolver inscope = new InscopeNamespaceResolver(parent);
NamePool pool = getNamePool();
// If the child is in the null namespace but the parent has a default namespace, xmlns="" should be added.
if (getURI().length()==0 && inscope.getURIForPrefix("", true).length()!=0) {
childNamespaces.add(0);
}
// Namespaces present on the parent but not on the child should be undeclared (if requested)
if (!inherit) {
Iterator it = inscope.iteratePrefixes();
while (it.hasNext()) {
String prefix = (String)it.next();
int prefixCode = pool.getCodeForPrefix(prefix)<<16;
boolean found = false;
if (namespaceList != null) {
for (int i=0; i<namespaceList.length; i++) {
if ((namespaceList[i] & 0xffff) == prefixCode) {
found = true;
break;
}
}
}
if (!found) {
childNamespaces.add(prefixCode);
}
}
}
// Redundant namespaces should be removed
if (namespaceList != null) {
for (int i=0; i<namespaceList.length; i++) {
int nscode = namespaceList[i];
String prefix = pool.getPrefixFromNamespaceCode(nscode);
String uri = pool.getURIFromNamespaceCode(nscode);
String parentUri = inscope.getURIForPrefix(prefix, true);
if (parentUri != null && parentUri.equals(uri)) {
// the namespace declaration is redundant
childNamespaces.remove(nscode);
}
}
}
int[] n2 = new int[childNamespaces.size()];
int j = 0;
IntIterator ii = childNamespaces.iterator();
while (ii.hasNext()) {
n2[j++] = ii.next();
}
namespaceList = n2;
}