/**
   * 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;
  }
Exemple #2
0
  /**
   * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>.
   */
  private void startElement(Element element, Attributes nsAtts) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    // Allocate attribute list.
    AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl();

    List attributes = element.getAttributes();
    Iterator i = attributes.iterator();
    while (i.hasNext()) {
      Attribute a = (Attribute) i.next();
      atts.addAttribute(
          a.getNamespaceURI(),
          a.getName(),
          a.getQualifiedName(),
          getAttributeTypeName(a.getAttributeType()),
          a.getValue());
    }

    try {
      contentHandler.startElement(namespaceURI, localName, rawName, atts);
    } catch (SAXException se) {
      throw new JDOMException("Exception in startElement", se);
    }
  }
Exemple #3
0
  @Override
  protected void finishOpen() throws IOException {
    try {
      final AttributesImpl attrs = new AttributesImpl();
      final int as = attributes.size();
      for (int a = 0; a < as; a++) {
        final byte[] name = attributes.name(a);
        final String uri = string(namespaces.get(prefix(name)));
        final String lname = string(local(name));
        final String rname = string(name);
        final String value = string(attributes.value(a));
        attrs.addAttribute(uri, lname, rname, null, value);
      }

      final String uri = string(namespaces.get(elem.prefix()));
      final String lname = string(elem.local());
      final String rname = string(elem.string());
      contentHandler.startElement(uri, lname, rname, attrs);

    } catch (final SAXException ex) {
      throw new IOException(ex);
    }
  }