/** * 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 TextContentConverter compose(TextContentConverter other) { Checks.isNotNull(other, "other"); return (Parent parent, String content) -> convertTextContent(parent, other.convertTextContent(parent, content)); }
/** * 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 TextContentConverter andThen(TextContentConverter other) { Checks.isNotNull(other, "other"); return (Parent parent, String content) -> other.convertTextContent(parent, convertTextContent(parent, content)); }
private void resetActiveText() { if (activeText != null) { activeText.setContent(textContentConverter.convertTextContent(activeText.getParent(), activeText.getContent())); activeText = null; } }
public static void setContentOfMatchingElements(Parent parent, ElementPredicate predicate, TextContentConverter converter, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final Element element = (Element) parent; if (predicate.accepts(element.getParent(), element)) { final int count = element.getChildrenCount(); if (count == 0) { // Add a text child final String content = converter.convertTextContent(element, null); if (content != null) { element.addText(content); } } else if (count == 1 && element.hasOnlyText()) { // Modify text child if possible final Text text = element.getChild(Text.class); final String content = converter.convertTextContent(element, text.getContent()); text.setContent(content); } } } for (final Child child : parent.getChildren()) { if (child instanceof Parent && evaluator.evaluate((Parent) child) == Evaluation.CONTINUE) { setContentOfMatchingElements((Parent) child, predicate, converter, evaluator); } } }
/** * Sets the content of matching texts. * <p> * Uses a Depth First traversal for recursion. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param predicate The predicate of matching texts. <em>MUST NOT</em> be null. * @param converter The content converter. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void setContentOfMatchingTexts(Parent parent, TextPredicate predicate, TextContentConverter converter, Evaluator<? super Parent> evaluator) { for (final Child child : parent.getChildren()) { if (child instanceof Text) { final Text text = (Text) child; if (predicate.accepts(parent, text)) { final String content = converter.convertTextContent(parent, text.getContent()); text.setContent(content); } } else if (child instanceof Parent && evaluator.evaluate((Parent) child) == Evaluation.CONTINUE) { setContentOfMatchingTexts((Parent) child, predicate, converter, evaluator); } } }