/** * Appends a namespace declaration in the form of a xmlns attribute to an attribute list, * crerating this latter if needed. * * @param atts <code>AttributeImpl</code> where to add the attribute. * @param ns <code>Namespace</code> the namespace to declare. * @return <code>AttributeImpl</code> the updated attribute list. */ private AttributesImpl addNsAttribute(AttributesImpl atts, Namespace ns) { if (this.declareNamespaces) { if (atts == null) { atts = new AttributesImpl(); } String prefix = ns.getPrefix(); if (prefix.equals("")) { atts.addAttribute( "", // namespace "", // local name "xmlns", // qualified name "CDATA", // type ns.getURI()); // value } else { atts.addAttribute( "", // namespace "", // local name "xmlns:" + ns.getPrefix(), // qualified name "CDATA", // type ns.getURI()); // value } } return atts; }
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. } }
/** * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code> * when a namespace is goes out of scope in the <code>Document</code>. * * @param namespace Namespace leaving scope. */ private void endPrefixMapping(Namespace namespace) throws JDOMException { try { contentHandler.endPrefixMapping(namespace.getPrefix()); } catch (SAXException se) { throw new JDOMException("Exception in endPrefixMapping", se); } }
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); }
/** * This will invoke the <code>ContentHandler.startPrefixMapping</code> callback when a new * namespace is encountered in the <code>Document</code>. * * @param element <code>Element</code> used in callbacks. * @param namespaces <code>List</code> stack of Namespaces in scope. * @return <code>Attributes</code> declaring the namespaces local to <code>element</code> or * <code>null</code>. */ private Attributes startPrefixMapping(Element element, NamespaceStack namespaces) throws JDOMException { AttributesImpl nsAtts = null; // The namespaces as xmlns attributes Namespace ns = element.getNamespace(); if (ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaces.getURI(prefix); if (!ns.getURI().equals(uri)) { namespaces.push(ns); nsAtts = this.addNsAttribute(nsAtts, ns); try { contentHandler.startPrefixMapping(prefix, ns.getURI()); } catch (SAXException se) { throw new JDOMException("Exception in startPrefixMapping", se); } } } // Fire additional namespace declarations List additionalNamespaces = element.getAdditionalNamespaces(); if (additionalNamespaces != null) { Iterator itr = additionalNamespaces.iterator(); while (itr.hasNext()) { ns = (Namespace) itr.next(); String prefix = ns.getPrefix(); String uri = namespaces.getURI(prefix); if (!ns.getURI().equals(uri)) { namespaces.push(ns); nsAtts = this.addNsAttribute(nsAtts, ns); try { contentHandler.startPrefixMapping(prefix, ns.getURI()); } catch (SAXException se) { throw new JDOMException("Exception in startPrefixMapping", se); } } } } return nsAtts; }
/** @deprecated Use getNamespace() */ String getPrefix() { return namespace.getPrefix(); }
void setAttribute(org.w3c.dom.Element element, String value) { if (namespace.getPrefix() == null) lookupPrefix(element); element.setAttributeNS(namespace.getUri(), qualify(namespace.getPrefix(), localName), value); }