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(); }
/** * @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 void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { String k; if (insidePerson = (rawName.equals("author") || rawName.equals("editor"))) { Value = ""; return; } if (insidePerson = (rawName.equals("booktitle") || rawName.equals("journal"))) { Value = ""; return; } if ((atts.getLength() > 0) && ((k = atts.getValue("key")) != null)) { key = k; recordTag = rawName; } }
private Map attributeMap(String tagName, Attributes atts) throws ParserException { if (null == tagName || null == atts) return null; Map mapping = null; try { mapping = (Map) attributeMaps.get(tagName); } catch (Exception e) { throw new ParserException( "Typecast error, unknown element found in attribute list mappings! " + e.getMessage()); } if (null == mapping) return null; Map resultMapping = new HashMap(); for (int i = 0; i < atts.getLength(); i++) { String xmlName = atts.getQName(i); String value = atts.getValue(i); TagMap.AttributeMapping aMap = null; try { aMap = (TagMap.AttributeMapping) mapping.get(xmlName); } catch (Exception e) { throw new ParserException( "Typecast error, unknown element found in property mapping! " + e.getMessage()); } if (null == aMap) throw new ParserException( "No attribute mapping specified for attribute: " + xmlName + " in tag: " + tagName); String propertyName = aMap.getPropertyName(); try { resultMapping.put(propertyName, aMap.convertValue(value)); } catch (Exception e) { throw new ParserException( "Can not convert given value: \"" + value + "\" to specified type: " + aMap.getType() + " for attribute: " + xmlName + " in tag: " + tagName + "! " + e.getMessage()); } } checkForRequiredAttributes(tagName, resultMapping, mapping); addDefaultValues(resultMapping, mapping); return resultMapping; }
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 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); } }
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); }