Пример #1
0
  public static String saxElementToDebugString(String uri, String qName, Attributes attributes) {
    // Open start tag
    final StringBuilder sb = new StringBuilder("<");
    sb.append(qName);

    final Set<String> declaredPrefixes = new HashSet<String>();
    mapPrefixIfNeeded(declaredPrefixes, uri, qName, sb);

    // Attributes if any
    for (int i = 0; i < attributes.getLength(); i++) {
      mapPrefixIfNeeded(declaredPrefixes, attributes.getURI(i), attributes.getQName(i), sb);

      sb.append(' ');
      sb.append(attributes.getQName(i));
      sb.append("=\"");
      sb.append(attributes.getValue(i));
      sb.append('\"');
    }

    // Close start tag
    sb.append('>');

    // Content
    sb.append("[...]");

    // Close element with end tag
    sb.append("</");
    sb.append(qName);
    sb.append('>');

    return sb.toString();
  }
Пример #2
0
 /**
  * @param attributes source attributes
  * @return new AttributesImpl containing all attributes that were in src attributes and that were
  *     in the default name space.
  */
 public static AttributesImpl getAttributesFromDefaultNamespace(final Attributes attributes) {
   final AttributesImpl ret = new AttributesImpl();
   final int size = attributes.getLength();
   for (int i = 0; i < size; i++) {
     final String ns = attributes.getURI(i);
     if (!"".equals(ns)) continue;
     final String lnam = attributes.getLocalName(i);
     final String qnam = attributes.getQName(i);
     final String typ = attributes.getType(i);
     final String val = attributes.getValue(i);
     ret.addAttribute(ns, lnam, qnam, typ, val);
   }
   return ret;
 }
Пример #3
0
  public static AttributesImpl removeAttribute(
      Attributes attributes, String uri, String localname) {
    final AttributesImpl newAttributes = new AttributesImpl();
    for (int i = 0; i < attributes.getLength(); i++) {
      final String attributeURI = attributes.getURI(i);
      final String attributeValue = attributes.getValue(i);
      final String attributeType = attributes.getType(i);
      final String attributeQName = attributes.getQName(i);
      final String attributeLocalname = attributes.getLocalName(i);

      if (!uri.equals(attributeURI) || !localname.equals(attributeLocalname)) {
        // Not a matched attribute
        newAttributes.addAttribute(
            attributeURI, attributeLocalname, attributeQName, attributeType, attributeValue);
      }
    }
    return newAttributes;
  }
Пример #4
0
  public static AttributesImpl addOrReplaceAttribute(
      Attributes attributes, String uri, String prefix, String localname, String value) {
    final AttributesImpl newAttributes = new AttributesImpl();
    boolean replaced = false;
    for (int i = 0; i < attributes.getLength(); i++) {
      final String attributeURI = attributes.getURI(i);
      final String attributeValue = attributes.getValue(i);
      final String attributeType = attributes.getType(i);
      final String attributeQName = attributes.getQName(i);
      final String attributeLocalname = attributes.getLocalName(i);

      if (uri.equals(attributeURI) && localname.equals(attributeLocalname)) {
        // Found existing attribute
        replaced = true;
        newAttributes.addAttribute(
            uri,
            localname,
            XMLUtils.buildQName(prefix, localname),
            ContentHandlerHelper.CDATA,
            value);
      } else {
        // Not a matched attribute
        newAttributes.addAttribute(
            attributeURI, attributeLocalname, attributeQName, attributeType, attributeValue);
      }
    }
    if (!replaced) {
      // Attribute did not exist already so add it
      newAttributes.addAttribute(
          uri,
          localname,
          XMLUtils.buildQName(prefix, localname),
          ContentHandlerHelper.CDATA,
          value);
    }
    return newAttributes;
  }