

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.googlecode.jslint4java.Issue;
import com.googlecode.jslint4java.JSLint;
import com.googlecode.jslint4java.JSLintBuilder;
import com.googlecode.jslint4java.Option;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

/**
 * @author iperdomo
 * 
 */
public class JSLintAndYUICompresor2 {

  public static void main(String[] args) {
    final String jsCode = "/* jslint */\n function foo() {\n var x=1;\n return x;\n}\n";
    
	try {
      final StringReader reader = new StringReader(jsCode);
      final JavaScriptCompressor compressor = new JavaScriptCompressor(reader, new ErrorReporter() {

        public void warning(String message, String sourceName, int line, String lineSource,
            int lineOffset) {
          if (line < 0) {
            System.err.println(message);
          } else {
            System.err.println(line + ':' + lineOffset + ':' + message);
          }
        }

        public void error(String message, String sourceName, int line, String lineSource,
            int lineOffset) {
          if (line < 0) {
            System.err.println(message);
          } else {
            System.err.println(line + ':' + lineOffset + ':' + message);
          }
        }

        public EvaluatorException runtimeError(String message, String sourceName, int line,
            String lineSource, int lineOffset) {
          error(message, sourceName, line, lineSource, lineOffset);
          return new EvaluatorException(message);
        }
      });

      final StringWriter writer = new StringWriter();
      final boolean munge = false;
      final boolean verbose = true;
      boolean preserveAllSemiColons = true;
      boolean disableOptimizations = true;

      compressor.compress(writer, -1, munge, verbose, preserveAllSemiColons, disableOptimizations);
      System.out.println(writer.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }

    // Testing 1.3.3
    try {
      JSLint jsLint = new JSLintBuilder().fromDefault();
      jsLint.addOption(Option.BROWSER);
      jsLint.addOption(Option.EQEQEQ);
      jsLint.addOption(Option.EVIL);
      jsLint.addOption(Option.LAXBREAK);

      final List<Issue> issues = jsLint.lint("0", jsCode);

      final StringBuilder sb = new StringBuilder();
      for (Issue issue : issues) {
        sb.append(issue.getLine() + ":" + issue.getCharacter() + ": " + issue.getReason() + "\n");
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
