<commit_before>/* -*- mode: Java; c-basic-offset: 2; -*- */

/**
 * SWF9 calls to external compiler
 *
 * @author <EMAIL>
 * @author <EMAIL>
 * @description: JavaScript -> ActionScript3 translator, calling AS3 compiler -> SW9 
 *
 */

package org.openlaszlo.sc;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openlaszlo.sc.parser.*;
import org.openlaszlo.utils.FileUtils;
import org.openlaszlo.server.LPS;

import org.apache.commons.collections.LRUMap;

/**
 * The SWF9External manages communication with the
 * external compiler - generation of source files,
 * calling the compiler, packaging up the result and
 * interpreting error messages.
 *
 * It is expected that a new SWF9External object
 * be created for each chunk of compilation.  Each
 * new SWF9External gets a new temporary 'work' directory
 * for compilation, and verifies classname uniqueness
 * within that space.
 */
public class SWF9External {

  /** Number of errors shown before truncating */
  static public final int MAX_ERRORS_SHOWN = 50;

  /**
   * A directory we create under the Java runtime's temp dir,
   * that contains our compilation work directories, one for each compilation.
   */
  public static final String WORK_DIR_PARENT = "lzswf9";

  /**
   * The prefix for naming the work directories, which appear
   * under the WORK_DIR_PARENT in the Java runtime's temp dir.
   * For example, /tmp/lzswf9/lzgen...., although /tmp
   * may be replaced by something else when running within
   * tomcat or another application server.
   */
  public static final String WORK_DIR_PREFIX = "lzgen";

  private File workdir;
  private Compiler.OptionMap options;
  private ScriptCompilerInfo mInfo;

  static public String optionsFilename = "LZC_COMPILER_OPTIONS";


  /*
   * Used by getFileNameForClassName to prevent filename conflicts.
   * The key is the 'tolower' name, the value is an ArrayList of
   * UniqueGlobalName objects.
   */
  private HashMap uniqueFileNames = new HashMap();

  public class UniqueGlobalName {
    String globalName;
    boolean isClass;
    int subdirnum;    // subdirector number, 0 means top level
  }

  private int maxSubdirnum = 0;

  // For incremental mode, check if last compile had same options as current compile.
  boolean compilerOptionsChanged() {
    try {
      File optsfile = new File(workDirectoryName(optionsFilename));
      String newOpts = optionsToString();
      if (optsfile.exists()) {
        String prevOpts = FileUtils.readFileString(optsfile);
        System.err.println("prevOpts: "+prevOpts);
        System.err.println("newOpts: "+newOpts);
        return !newOpts.equals(prevOpts);
      } else {
        return true;
      }
    }
    catch (IOException ioe) {
      throw new CompilerError("compilerOptionsChanged: error while reading incremental compiler options file: " + ioe);
    }
  }

  // For incremental mode, write out the compiler options in a persistent file in the working dir
  void writeOptionsFile() {
    try {
      File optsfile = new File(workDirectoryName(optionsFilename));
      String newOpts = optionsToString();
      StringReader from = new StringReader(newOpts);
      // Rewrite the compiler options file with current options
      FileWriter outw = new FileWriter(optsfile);
      FileUtils.send(from, outw);
      outw.close();
    }
    catch (IOException ioe) {
      throw new CompilerError("writeOptionsFile: cannot write incremental compiler options file: " + ioe);
    }
  }




    private String optionsToString() {<commit_msg>Change 20100310-hqm-e by <EMAIL> on 2010-03-10 12:41:04 EST     in /Users/hqm/openlaszlo/trunk     for http://svn.openlaszlo.org/openlaszlo/trunk<commit_after>/* -*- mode: Java; c-basic-offset: 2; -*- */

/**
 * SWF9 calls to external compiler
 *
 * @author <EMAIL>
 * @author <EMAIL>zlo.org
 * @description: JavaScript -> ActionScript3 translator, calling AS3 compiler -> SW9 
 *
 */

package org.openlaszlo.sc;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openlaszlo.sc.parser.*;
import org.openlaszlo.utils.FileUtils;
import org.openlaszlo.server.LPS;

import org.apache.commons.collections.LRUMap;

/**
 * The SWF9External manages communication with the
 * external compiler - generation of source files,
 * calling the compiler, packaging up the result and
 * interpreting error messages.
 *
 * It is expected that a new SWF9External object
 * be created for each chunk of compilation.  Each
 * new SWF9External gets a new temporary 'work' directory
 * for compilation, and verifies classname uniqueness
 * within that space.
 */
public class SWF9External {

  /** Number of errors shown before truncating */
  static public final int MAX_ERRORS_SHOWN = 50;

  /**
   * A directory we create under the Java runtime's temp dir,
   * that contains our compilation work directories, one for each compilation.
   */
  public static final String WORK_DIR_PARENT = "lzswf9";

  /**
   * The prefix for naming the work directories, which appear
   * under the WORK_DIR_PARENT in the Java runtime's temp dir.
   * For example, /tmp/lzswf9/lzgen...., although /tmp
   * may be replaced by something else when running within
   * tomcat or another application server.
   */
  public static final String WORK_DIR_PREFIX = "lzgen";

  private File workdir;
  private Compiler.OptionMap options;
  private ScriptCompilerInfo mInfo;

  static public String optionsFilename = "LZC_COMPILER_OPTIONS";


  /*
   * Used by getFileNameForClassName to prevent filename conflicts.
   * The key is the 'tolower' name, the value is an ArrayList of
   * UniqueGlobalName objects.
   */
  private HashMap uniqueFileNames = new HashMap();

  public class UniqueGlobalName {
    String globalName;
    boolean isClass;
    int subdirnum;    // subdirector number, 0 means top level
  }

  private int maxSubdirnum = 0;

  // For incremental mode, check if last compile had same options as current compile.
  boolean compilerOptionsChanged() {
    try {
      File optsfile = new File(workDirectoryName(optionsFilename));
      String newOpts = optionsToString();
      if (optsfile.exists()) {
        String prevOpts = FileUtils.readFileString(optsfile);
        return !newOpts.equals(prevOpts);
      } else {
        return true;
      }
    }
    catch (IOException ioe) {
      throw new CompilerError("compilerOptionsChanged: error while reading incremental compiler options file: " + ioe);
    }
  }

  // For incremental mode, write out the compiler options in a persistent file in the working dir
  void writeOptionsFile() {
    try {
      File optsfile = new File(workDirectoryName(optionsFilename));
      String newOpts = optionsToString();
      StringReader from = new StringReader(newOpts);
      // Rewrite the compiler options file with current options
      FileWriter outw = new FileWriter(optsfile);
      FileUtils.send(from, outw);
      outw.close();
    }
    catch (IOException ioe) {
      throw new CompilerError("writeOptionsFile: cannot write incremental compiler options file: " + ioe);
    }
  }




    private String optionsToString() {
{'fraction_non_alphanumeric': 0.08725857996387383, 'fraction_numerical': 0.0072252327358621644, 'mean_word_length': 3.3863497867154173, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3016733', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1995, 'n_words': 711}