void removeNamespace(Namespace namespace) { Namespace current = getNodeNamespace(); // Do not remove in-use namespace if (namespace.is(current)) return; NamedNodeMap attrs = this.dom.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { XmlNode attr = XmlNode.createImpl(attrs.item(i)); if (namespace.is(attr.getNodeNamespace())) return; } // TODO I must confess I am not sure I understand the spec fully. See ECMA357 13.4.4.31 String existingPrefix = getExistingPrefixFor(namespace); if (existingPrefix != null) { if (namespace.isUnspecifiedPrefix()) { // we should remove any namespace with this URI from scope; we do this by declaring a // namespace with the same // prefix as the existing prefix and setting its URI to the default namespace declareNamespace(existingPrefix, getDefaultNamespace().getUri()); } else { if (existingPrefix.equals(namespace.getPrefix())) { declareNamespace(existingPrefix, getDefaultNamespace().getUri()); } } } else { // the argument namespace is not declared in this scope, so do nothing. } }
void declareNamespace(String prefix, String uri) { if (!(dom instanceof Element)) throw new IllegalStateException(); if (dom.lookupNamespaceURI(uri) != null && dom.lookupNamespaceURI(uri).equals(prefix)) { // do nothing } else { Element e = (Element) dom; declareNamespace(e, prefix, uri); } }
String ecmaToXMLString(XmlProcessor processor) { if (this.isElementType()) { Element copy = (Element) this.dom.cloneNode(true); Namespace[] inScope = this.getInScopeNamespaces(); for (int i = 0; i < inScope.length; i++) { declareNamespace(copy, inScope[i].getPrefix(), inScope[i].getUri()); } return processor.ecmaToXmlString(copy); } else { return processor.ecmaToXmlString(dom); } }