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 lookupPrefix(org.w3c.dom.Node node) { if (node == null) throw new IllegalArgumentException("node must not be null"); String prefix = node.lookupPrefix(namespace.getUri()); if (prefix == null) { // check to see if we match the default namespace String defaultNamespace = node.lookupNamespaceURI(null); if (defaultNamespace == null) defaultNamespace = ""; String nodeNamespace = namespace.getUri(); if (nodeNamespace.equals(defaultNamespace)) { prefix = ""; } } int i = 0; while (prefix == null) { String generatedPrefix = "e4x_" + i++; String generatedUri = node.lookupNamespaceURI(generatedPrefix); if (generatedUri == null) { prefix = generatedPrefix; org.w3c.dom.Node top = node; while (top.getParentNode() != null && top.getParentNode() instanceof org.w3c.dom.Element) { top = top.getParentNode(); } ((org.w3c.dom.Element) top) .setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace.getUri()); } } namespace.setPrefix(prefix); }
static Namespace create(String prefix, String uri) { if (prefix == null) throw new IllegalArgumentException("Empty string represents default namespace prefix"); if (uri == null) throw new IllegalArgumentException("Namespace may not lack a URI"); Namespace rv = new Namespace(); rv.prefix = prefix; rv.uri = uri; return rv; }
private void exportToScope(boolean sealed) { xmlPrototype = newXML(XmlNode.createText(options, "")); xmlListPrototype = newXMLList(); namespacePrototype = Namespace.create(this.globalScope, null, XmlNode.Namespace.GLOBAL); qnamePrototype = QName.create( this, this.globalScope, null, XmlNode.QName.create(XmlNode.Namespace.create(""), "")); xmlPrototype.exportAsJSClass(sealed); xmlListPrototype.exportAsJSClass(sealed); namespacePrototype.exportAsJSClass(sealed); qnamePrototype.exportAsJSClass(sealed); }
Namespace[] getNamespaces() { Iterator i = map.keySet().iterator(); ArrayList rv = new ArrayList(); while (i.hasNext()) { String prefix = (String) i.next(); String uri = (String) map.get(prefix); Namespace n = Namespace.create(prefix, uri); if (!n.isEmpty()) { rv.add(n); } } return (Namespace[]) rv.toArray(new Namespace[0]); }
private Namespace getNodeNamespace() { String uri = dom.getNamespaceURI(); String prefix = dom.getPrefix(); if (uri == null) uri = ""; if (prefix == null) prefix = ""; return Namespace.create(prefix, uri); }
Namespace getNamespaceDeclaration(String prefix) { if (prefix.equals("") && dom instanceof Attr) { // Default namespaces do not apply to attributes; see XML Namespaces section 5.2 return Namespace.create("", ""); } Namespaces rv = getAllNamespaces(); return rv.getNamespace(prefix); }
String qualify(org.w3c.dom.Node node) { if (namespace.getPrefix() == null) { if (node != null) { lookupPrefix(node); } else { if (namespace.getUri().equals("")) { namespace.setPrefix(""); } else { // TODO I am not sure this is right, but if we are creating a standalone node, I // think we can set the // default namespace on the node itself and not worry about setting a prefix // for that namespace. namespace.setPrefix(""); } } } return qualify(namespace.getPrefix(), localName); }
private void addNamespaces(Namespaces rv, Element element) { if (element == null) throw new RuntimeException("element must not be null"); String myDefaultNamespace = toUri(element.lookupNamespaceURI(null)); String parentDefaultNamespace = ""; if (element.getParentNode() != null) { parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null)); } if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element)) { rv.declare(Namespace.create("", myDefaultNamespace)); } NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) { rv.declare(Namespace.create(attr.getLocalName(), attr.getValue())); } } }
private Namespaces getAllNamespaces() { Namespaces rv = new Namespaces(); Node target = this.dom; if (target instanceof Attr) { target = ((Attr) target).getOwnerElement(); } while (target != null) { if (target instanceof Element) { addNamespaces(rv, (Element) target); } target = target.getParentNode(); } // Fallback in case no namespace was declared rv.declare(Namespace.create("", "")); return rv; }
/** @deprecated Use getNamespace() */ String getPrefix() { return namespace.getPrefix(); }
/** @deprecated Use getNamespace() */ String getUri() { return namespace.getUri(); }
void setAttribute(org.w3c.dom.Element element, String value) { if (namespace.getPrefix() == null) lookupPrefix(element); element.setAttributeNS(namespace.getUri(), qualify(namespace.getPrefix(), localName), value); }
private boolean namespacesEqual(Namespace one, Namespace two) { if (one == null && two == null) return true; if (one == null || two == null) return false; return equals(one.getUri(), two.getUri()); }
/** @deprecated */ static QName create(String uri, String localName, String prefix) { return create(Namespace.create(prefix, uri), localName); }
static Namespace create(String uri) { Namespace rv = new Namespace(); rv.uri = uri; return rv; }
Namespace getNamespaceByUri(String uri) { if (uriToPrefix.get(uri) == null) return null; return Namespace.create(uri, (String) uriToPrefix.get(uri)); }
private String getExistingPrefixFor(Namespace namespace) { if (getDefaultNamespace().getUri().equals(namespace.getUri())) { return ""; } return dom.lookupPrefix(namespace.getUri()); }
private Namespace getDefaultNamespace() { String prefix = ""; String uri = (dom.lookupNamespaceURI(null) == null) ? "" : dom.lookupNamespaceURI(null); return Namespace.create(prefix, uri); }
Namespace getNamespace(String prefix) { if (map.get(prefix) == null) return null; return Namespace.create(prefix, (String) map.get(prefix)); }