<commit_before>package cz.crcs.ectester.common.output;

import cz.crcs.ectester.common.test.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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.OutputStream;

/**
 * @author <NAME> <EMAIL>
 */
public abstract class BaseXMLTestWriter implements TestWriter {
    private OutputStream output;
    private DocumentBuilder db;
    protected Document doc;
    private Node root;
    private Node tests;

    public BaseXMLTestWriter(OutputStream output) throws ParserConfigurationException {
        this.output = output;
        this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    }

    @Override
    public void begin(TestSuite suite) {
        doc = db.newDocument();
        Element rootElem = doc.createElement("testSuite");
        rootElem.setAttribute("name", suite.getName());
        rootElem.setAttribute("desc", suite.getDescription());

        root = rootElem;
        doc.appendChild(root);
        root.appendChild(deviceElement(suite));
        tests = doc.createElement("tests");
        root.appendChild(tests);
    }

    protected abstract Element testableElement(Testable t);

    protected abstract Element deviceElement(TestSuite suite);

    private String causeString(Object cause) {
        if (cause == null) {
            return "null";
        } else if (cause instanceof String) {
            return (String) cause;
        } else if (cause instanceof Throwable) {
            StringBuilder sb = new StringBuilder();
            for (Throwable t = (Throwable) cause; t != null; t = t.getCause()) {
                sb.append(t.toString());
                sb.append(System.lineSeparator());
            }
            return sb.toString();
        } else {
            return cause.toString();
        }
    }

    private Element resultElement(Result result) {
        Element resultElem = doc.createElement("result");

        Element ok = doc.createElement("ok");
        ok.setTextContent(String.valueOf(result.ok()));
        Element value = doc.createElement("value");
        value.setTextContent(result.getValue().name());
        Element cause = doc.createElement("cause");
        cause.setTextContent(causeString(cause));

        resultElem.appendChild(ok);
        resultElem.appendChild(value);
        resultElem.appendChild(cause);

        return resultElem;
    }

    private Element testElement(Test t) {
        Element testElem;<commit_msg>Fix XML output of Result cause.<commit_after>package cz.crcs.ectester.common.output;

import cz.crcs.ectester.common.test.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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.OutputStream;

/**
 * @author <NAME> <EMAIL>
 */
public abstract class BaseXMLTestWriter implements TestWriter {
    private OutputStream output;
    private DocumentBuilder db;
    protected Document doc;
    private Node root;
    private Node tests;

    public BaseXMLTestWriter(OutputStream output) throws ParserConfigurationException {
        this.output = output;
        this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    }

    @Override
    public void begin(TestSuite suite) {
        doc = db.newDocument();
        Element rootElem = doc.createElement("testSuite");
        rootElem.setAttribute("name", suite.getName());
        rootElem.setAttribute("desc", suite.getDescription());

        root = rootElem;
        doc.appendChild(root);
        root.appendChild(deviceElement(suite));
        tests = doc.createElement("tests");
        root.appendChild(tests);
    }

    protected abstract Element testableElement(Testable t);

    protected abstract Element deviceElement(TestSuite suite);

    private String causeString(Object cause) {
        if (cause == null) {
            return "null";
        } else if (cause instanceof String) {
            return (String) cause;
        } else if (cause instanceof Throwable) {
            StringBuilder sb = new StringBuilder();
            for (Throwable t = (Throwable) cause; t != null; t = t.getCause()) {
                sb.append(t.toString());
                sb.append(System.lineSeparator());
            }
            return sb.toString();
        } else {
            return cause.toString();
        }
    }

    private Element resultElement(Result result) {
        Element resultElem = doc.createElement("result");

        Element ok = doc.createElement("ok");
        ok.setTextContent(String.valueOf(result.ok()));
        Element value = doc.createElement("value");
        value.setTextContent(result.getValue().name());
        Element cause = doc.createElement("cause");
        cause.setTextContent(causeString(result.getCause()));

        resultElem.appendChild(ok);
        resultElem.appendChild(value);
        resultElem.appendChild(cause);

        return resultElem;
    }

    private Element testElement(Test t) {
        Element testElem;
{'fraction_non_alphanumeric': 0.10791117279666898, 'fraction_numerical': 0.001040943789035392, 'mean_word_length': 3.0202231520223153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '5104641', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1461, 'n_words': 336}