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(); }
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { digest.update((byte) ((ELEMENT_CODE >> 24) & 0xff)); digest.update((byte) ((ELEMENT_CODE >> 16) & 0xff)); digest.update((byte) ((ELEMENT_CODE >> 8) & 0xff)); digest.update((byte) (ELEMENT_CODE & 0xff)); addToCharBuff('{'); addToCharBuff(namespaceURI); addToCharBuff('}'); addToCharBuff(localName); updateWithCharBuf(); digest.update((byte) 0); digest.update((byte) 0); int attCount = atts.getLength(); digest.update((byte) ((attCount >> 24) & 0xff)); digest.update((byte) ((attCount >> 16) & 0xff)); digest.update((byte) ((attCount >> 8) & 0xff)); digest.update((byte) (attCount & 0xff)); for (int i = 0; i < attCount; i++) { digest.update((byte) ((ATTRIBUTE_CODE >> 24) & 0xff)); digest.update((byte) ((ATTRIBUTE_CODE >> 16) & 0xff)); digest.update((byte) ((ATTRIBUTE_CODE >> 8) & 0xff)); digest.update((byte) (ATTRIBUTE_CODE & 0xff)); final String attURI = atts.getURI(i); final String attNam = atts.getLocalName(i); addToCharBuff('{'); addToCharBuff(attURI); addToCharBuff('}'); addToCharBuff(attNam); updateWithCharBuf(); digest.update((byte) 0); digest.update((byte) 0); final String val = atts.getValue(i); updateWith(val); } }
/** * @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; }
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; }
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; }
/** * Append classes to existing attributes. This creates a new AttributesImpl object. * * @param attributes existing attributes * @param newClasses new classes to append * @return new attributes */ public static AttributesImpl appendToClassAttribute(Attributes attributes, String newClasses) { final String oldClassAttribute = attributes.getValue("class"); final String newClassAttribute = oldClassAttribute == null ? newClasses : oldClassAttribute + ' ' + newClasses; return addOrReplaceAttribute(attributes, "", "", "class", newClassAttribute); }