Exemplo n.º 1
0
  /**
   * 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;
  }
Exemplo n.º 2
0
 /**
  * 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);
   }
 }
Exemplo n.º 3
0
  /**
   * 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;
  }