private final String convertAttributeValue(Element element, String name, String value) { return attributeValueConverter.convertAttributeValue(element, name, value); }
reader.setAttributeValueConverter(AttributeValueConverter.fromPathMap(margs.set)); DataUtil.setValueOfMatchingAttributes(document, AttributePredicate.fromPaths(margs.set.keySet()), AttributeValueConverter.fromPathMap(margs.set), DataUtil.RECURSE); DataUtil.setValueOfMatchingAttributes(document, AttributePredicate.ANY_ATTRIBUTE, AttributeValueConverter.scramble(true), DataUtil.RECURSE); DataUtil.setValueOfMatchingAttributes(document, AttributePredicate.fromPaths(margs.scrambled), AttributeValueConverter.scramble(true), DataUtil.RECURSE); DataUtil.setContentOfMatchingTexts(document,
@Test public void testSetValueOfMatchingAttributes() throws IOException { final String source = XML + EOL + "<root x='a'>" + EOL + "<child y='a'/>" + EOL + "</root>"; final String expected = XML + EOL + "<root x='A'>" + EOL + "<child y='A'/>" + EOL + "</root>"; final Document doc = XmlDataReader.load(source, StandardCharsets.UTF_8); DataUtil.setValueOfMatchingAttributes(doc, AttributePredicate.ANY_ATTRIBUTE, AttributeValueConverter.fromValueFunction(String::toUpperCase), DataUtil.RECURSE); final String target = XmlDataWriter.toString(doc, INDENT, XmlWriter.Feature.USE_SINGLE_QUOTE); assertEquals(expected, target); }
/** * Returns a converter that first applies {@code other} then this converter. * * @param other The other converter. * @return A converter that first applies {@code other} then this converter. * @throws IllegalArgumentException When {@code other} is {@code null}. */ public default AttributeValueConverter compose(AttributeValueConverter other) { Checks.isNotNull(other, "other"); return (Element element, String name, String value) -> convertAttributeValue(element, name, other.convertAttributeValue(element, name, value)); }
/** * Returns a converter that first applies this converter and then {@code other}. * * @param other The other converter. * @return A converter that first applies this converter and then {@code other}. * @throws IllegalArgumentException When {@code other} is {@code null}. */ public default AttributeValueConverter andThen(AttributeValueConverter other) { Checks.isNotNull(other, "other"); return (Element element, String name, String value) -> other.convertAttributeValue(element, name, convertAttributeValue(element, name, value)); }
/** * 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); } } }