Search in sources :

Example 1 with ErrorReporter

use of yuicompressor.org.mozilla.javascript.ErrorReporter in project hackpad by dropbox.

the class YUICompressor method main.

public static void main(String[] args) {
    CmdLineParser parser = new CmdLineParser();
    CmdLineParser.Option typeOpt = parser.addStringOption("type");
    CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
    CmdLineParser.Option nomungeOpt = parser.addBooleanOption("nomunge");
    CmdLineParser.Option linebreakOpt = parser.addStringOption("line-break");
    CmdLineParser.Option preserveSemiOpt = parser.addBooleanOption("preserve-semi");
    CmdLineParser.Option disableOptimizationsOpt = parser.addBooleanOption("disable-optimizations");
    CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
    CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
    CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");
    Reader in = null;
    Writer out = null;
    try {
        parser.parse(args);
        Boolean help = (Boolean) parser.getOptionValue(helpOpt);
        if (help != null && help.booleanValue()) {
            usage();
            System.exit(0);
        }
        boolean verbose = parser.getOptionValue(verboseOpt) != null;
        String charset = (String) parser.getOptionValue(charsetOpt);
        if (charset == null || !Charset.isSupported(charset)) {
            charset = System.getProperty("file.encoding");
            if (charset == null) {
                charset = "UTF-8";
            }
            if (verbose) {
                System.err.println("\n[INFO] Using charset " + charset);
            }
        }
        String[] fileArgs = parser.getRemainingArgs();
        String type = (String) parser.getOptionValue(typeOpt);
        if (fileArgs.length == 0) {
            if (type == null || !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
                usage();
                System.exit(1);
            }
            in = new InputStreamReader(System.in, charset);
        } else {
            if (type != null && !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
                usage();
                System.exit(1);
            }
            String inputFilename = fileArgs[0];
            if (type == null) {
                int idx = inputFilename.lastIndexOf('.');
                if (idx >= 0 && idx < inputFilename.length() - 1) {
                    type = inputFilename.substring(idx + 1);
                }
            }
            if (type == null || !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
                usage();
                System.exit(1);
            }
            in = new InputStreamReader(new FileInputStream(inputFilename), charset);
        }
        int linebreakpos = -1;
        String linebreakstr = (String) parser.getOptionValue(linebreakOpt);
        if (linebreakstr != null) {
            try {
                linebreakpos = Integer.parseInt(linebreakstr, 10);
            } catch (NumberFormatException e) {
                usage();
                System.exit(1);
            }
        }
        String outputFilename = (String) parser.getOptionValue(outputFilenameOpt);
        if (type.equalsIgnoreCase("js")) {
            try {
                JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {

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

                    public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
                        if (line < 0) {
                            System.err.println("\n[ERROR] " + message);
                        } else {
                            System.err.println("\n[ERROR] " + 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);
                    }
                });
                // Close the input stream first, and then open the output stream,
                // in case the output file should override the input file.
                in.close();
                in = null;
                if (outputFilename == null) {
                    out = new OutputStreamWriter(System.out, charset);
                } else {
                    out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
                }
                boolean munge = parser.getOptionValue(nomungeOpt) == null;
                boolean preserveAllSemiColons = parser.getOptionValue(preserveSemiOpt) != null;
                boolean disableOptimizations = parser.getOptionValue(disableOptimizationsOpt) != null;
                compressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations);
            } catch (EvaluatorException e) {
                e.printStackTrace();
                // Return a special error code used specifically by the web front-end.
                System.exit(2);
            }
        } else if (type.equalsIgnoreCase("css")) {
            CssCompressor compressor = new CssCompressor(in);
            // Close the input stream first, and then open the output stream,
            // in case the output file should override the input file.
            in.close();
            in = null;
            if (outputFilename == null) {
                out = new OutputStreamWriter(System.out, charset);
            } else {
                out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
            }
            compressor.compress(out, linebreakpos);
        }
    } catch (CmdLineParser.OptionException e) {
        usage();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : CmdLineParser(jargs.gnu.CmdLineParser) ErrorReporter(yuicompressor.org.mozilla.javascript.ErrorReporter) EvaluatorException(yuicompressor.org.mozilla.javascript.EvaluatorException)

Aggregations

CmdLineParser (jargs.gnu.CmdLineParser)1 ErrorReporter (yuicompressor.org.mozilla.javascript.ErrorReporter)1 EvaluatorException (yuicompressor.org.mozilla.javascript.EvaluatorException)1