<commit_before>package org.wso2.carbon.kernel.configresolver;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
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 javax.xml.transform.stream.StreamSource;

/**
 * Config Resolver utility methods.
 *
 * @since 5.2.0
 */
public class ConfigResolverUtils {
    private static final Logger logger = LoggerFactory.getLogger(ConfigResolverUtils.class);


    /**
     * This method converts a given YAML String to XML format.
     *
     * @param yamlString YAML String that needs to be converted to XML format
     * @return String in XML format
     */
    public static String convertYAMLToXML(String yamlString, String rootElement) {
        String jsonString = convertYAMLToJSON(yamlString);
        return convertJSONToXML(jsonString, rootElement);
    }

    /**
     * This method converts a given XML String to YAML format.
     *
     * @param xmlString XML String that needs to be converted to YAML format
     * @return String in YAML format
     */
    public static String convertXMLToYAML(String xmlString, String rootElement) {
        String jsonString = convertXMLToJSON(xmlString);
        return convertJSONToYAML(jsonString, rootElement);
    }

    /**
     * This method converts a given YAML String to JSON format.
     *
     * @param yamlString YAML String that needs to be converted to JSON format
     * @return String in JSON format
     */
    public static String convertYAMLToJSON(String yamlString) {
        String jsonString;
        Yaml yaml = new Yaml();
        Map map = (Map) yaml.load(yamlString);
        JSONObject jsonObject = new JSONObject(map);
        jsonString = jsonObject.toString();
        return jsonString;
    }

    /**
     * This method converts a given JSON String to YAML format.
     *
     * @param jsonString JSON String that needs to be converted to YAML format
     * @return String in YAML format
     */
    public static String convertJSONToYAML(String jsonString, String rootElement) {
        Yaml yaml = new Yaml();
        Map map = yaml.loadAs(jsonString, Map.class);
        //Remove root element<commit_msg>Remove unnecessary variable<commit_after>package org.wso2.carbon.kernel.configresolver;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
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 javax.xml.transform.stream.StreamSource;

/**
 * Config Resolver utility methods.
 *
 * @since 5.2.0
 */
public class ConfigResolverUtils {
    private static final Logger logger = LoggerFactory.getLogger(ConfigResolverUtils.class);


    /**
     * This method converts a given YAML String to XML format.
     *
     * @param yamlString YAML String that needs to be converted to XML format
     * @return String in XML format
     */
    public static String convertYAMLToXML(String yamlString, String rootElement) {
        String jsonString = convertYAMLToJSON(yamlString);
        return convertJSONToXML(jsonString, rootElement);
    }

    /**
     * This method converts a given XML String to YAML format.
     *
     * @param xmlString XML String that needs to be converted to YAML format
     * @return String in YAML format
     */
    public static String convertXMLToYAML(String xmlString, String rootElement) {
        String jsonString = convertXMLToJSON(xmlString);
        return convertJSONToYAML(jsonString, rootElement);
    }

    /**
     * This method converts a given YAML String to JSON format.
     *
     * @param yamlString YAML String that needs to be converted to JSON format
     * @return String in JSON format
     */
    public static String convertYAMLToJSON(String yamlString) {
        Yaml yaml = new Yaml();
        Map map = (Map) yaml.load(yamlString);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject.toString();
    }

    /**
     * This method converts a given JSON String to YAML format.
     *
     * @param jsonString JSON String that needs to be converted to YAML format
     * @return String in YAML format
     */
    public static String convertJSONToYAML(String jsonString, String rootElement) {
        Yaml yaml = new Yaml();
        Map map = yaml.loadAs(jsonString, Map.class);
        //Remove root element
{'fraction_non_alphanumeric': 0.0814129202871175, 'fraction_numerical': 0.0026445032111824707, 'mean_word_length': 3.3118892508143323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '986024', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1446, 'n_words': 461}