<commit_before>package org.testng.internal;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.testng.IClass;
import org.testng.IObjectFactory;
import org.testng.IResultMap;
import org.testng.ITestNGMethod;
import org.testng.TestNGCommandLineArgs;
import org.testng.TestNGException;
import org.testng.TestRunner;
import org.testng.internal.annotations.AnnotationHelper;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.annotations.IConfiguration;
import org.testng.internal.annotations.IParameters;
import org.testng.internal.annotations.ITest;
import org.testng.log.TextFormatter;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlTest;

/**
 * Helper methods to parse annotations.
 *
 * @author <NAME>, Apr 26, 2004
 * @author <a href = "mailto:the_mindstorm&#64;evolva.ro"><NAME></a>
 */
public final class Utils {
  private static final String LINE_SEP = System.getProperty("line.separator");
  
  /**
   * Hide constructor for utility class.
   */
  private Utils() {
    // Hide constructor
  }
  
  /**
   * Splits the given String s into tokens where the separator is
   * either the space character or the comma character. For example, 
   * if s is "a,b, c" this method returns {"a", "b", "c"}
   *  
   * @param s the string to split
   * @return the split token 
   */  
  public static String[] stringToArray(String s) {
    // TODO CQ would s.split() be a better way of doing this?
    StringTokenizer st = new StringTokenizer(s, " ,");
    String[] result = new String[st.countTokens()];
    for (int i = 0; i < result.length; i++) {
      result[i] = st.nextToken();
    }

    return result;
  }

  public static Class[] xmlClassesToClasses(List<XmlClass> classes) {
    List<Class> result = new ArrayList<Class>();

    for (XmlClass xmlClass : classes) {
      result.add(xmlClass.getSupportClass());
    }

    Class[] xmlClasses = result.toArray(new Class[classes.size()]);
    Map<Class, Class> withNestedClasses = new HashMap<Class, Class>();
    findAllClasses(xmlClasses, withNestedClasses);

    return withNestedClasses.values().toArray(new Class[withNestedClasses.size()]);
  }

  public static XmlClass[] classesToXmlClasses(Class[] classes) {
    List<XmlClass> result = new ArrayList<XmlClass>();

    for (Class cls : classes) {
      result.add(new XmlClass(cls));
    }

    return result.toArray(new XmlClass[classes.length]);

  }
  
  /**
   * Find all the classes inside this array, including nested ones.
   * @param classes
   * @return
   */
  private static void findAllClasses(Class[] classes, Map<Class, Class> result) {
    for (Class cls : classes) {
      if (!result.containsKey(cls)) {
        result.put(cls, cls);
      }
    }
  }

  public static String[] parseMultiLine(String line) {
    List<String> vResult = new ArrayList<String>();
    if ((null != line) && !"".equals(line.trim())) {
      StringTokenizer st = new StringTokenizer(line, " ");
      while (st.hasMoreTokens()) {
        vResult.add(st.nextToken());
      }
      // Bug in split when passed " " : returns one too many result
      //      result = line.split(" ");
    }

    String[] result = vResult.toArray(new String[vResult.size()]);

    return result;
  }

  /**
   * Writes the content of the sb string buffer to the file named filename in outputDir. If 
   * outDir does not exist, it is created.
   *
   * @param outputDir the output directory (may not exist).
   * @param fileName the filename
   * @param sb the file content
   */
  public static void writeFile(String outputDir, String fileName, StringBuffer sb) {
    writeFile(outputDir, fileName, sb.toString());
  }

  /**
   * Writes the content of the sb string to the file named filename in outDir. If 
   * outDir does not exist, it is created.
   *
   * @param outputDir the output directory (may not exist). If <tt>null</tt> then current directory is used.
   * @param fileName the filename
   * @param sb the file content
   */
  public static void writeFile(String outputDir, String fileName, String sb) {
    try {
      final String outDirPath= outputDir != null ? outputDir : "";
      final File outDir= new File(outDirPath);
      if (!outDir.exists()) {
        outDir.mkdirs();
      }
      
      File outputFile = new File(outDir, fileName);
      outputFile.delete();
      outputFile.createNewFile();
      
      writeFile(outputFile, sb);
    }
    catch (IOException e) {
      if (TestRunner.getVerbose() > 1) {
        e.printStackTrace();
      }
      else {
        log("[Utils]", 1, e.getMessage());
      }
    }
  }

  private static void writeFile(File outputFile, String sb) {<commit_msg>Added helper method to write to File<commit_after>package org.testng.internal;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.testng.IClass;
import org.testng.IObjectFactory;
import org.testng.IResultMap;
import org.testng.ITestNGMethod;
import org.testng.TestNGCommandLineArgs;
import org.testng.TestNGException;
import org.testng.TestRunner;
import org.testng.internal.annotations.AnnotationHelper;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.annotations.IConfiguration;
import org.testng.internal.annotations.IParameters;
import org.testng.internal.annotations.ITest;
import org.testng.log.TextFormatter;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlTest;

/**
 * Helper methods to parse annotations.
 *
 * @author <NAME>, Apr 26, 2004
 * @author <a href = "mailto:the_mindstorm&#64;evolva.ro"><NAME></a>
 */
public final class Utils {
  private static final String LINE_SEP = System.getProperty("line.separator");
  
  /**
   * Hide constructor for utility class.
   */
  private Utils() {
    // Hide constructor
  }
  
  /**
   * Splits the given String s into tokens where the separator is
   * either the space character or the comma character. For example, 
   * if s is "a,b, c" this method returns {"a", "b", "c"}
   *  
   * @param s the string to split
   * @return the split token 
   */  
  public static String[] stringToArray(String s) {
    // TODO CQ would s.split() be a better way of doing this?
    StringTokenizer st = new StringTokenizer(s, " ,");
    String[] result = new String[st.countTokens()];
    for (int i = 0; i < result.length; i++) {
      result[i] = st.nextToken();
    }

    return result;
  }

  public static Class[] xmlClassesToClasses(List<XmlClass> classes) {
    List<Class> result = new ArrayList<Class>();

    for (XmlClass xmlClass : classes) {
      result.add(xmlClass.getSupportClass());
    }

    Class[] xmlClasses = result.toArray(new Class[classes.size()]);
    Map<Class, Class> withNestedClasses = new HashMap<Class, Class>();
    findAllClasses(xmlClasses, withNestedClasses);

    return withNestedClasses.values().toArray(new Class[withNestedClasses.size()]);
  }

  public static XmlClass[] classesToXmlClasses(Class[] classes) {
    List<XmlClass> result = new ArrayList<XmlClass>();

    for (Class cls : classes) {
      result.add(new XmlClass(cls));
    }

    return result.toArray(new XmlClass[classes.length]);

  }
  
  /**
   * Find all the classes inside this array, including nested ones.
   * @param classes
   * @return
   */
  private static void findAllClasses(Class[] classes, Map<Class, Class> result) {
    for (Class cls : classes) {
      if (!result.containsKey(cls)) {
        result.put(cls, cls);
      }
    }
  }

  public static String[] parseMultiLine(String line) {
    List<String> vResult = new ArrayList<String>();
    if ((null != line) && !"".equals(line.trim())) {
      StringTokenizer st = new StringTokenizer(line, " ");
      while (st.hasMoreTokens()) {
        vResult.add(st.nextToken());
      }
      // Bug in split when passed " " : returns one too many result
      //      result = line.split(" ");
    }

    String[] result = vResult.toArray(new String[vResult.size()]);

    return result;
  }

  /**
   * Writes the content of the sb string buffer to the file named filename in outputDir. If 
   * outDir does not exist, it is created.
   *
   * @param outputDir the output directory (may not exist).
   * @param fileName the filename
   * @param sb the file content
   */
  public static void writeFile(String outputDir, String fileName, StringBuffer sb) {
    writeFile(outputDir, fileName, sb.toString());
  }

  /**
   * Writes the content of the sb string to the file named filename in outDir. If 
   * outDir does not exist, it is created.
   *
   * @param outputDir the output directory (may not exist). If <tt>null</tt> then current directory is used.
   * @param fileName the filename
   * @param sb the file content
   */
  public static void writeFile(String outputDir, String fileName, String sb) {
    final String outDirPath= outputDir != null ? outputDir : "";
    final File outDir= new File(outDirPath);
    writeFile(outDir, fileName, sb);
  }
  
  /**
   * Writes the content of the sb string to the file named filename in outDir. If 
   * outDir does not exist, it is created.
   *
   * @param outDir the output directory (may not exist). If <tt>null</tt> then current directory is used.
   * @param fileName the filename
   * @param sb the file content
   */
  public static void writeFile(File outDir, String fileName, String sb) {
    try {
      if (!outDir.exists()) {
        outDir.mkdirs();
      }
      
      File outputFile = new File(outDir, fileName);
      outputFile.delete();
      outputFile.createNewFile();
      
      writeFile(outputFile, sb);
    }
    catch (IOException e) {
      if (TestRunner.getVerbose() > 1) {
        e.printStackTrace();
      }
      else {
        log("[Utils]", 1, e.getMessage());
      }
    }
  }

  private static void writeFile(File outputFile, String sb) {
{'fraction_non_alphanumeric': 0.11790909090909091, 'fraction_numerical': 0.002, 'mean_word_length': 3.3499406880189797, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '7473290', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3304, 'n_words': 962}