/** * 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 startPrefixMapping(Namespace namespace) throws JDOMException { try { contentHandler.startPrefixMapping(namespace.getPrefix(), namespace.getURI()); } catch (SAXException se) { throw new JDOMException("Exception in startPrefixMapping", se); } }
/** * 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; }
/** * This will add the prefix mapping to the JDOM <code>Document</code> object. * * @param prefix <code>String</code> namespace prefix. * @param uri <code>String</code> namespace URI. */ public void startPrefixMapping(String prefix, String uri) throws SAXException { if (suppress) return; Namespace ns = Namespace.getNamespace(prefix, uri); declaredNamespaces.add(ns); }
/** * 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; }
/** * This reports the occurrence of an actual element. It will include the element's attributes, * with the exception of XML vocabulary specific attributes, such as <code> * xmlns:[namespace prefix]</code> and <code>xsi:schemaLocation</code>. * * @param namespaceURI <code>String</code> namespace URI this element is associated with, or an * empty <code>String</code> * @param localName <code>String</code> name of element (with no namespace prefix, if one is * present) * @param qName <code>String</code> XML 1.0 version of element name: [namespace * prefix]:[localName] * @param atts <code>Attributes</code> list for this element * @throws SAXException when things go wrong */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (suppress) return; Element element = null; if ((namespaceURI != null) && (!namespaceURI.equals(""))) { String prefix = ""; // Determine any prefix on the Element if (!qName.equals(localName)) { int split = qName.indexOf(":"); prefix = qName.substring(0, split); } Namespace elementNamespace = Namespace.getNamespace(prefix, namespaceURI); element = factory.element(localName, elementNamespace); } else { element = factory.element(localName); } // Take leftover declared namespaces and add them to this element's // map of namespaces if (declaredNamespaces.size() > 0) { transferNamespaces(element); } // Handle attributes for (int i = 0, len = atts.getLength(); i < len; i++) { Attribute attribute = null; String attLocalName = atts.getLocalName(i); String attQName = atts.getQName(i); int attType = getAttributeType(atts.getType(i)); // Bypass any xmlns attributes which might appear, as we got // them already in startPrefixMapping(). // This is sometimes necessary when SAXHandler is used with // another source than SAXBuilder, as with JDOMResult. if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) { continue; } // First clause per http://markmail.org/message/2p245ggcjst27xe6 // patch from Mattias Jiderhamn if ("".equals(attLocalName) && attQName.indexOf(":") == -1) { attribute = factory.attribute(attQName, atts.getValue(i), attType); } else if (!attQName.equals(attLocalName)) { String attPrefix = attQName.substring(0, attQName.indexOf(":")); Namespace attNs = Namespace.getNamespace(attPrefix, atts.getURI(i)); attribute = factory.attribute(attLocalName, atts.getValue(i), attType, attNs); } else { attribute = factory.attribute(attLocalName, atts.getValue(i), attType); } factory.setAttribute(element, attribute); } flushCharacters(); if (atRoot) { document.setRootElement(element); // XXX should we use a factory call? atRoot = false; } else { factory.addContent(getCurrentElement(), element); } currentElement = element; }