public Attribute(String name, String value) { this(name); setValue(value); }
public Attribute(String name, double value) { this(name); setValue(value); }
public Attribute(String name, long value) { this(name); setValue(value); }
public Attribute(String name, Object value) { this(name); setValue(value); }
public Attribute(String name, int value) { this(name); setValue(value); }
public Attribute(String name, byte value) { this(name); setValue(value); }
public <E extends Enum<E>> Attribute(String name, E value) { this(name); setValue(value); }
public Attribute(String name, boolean value) { this(name); setValue(value); }
public Attribute(String name, short value) { this(name); setValue(value); }
public Attribute(String name, float 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 testMisc() { final Attribute att = new Attribute(NAME); assertEquals(att, att); assertEquals(NAME, att.getName()); final Attribute att2 = new Attribute(att); assertEquals(att, att2); att2.setValue("XXX"); assertNotEquals(att, att2); assertNotEquals(att, new Attribute("NAME")); }