public int computeCost(Attributes attrs) { int accu = 0; for (Map.Entry<String, String> attr : attrs.entrySet()) { accu += objectOverhead + computeCost(attr.getKey()) + computeCost(attr.getValue()); } return accu; }
public static String attributeString(Attributes attributes) { StringBuilder b = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> e : attributes.entrySet()) { if (first) { first = false; } else { b.append(" "); } // We're just writing null with no quotes if the value is null. // This is acceptable since it only occurs in updateAttributes, // which is a processing instruction, so we define the format of it. // // TODO: We should escape ' and " and < and & etc. in the value. b.append(e.getKey() + "=" + (e.getValue() == null ? "null" : "\"" + xmlAttrEscape(e.getValue()) + "\"")); } return b.toString(); }
private ValidationResult validateAttributes(String tag, Attributes attr, ViolationCollector v) { for (Map.Entry<String, String> e : attr.entrySet()) { String key = e.getKey(); String value = e.getValue(); if (!elementAllowsAttribute(tag, key, value)) { return invalidAttribute(v, tag, key, value); } } return ValidationResult.VALID; }
public static String toConciseString(Attributes attributes) { if (attributes.isEmpty()) { return "{}"; } StringBuilder b = new StringBuilder(); b.append("{ "); boolean first = true; for (Map.Entry<String, String> entry : attributes.entrySet()) { if (first) { first = false; } else { b.append(", "); } b.append(entry.getKey()); b.append("="); b.append(literalString(entry.getValue())); } b.append(" }"); return b.toString(); }
public static Attributes scrubAttributes(Attributes unscrubbed, ScrubCache nameScrubber) { List<Attribute> list = new ArrayList<Attribute>(); for (Map.Entry<String, String> entry : unscrubbed.entrySet()) { list.add(new Attribute( nameScrubber.scrubUniquely(entry.getKey()), scrubMostString(entry.getValue()))); } return AttributesImpl.fromUnsortedAttributesUnchecked(list); }
private boolean attributesEqual(Attributes a, Attributes b) { if (a.size() != b.size()) { return false; } for (Map.Entry<String, String> ae : a.entrySet()) { if (!equal(ae.getValue(), b.get(ae.getKey()))) { return false; } } return true; }
private ValidationResult checkAttributesWellFormed(Attributes attr, ViolationCollector v) { if (attr == null) { return nullAttributes(v); } String previousKey = null; for (Map.Entry<String, String> e : attr.entrySet()) { if (e.getKey() == null) { return nullAttributeKey(v); } if (!Utf16Util.isXmlName(e.getKey())) { return attributeNameNotXmlName(v, e.getKey()); } if (e.getValue() == null) { return nullAttributeValue(v); } if (!Utf16Util.isValidUtf16(e.getValue())) { return attributeValueNotValidUtf16(v); } if (previousKey != null && previousKey.compareTo(e.getKey()) >= 0) { return attributeKeysNotStrictlyMonotonic(v, previousKey, e.getKey()); } previousKey = e.getKey(); } return ValidationResult.VALID; }
/** * Validate the given <code>Attributes</code>. * * @param attributes the <code>Attributes</code> to validate * @return information about whether the <code>Attributes</code> is valid and, * if not, why it is not */ AttributeValidationResult validate(Attributes attributes) { for (String name : requiredAttributes) { if (!attributes.containsKey(name)) { return new MissingRequiredAttribute(name); } } for (Map.Entry<String, String> attribute : attributes.entrySet()) { String name = attribute.getKey(); ValueValidator validator = valueValidators.get(name); if (validator == null) { return new AttributeNotAllowed(name); } String value = attribute.getValue(); if (!validator.validate(value)) { return new InvalidAttributeValue(name, value); } } return AttributeValidationResult.VALID; }