Example #1
0
  /**
   * Build the SAX attributes object based upon Java's String map. This convenience method will
   * build, or add to an existing attributes object, the attributes detailed in the AttributeMap.
   *
   * @param elementNamespace SAX Helper class to keep track of namespaces able to determine the
   *     correct prefix for a given namespace URI.
   * @param attributes An existing SAX AttributesImpl object to add attributes to. If the value is
   *     null then a new attributes object will be created to house the attributes.
   * @param attributeMap A map of attributes and values.
   * @return
   */
  private AttributesImpl map2sax(Namespace elementNamespace, AttributeMap... attributeMaps) {

    AttributesImpl attributes = new AttributesImpl();
    for (AttributeMap attributeMap : attributeMaps) {
      boolean differentNamespaces = false;
      Namespace attributeNamespace = attributeMap.getNamespace();
      if (attributeNamespace != null && !(attributeNamespace.URI.equals(elementNamespace.URI))) {
        differentNamespaces = true;
      }

      // copy each one over.
      for (Map.Entry<String, String> attr : attributeMap.entrySet()) {
        if (attr.getValue() == null) {
          continue;
        }

        if (differentNamespaces) {
          attributes.addAttribute(
              attributeNamespace.URI,
              attr.getKey(),
              qName(attributeNamespace, attr.getKey()),
              "CDATA",
              attr.getValue());

        } else {
          attributes.addAttribute("", attr.getKey(), attr.getKey(), "CDATA", attr.getValue());
        }
      }
    }
    return attributes;
  }
  /**
   * Build the SAX attributes object based upon Java's String map. This convenience method will
   * build, or add to an existing attributes object, the attributes detailed in the AttributeMap.
   *
   * @param namespaces SAX Helper class to keep track of namespaces able to determine the correct
   *     prefix for a given namespace URI.
   * @param attributes An existing SAX AttributesImpl object to add attributes too. If the value is
   *     null then a new attributes object will be created to house the attributes.
   * @param attributeMap A map of attributes and values.
   * @return
   */
  private AttributesImpl map2sax(
      Namespace elementNamespace,
      NamespaceSupport namespaces,
      AttributesImpl attributes,
      AttributeMap attributeMap) {

    if (attributes == null) attributes = new AttributesImpl();
    if (attributeMap != null) {
      // Figure out the namespace issue
      Namespace namespace = attributeMap.getNamespace();
      String URI;
      if (namespace != null) URI = namespace.URI;
      else URI = WingConstants.DRI.URI;

      String prefix = namespaces.getPrefix(URI);

      // copy each one over.
      for (String name : attributeMap.keySet()) {
        String value = attributeMap.get(name);
        if (value == null) continue;

        // If the indended namespace is the element's namespace then we
        // leave
        // off the namespace declaration because w3c say's its redundent
        // and breaks lots of xsl stuff.
        if (elementNamespace.URI.equals(URI))
          attributes.addAttribute("", name, name, "CDATA", value);
        else attributes.addAttribute(URI, name, qName(prefix, name), "CDATA", value);
      }
    }
    return attributes;
  }