/** * Adds a string attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, String value) { addAttribute(new Attribute(name, value)); }
static String getNameAndAttributes(Element element) { final StringBuilder builder = new StringBuilder(); builder.append(element.getName()); for (final Attribute att : element.getSortedAttributes()) { builder.append(':'); builder.append(att.getName()); builder.append('='); builder.append(att.getValue()); } return builder.toString(); }
public Attribute(String name, String value) { this(name); setValue(value); }
/** * Changes the value of attributes that have a given name. * * @param name The attribute name. * @param modifier A function that take value and returns new value. */ public void changeAttributeValue(String name, Function<String, String> modifier) { final Attribute att = getAttribute(name); if (att != null) { final String modified = modifier.apply(att.getValue()); att.setValue(modified); } }
/** * Sets value of matching attributes. * <p> * Uses a Depth First traversal for recursion.<br> * Change is applied on parent before its children. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param predicate The predicate of matching attributes. <em>MUST NOT</em> be null. * @param converter The attribute value converter. <em>MUST NOT</em> be null. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void setValueOfMatchingAttributes(Parent parent, AttributePredicate predicate, AttributeValueConverter converter, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final Element element = (Element) parent; for (final Attribute att : element.getAttributes()) { if (predicate.accepts(element, att.getName(), att.getValue())) { final String newValue = converter.convertAttributeValue(element, att.getName(), att.getValue()); att.setValue(newValue); } } } for (final Parent child : parent.getChildren(Parent.class)) { if (evaluator.evaluate(child) == Evaluation.CONTINUE) { setValueOfMatchingAttributes(child, predicate, converter, evaluator); } } }
@Test public void testBoolean() { final Attribute att = new Attribute(NAME, true); assertTrue(att.getValueAsBoolean()); assertEquals("true", att.getValue()); }
@Test public void testFloat() { final Attribute att = new Attribute(NAME, 10.0f); assertEquals(10.0f, att.getValueAsFloat(), 1.0e-10); assertEquals("10.0", att.getValue()); }
@Test public void testByte() { final Attribute att = new Attribute(NAME, (byte) 10); assertEquals((byte) 10, att.getValueAsByte()); assertEquals("10", att.getValue()); }
@Test public void testShort() { final Attribute att = new Attribute(NAME, (short) 10); assertEquals((short) 10, att.getValueAsShort()); assertEquals("10", att.getValue()); }
@Test public void testLong() { final Attribute att = new Attribute(NAME, 10L); assertEquals(10L, att.getValueAsLong()); assertEquals("10", att.getValue()); }
@Test public void testInteger() { final Attribute att = new Attribute(NAME, 10); assertEquals(10, att.getValueAsInt()); assertEquals("10", att.getValue()); }
@Test public void testDouble() { final Attribute att = new Attribute(NAME, 10.0); assertEquals(10.0, att.getValueAsDouble(), 1.0e-10); assertEquals("10.0", att.getValue()); }
@Test public void testEnum() { final Attribute att = new Attribute(NAME, Enum.A); assertEquals(Enum.A, att.getValueAsEnum(Enum.class)); assertEquals(Enum.A.name(), att.getValue()); } }
/** * Creates a Predicate that accepts Attributes with a given name. * * @param name The name. * @return A new Predicate that accepts Attributes that are named {@code name}. */ public static Predicate<Attribute> named(String name) { return att -> att.getName().equals(name); }
/** * Returns the value associated to an attribute. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists. * @param missingReaction The reaction to adopt when attribute is not found. * @return The value of attribute named {@code name} or {@code def}. * @throws NotFoundException when attribute is not found and {@code missingReaction} is {@link FailureReaction#FAIL}. */ public String getAttributeValue(String name, String def, FailureReaction missingReaction) { final Attribute att = getAttribute(name); if (att == null) { if (missingReaction == FailureReaction.DEFAULT) { return def; } else if (missingReaction == FailureReaction.WARN) { LOGGER.warn("No attribute named '" + name + "'"); return def; } else { throw new NotFoundException("No attribute named '" + name + "'"); } } else { return att.getValue(); } }
/** * Creates a Predicate that accepts Attributes with a name belonging to a set. * * @param names The names. * @return A new Predicate that accepts Attributes whose name belongs to {@code names}. */ public static Predicate<Attribute> named(Set<String> names) { return att -> names.contains(att.getName()); }