001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.renderer.util;
003
004import static de.cuioss.tools.string.MoreStrings.emptyToNull;
005import static java.util.Objects.requireNonNull;
006import java.io.IOException;
007import java.util.ArrayList;
008import java.util.List;
009import java.util.stream.Collectors;
010import javax.xml.XMLConstants;
011import org.jdom2.Attribute;
012import org.jdom2.Document;
013import org.jdom2.Element;
014import org.jdom2.JDOMException;
015import org.jdom2.input.SAXBuilder;
016import de.cuioss.tools.io.IOStreams;
017
018/**
019 * Helper class providing convenience methods for dealing with Dom trees.
020 *
021 * @author Oliver Wolff
022 */
023public final class DomUtils {
024    /**
025     * The root element for creating {@link Document}s. This ensures correct
026     * structure regarding xml only having one root-element
027     */
028    public static final String ROOT_TEMPLATE = "<root>%s</root>";
029
030    /**
031     * Creates an instance of {@link Document} for the given htmlString. It always
032     * uses {@link #ROOT_TEMPLATE} as the root element.
033     *
034     * @param htmlString must not be null
035     * @return the created {@link Document} with {@link #ROOT_TEMPLATE} as the root
036     *         element.
037     */
038    public static Document htmlStringToDocument(final String htmlString) {
039        requireNonNull(htmlString);
040        final var wrappedInput = String.format(ROOT_TEMPLATE, htmlString);
041        try (var input = IOStreams.toInputStream(wrappedInput)) {
042            var saxBuilder = new SAXBuilder();
043            saxBuilder.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
044            saxBuilder.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
045            return saxBuilder.build(input);
046        } catch (JDOMException | IOException e) {
047            throw new IllegalArgumentException("Unable to parse given String, due to ", e);
048        }
049    }
050
051    /**
052     * Extracts all attributes with the given name. The method will recursively
053     * check all children as well.
054     *
055     * @param element       to be checked, must not be null.
056     * @param attributeName to be looked for, must not be null nor empty.
057     * @return a {@link List} with the found attributes, never null but may be
058     *         empty.
059     */
060    public static List<Attribute> filterForAttribute(final Element element, final String attributeName) {
061        requireNonNull(element);
062        requireNonNull(emptyToNull(attributeName));
063        List<Attribute> found = new ArrayList<>();
064        var current = element.getAttribute(attributeName);
065        if (null != current) {
066            found.add(current);
067        }
068        for (Element child : element.getChildren()) {
069            found.addAll(filterForAttribute(child, attributeName));
070        }
071        return found;
072    }
073
074    /**
075     * Extracts all attributes with the given name and the attribute-value
076     * containing the given String. The method will recursively check all children
077     * as well.
078     *
079     * @param element            to be checked, must not be null.
080     * @param attributeName      to be looked for, must not be null nor empty.
081     * @param attributeValuePart the string of the attribute value to be filtered
082     *                           for.
083     * @return a {@link List} with the found attributes, never null but may be
084     *         empty.
085     */
086    public static List<Attribute> filterForAttributeContainingValue(final Element element, final String attributeName, final String attributeValuePart) {
087        requireNonNull(emptyToNull(attributeValuePart));
088        return filterForAttribute(element, attributeName).stream().filter(a -> a.getValue().contains(attributeValuePart)).collect(Collectors.toList());
089    }
090
091    @java.lang.SuppressWarnings("all")
092    @lombok.Generated
093    private DomUtils() {
094        throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
095    }
096}