📄 attributehelper.java
字号:
} try { return Float.parseFloat(value); } catch (NumberFormatException e) { throw new ParseException("Invalid float value for attribute: '" + name + "'", e); } } /** * Gets the value of a float attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @return the float value of this attribute, 0 by default * @throws ParseException if required is set and the attribute is not defined, * or if the attribute value is not a valid float */ public float consumeFloat(String name, boolean required) throws ParseException { return consumeFloat(name, required, 0); } /** * Gets the value of a boolean attribute and remove it from the list. The * accepted values are based upon xsd:boolean syntax (true, false, 1, 0). * * @param name attribute name * @param required indicates attribute is required * @param defaultValue the default value for an optional attribute (used * if not present) * @return the boolean value of this attribute * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is neither {@code true} * nor {@code false}. */ public boolean consumeBoolean(String name, boolean required, boolean defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } if ("true".equals(value) || "1".equals(value)) { return true; } else if ("false".equals(value) || "0".equals(value)) { return false; } else { throw new ParseException("Invalid boolean value for attribute: '" + name + "'"); } } /** * Gets the value of a boolean attribute and remove it from the list. The * accepted values are based upon xsd:boolean syntax (true, false, 1, 0). * * @param name attribute name * @param required indicates attribute is required * @return the boolean value of this attribute, false by default * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is neither {@code true} * nor {@code false}. */ public boolean consumeBoolean(String name, boolean required) throws ParseException { return consumeBoolean(name, required, false); } /** * Gets the value of a {@link DateTime} attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @return the date-time value of this attribute, {@code null} by default * @exception ParseException if required is set and the attribute * is not defined, or if the date-time attribute cannot be parsed */ public DateTime consumeDateTime(String name, boolean required) throws ParseException { String value = consume(name, required); if (value == null) { return null; } try { return DateTime.parseDateTimeChoice(value); } catch (NumberFormatException e) { throw new ParseException("Invalid date/time value.", e); } } /** * Defines a custom mapping of an enum value to an attribute value (similar to * a closure). */ public static interface EnumToAttributeValue<T extends Enum<T>> { String getAttributeValue(T enumValue); } /** * Implements the most common custom mapping of an enum value to an attribute * value using the lower-case form of the enum name. */ public static class LowerCaseEnumToAttributeValue<T extends Enum<T>> implements EnumToAttributeValue<T> { public String getAttributeValue(T enumValue) { return enumValue.name().toLowerCase(); } } /** * Gets the value of an enumerated attribute and remove it from the list, * using a custom mapping of enum to attribute value. * * @param name attribute name * @param required indicates attribute is required * @param enumClass enumeration class * @param defaultValue the default value for an optional attribute * (used if not present) * @param enumToAttributeValue custom mapping of enum to attribute value * @return an enumerated value * @throws ParseException if required is set and the attribute is not defined, * or if the attribute value is not a valid enumerated * value */ public <T extends Enum<T>> T consumeEnum(String name, boolean required, Class<T> enumClass, T defaultValue, EnumToAttributeValue<T> enumToAttributeValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } for (T enumValue : enumClass.getEnumConstants()) { if (enumToAttributeValue.getAttributeValue(enumValue).equals(value)) { return enumValue; } } throw new ParseException("Invalid value for attribute : '" + name + "'"); } /** * Gets the value of an enumerated attribute and remove it from the list. * * Enumerated values are case-insensitive. * * @param name attribute name * @param required indicates attribute is required * @param enumClass enumeration class * @param defaultValue the default value for an optional attribute (used * if not present) * @return an enumerated value * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid * enumerated value */ public <T extends Enum<T>> T consumeEnum(String name, boolean required, Class<T> enumClass, T defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } try { return Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new ParseException("Invalid value for attribute : '" + name + "'", e); } } /** * Gets the value of an enumerated attribute and remove it from the list. * * Enumerated values are case-insensitive. * * @param name attribute name * @param required indicates attribute is required * @param enumClass enumeration class * @return an enumerated value or null if not present * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid * enumerated value */ public <T extends Enum<T>> T consumeEnum(String name, boolean required, Class<T> enumClass) throws ParseException { return consumeEnum(name, required, enumClass, null); } /** * Makes sure all attributes have been removed from the list. * * To all attribute in the default namespace must correspond exactly * one call to consume*(). * * @exception ParseException if an attribute in the default namespace * hasn't been removed */ public void assertAllConsumed() throws ParseException { StringBuffer message = new StringBuffer(); if (!attrs.isEmpty()) { message.append("Unknown attribute"); if (attrs.size() > 1) { message.append('s'); } message.append(':'); for (String name : attrs.keySet()) { message.append(" '"); message.append(name); message.append("' "); } } if (!dups.isEmpty()) { message.append("Duplicate attribute"); if (dups.size() > 1) { message.append('s'); } message.append(':'); for (String dup : dups) { message.append(" '"); message.append(dup); message.append("' "); } } if (!contentConsumed && content != null && content.length() != 0) { message.append("Unexpected text content "); } if (message.length() != 0) { throw new ParseException(message.toString()); } } /** * Sets the content. * * @param content element's text content */ void setContent(String content) { // text content this.content = content == null ? null : content.trim(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -