/** * Returns the value associated to an attribute as a string or a default value. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists. * @return The value of attribute named {@code name} or {@code def}. */ public String getAttributeValue(String name, String def) { return getAttributeValue(name, def, FailureReaction.DEFAULT); }
/** * Returns the value associated to an attribute as a string or {@code null}. * * @param name The attribute name. * @return The value of attribute named {@code name} or {@code null}. */ public String getAttributeValue(String name) { return getAttributeValue(name, null); }
/** * Returns a comparator that compares the values of a particular attribute. * * @param attributeName The name of the attribute to use. * @return A comparator comparing the value of attributes named {@code attributeName}. */ public static Comparator<Element> compareAttribute(String attributeName) { return (o1, o2) -> { final String value1 = o1 == null ? null : o1.getAttributeValue(attributeName, null, FailureReaction.WARN); final String value2 = o2 == null ? null : o2.getAttributeValue(attributeName, null, FailureReaction.WARN); return Operators.compare(value1, value2); }; }
public Enum<?> getAttributeAsRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def) { return StringConversion.asRawEnum(getAttributeValue(name, null), enumClass, def, FailureReaction.DEFAULT, FailureReaction.WARN); }
public Enum<?> getAttributeAsOptionalRawEnum(String name, Class<? extends Enum<?>> enumClass, Enum<?> def) { return StringConversion.asOptionalRawEnum(getAttributeValue(name, null), enumClass, def, FailureReaction.WARN); }
public <E extends Enum<E>> E getAttributeAsOptionalEnum(String name, Class<E> enumClass, E def) { return StringConversion.asOptionalEnum(getAttributeValue(name, null), enumClass, def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Boolean or a default value. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Boolean. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Boolean, then {@code def} is returned and a warning is issued. */ public Boolean getAttributeAsOptionalBoolean(String name, Boolean def) { return StringConversion.asOptionalBoolean(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Long. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Long. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Long, then {@code def} is returned and a warning is issued. */ public Long getAttributeAsOptionalLong(String name, Long def) { return StringConversion.asOptionalLong(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Double. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Double. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Double, then {@code def} is returned and a warning is issued. */ public Double getAttributeAsOptionalDouble(String name, Double def) { return StringConversion.asOptionalDouble(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Short. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Short. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Short, then {@code def} is returned and a warning is issued. */ public Short getAttributeAsOptionalShort(String name, Short def) { return StringConversion.asOptionalShort(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as an Integer. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to an Integer. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to an Integer, then {@code def} is returned and a warning is issued. */ public Integer getAttributeAsOptionalInt(String name, Integer def) { return StringConversion.asOptionalInt(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Byte. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Byte. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Byte, then {@code def} is returned and a warning is issued. */ public Byte getAttributeAsOptionalByte(String name, Byte def) { return StringConversion.asOptionalByte(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a double. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a double. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a double, then {@code def} is returned and a warning is issued. */ public double getAttributeAsDouble(String name, double def) { return StringConversion.asDouble(getAttributeValue(name, null), def, FailureReaction.DEFAULT, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as an int. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to an int. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to an int, then {@code def} is returned and a warning is issued. */ public int getAttributeAsInt(String name, int def) { return StringConversion.asInt(getAttributeValue(name, null), def, FailureReaction.DEFAULT, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a byte. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a byte. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a byte, then {@code def} is returned and a warning is issued. */ public byte getAttributeAsByte(String name, byte def) { return StringConversion.asByte(getAttributeValue(name, null), def, FailureReaction.DEFAULT, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a Float. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a Float. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a Float, then {@code def} is returned and a warning is issued. */ public Float getAttributeAsOptionalFloat(String name, Float def) { return StringConversion.asOptionalFloat(getAttributeValue(name, null), def, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a long. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a long. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a long, then {@code def} is returned and a warning is issued. */ public long getAttributeAsLong(String name, long def) { return StringConversion.asLong(getAttributeValue(name, null), def, FailureReaction.DEFAULT, FailureReaction.WARN); }
/** * Returns the value associated to an attribute as a short. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists * or if its value can not be converted to a short. * @return The value of attribute named {@code name} or {@code def}.<br> * If the attribute exists but its value can not be converted * to a short, then {@code def} is returned and a warning is issued. */ public short getAttributeAsShort(String name, short def) { return StringConversion.asShort(getAttributeValue(name, null), def, FailureReaction.DEFAULT, FailureReaction.WARN); }
public static Args loadArgs(Element element) { final Args.Builder builder = Args.builder(); for (final Element child : element.getElementsNamed(ARG)) { final String argName = child.getAttributeValue(NAME, null); final String argValue = child.getAttributeValue(VALUE, null); builder.setArg(argName, argValue); } return builder.build(); }
@Test public void testElementFiltering() throws IOException { final XmlDataReader reader = new XmlDataReader(); reader.setEnabled(XmlDataReader.Feature.LOAD_SPACES, false); reader.setElementPreFilter((parent, element) -> "root".equals(element.getName()) || "E1".equals(element.getAttributeValue("id", "")) || "E1.1".equals(element.getAttributeValue("id", "")) || "E2".equals(element.getAttributeValue("id", ""))); final URL url = getClass().getClassLoader().getResource("test-reader.xml"); final Document doc = reader.read(Files.toFile(url).getPath()); LOGGER.debug("Only E1, E1.1 and E2"); XmlDataWriter.print(doc, OUT, " ", false); }