/FileExtraction/Java_unseen/AIMLang_aiml-java-interpreter/core/src/main/java/org/aimlang/core/utils/AppUtils.java
package org.aimlang.core.utils;

import org.w3c.dom.Node;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.List;
import java.util.Random;

/**
 * Additional utils
 *
 * @author anton
 * @since 21/10/16
 */
public class AppUtils {
    private static final Random random = new Random();

    public static <E> E getRandom(List<E> collection) {
        return collection.get(random.nextInt(collection.size()));
    }

    public static String node2String(Node node) {
        var nodeName = node.getNodeName();
        var sw = new StringWriter();
        try {
            var t = TransformerFactory.newInstance().newTransformer();
            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            t.setOutputProperty(OutputKeys.INDENT, "yes");
            t.transform(new DOMSource(node), new StreamResult(sw));
        } catch (TransformerException te) {
            System.out.println("nodeToString Transformer Exception");
        }
        return sw.toString()
                .replaceAll("(\r\n|\n\r|\r|\n)", " ")
                .replaceAll("> ", ">")
                .replaceFirst("<" + nodeName + ">", "")
                .replaceFirst("</" + nodeName + ">", "");
    }
}
