/**
   * 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;
  }