<commit_before>/*
 * This file is part of the DITA Open Toolkit project.
 * See the accompanying license.txt file for applicable licenses.
 */
package org.dita.dost.util;

import static javax.xml.XMLConstants.NULL_NS_URI;
import static org.apache.commons.io.FileUtils.*;
import static org.dita.dost.util.Constants.*;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.dita.dost.exception.DITAOTException;
import org.w3c.dom.*;

import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * XML utility methods.
 * 
 * @since 1.5.4
 * @author <NAME>
 */
public final class XMLUtils {

    /** Private constructor to make class uninstantiable. */
    private XMLUtils() {}

    /**
     * List descendant elements by DITA class.
     *
     * @param elem root element
     * @param cls DITA class to match elements
     * @param deep {@code true} to read descendants, {@code false} to read only direct children
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem, final DitaClass cls, final boolean deep) {
        final NodeList children = deep ? elem.getElementsByTagName("*") : elem.getChildNodes();
        final List<Element> res = new ArrayList<>(children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (cls.matches(child)) {
                res.add((Element) child);
            }
        }
        return res;
    }

    /**
     * List chilid elements by DITA class.
     *
     * @param elem root element
     * @param cls DITA class to match elements
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem, final DitaClass cls) {
        return getChildElements(elem, cls, false);
    }

    /**
     * List child elements elements.
     *
     * @param elem root element
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem) {
        final NodeList children = elem.getChildNodes();
        final List<Element> res = new ArrayList<>(children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                res.add((Element) child);
            }
        }
        return res;
    }

    /**
     * Add or set attribute.
     * 
     * @param atts attributes
     * @param uri namespace URI<commit_msg>Add XML utility method<commit_after>/*
 * This file is part of the DITA Open Toolkit project.
 * See the accompanying license.txt file for applicable licenses.
 */
package org.dita.dost.util;

import static javax.xml.XMLConstants.NULL_NS_URI;
import static org.apache.commons.io.FileUtils.*;
import static org.dita.dost.util.Constants.*;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.dita.dost.exception.DITAOTException;
import org.w3c.dom.*;

import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * XML utility methods.
 * 
 * @since 1.5.4
 * @author <NAME>
 */
public final class XMLUtils {

    /** Private constructor to make class uninstantiable. */
    private XMLUtils() {}

    /**
     * List descendant elements by DITA class.
     *
     * @param elem root element
     * @param cls DITA class to match elements
     * @param deep {@code true} to read descendants, {@code false} to read only direct children
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem, final DitaClass cls, final boolean deep) {
        final NodeList children = deep ? elem.getElementsByTagName("*") : elem.getChildNodes();
        final List<Element> res = new ArrayList<>(children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (cls.matches(child)) {
                res.add((Element) child);
            }
        }
        return res;
    }

    /**
     * List chilid elements by DITA class.
     *
     * @param elem root element
     * @param cls DITA class to match elements
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem, final DitaClass cls) {
        return getChildElements(elem, cls, false);
    }

    /**
     * List child elements elements.
     *
     * @param elem root element
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem) {
        return getChildElements(elem, false);
    }

    /**
     * List child elements elements.
     *
     * @param elem root element
     * @param deep {@code true} to read descendants, {@code false} to read only direct children
     * @raturn list of matching elements
     */
    public static List<Element> getChildElements(final Element elem, final boolean deep) {
        final NodeList children = deep ? elem.getElementsByTagName("*") : elem.getChildNodes();
        final List<Element> res = new ArrayList<>(children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                res.add((Element) child);
            }
        }
        return res;
    }

    /**
     * Add or set attribute.
     * 
     * @param atts attributes
     * @param uri namespace URI
{'fraction_non_alphanumeric': 0.10875160875160875, 'fraction_numerical': 0.0019305019305019305, 'mean_word_length': 2.7072152653548, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5317147', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1753, 'n_words': 537}