Skip to content
Browse files

Embedded HTTP server. Requires Java 6.

Using generics from Java 5 and httpserver from Sun Java 6.
Add JSON.org library.
  • Loading branch information...
1 parent c3b3a55 commit b1357749278ac6b0fb228bf02ec4d0c49cfe7c3f @reid reid committed Oct 19, 2010
View
2 ant.properties
@@ -3,6 +3,6 @@ lib.dir = lib
doc.dir = doc
build.dir = build
product.name = yuicompressor
-version.number = 2.4.4
+version.number = 2.4.5pre
jar.name = ${product.name}-${version.number}.jar
dist.package.name = ${product.name}-${version.number}
View
6 build.xml
@@ -20,9 +20,10 @@
includes="**/*.java"
deprecation="off"
debug="on"
- source="1.4">
+ source="6">
<classpath>
<pathelement location="${lib.dir}/jargs-1.0.jar"/>
+ <pathelement location="${lib.dir}/json.jar"/>
<pathelement location="${lib.dir}/rhino-1.6R7.jar"/>
</classpath>
</javac>
@@ -34,6 +35,7 @@
(some of our own classes will override the Rhino classes) -->
<unjar src="${lib.dir}/jargs-1.0.jar" dest="${build.dir}/jar"/>
<unjar src="${lib.dir}/rhino-1.6R7.jar" dest="${build.dir}/jar"/>
+ <unjar src="${lib.dir}/json.jar" dest="${build.dir}/jar"/>
<copy todir="${build.dir}/jar">
<fileset dir="${build.dir}/classes" includes="**/*.class"/>
</copy>
@@ -63,4 +65,4 @@
includes="${dist.package.name}/**/*"/>
</target>
-</project>
+</project>
View
BIN lib/json.jar
Binary file not shown.
View
BIN src/com/yahoo/platform/yui/compressor/CompressorErrorReporter.java
Binary file not shown.
View
239 src/com/yahoo/platform/yui/compressor/CompressorHttpHandler.java
@@ -0,0 +1,239 @@
+package com.yahoo.platform.yui.compressor;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONString;
+import org.json.JSONStringer;
+import org.mozilla.javascript.EvaluatorException;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.HttpURLConnection;
+import java.net.URLDecoder;
+import java.util.LinkedList;
+import java.util.Map;
+
+public class CompressorHttpHandler implements HttpHandler {
+
+ Configuration config;
+
+ class Response implements JSONString {
+ private LinkedList<String> errors = new LinkedList<String>();
+ private LinkedList<String> warnings = new LinkedList<String>();
+ private ByteArrayOutputStream result = new ByteArrayOutputStream();
+
+ public LinkedList<String> getErrors() {
+ return errors;
+ }
+
+ public void setErrors(LinkedList<String> errors) {
+ this.errors = errors;
+ }
+
+ public LinkedList<String> getWarnings() {
+ return warnings;
+ }
+
+ public void setWarnings(LinkedList<String> warnings) {
+ this.warnings = warnings;
+ }
+
+ public ByteArrayOutputStream getResult() {
+ return result;
+ }
+
+ public void setResult(ByteArrayOutputStream result) {
+ this.result = result;
+ }
+
+ public String toJSONString() {
+ try {
+ JSONArray warnings = new JSONArray(getWarnings());
+ JSONArray errors = new JSONArray(getErrors());
+ String result = new String(getResult().toByteArray());
+ return new JSONStringer()
+ .object().key("result").value(result)
+ .key("warnings").value(warnings)
+ .key("errors").value(errors)
+ .endObject().toString();
+ } catch (JSONException ex) {
+ return "JSON Failure: " + ex.getMessage();
+ }
+ }
+ }
+
+ public CompressorHttpHandler (Configuration config) {
+ config.setOutputRaw("json");
+ this.config = config;
+ }
+
+ public void handle (HttpExchange t) throws IOException {
+ // Inherit configuration defaults from the command line.
+ // Make a clone for this request.
+ Configuration config = this.config.clone();
+
+ try {
+ config = parseOptions(config, t); // get desired response format first
+ if (!t.getRequestMethod().toUpperCase().equals("POST")) {
+ throw new ConfigurationException("You must POST urlencoded JavaScript or CSS to this endpoint.");
+ }
+ } catch (ConfigurationException ex) {
+ abort("Bad request", ex, HttpURLConnection.HTTP_BAD_REQUEST, config, t);
+ return;
+ }
+
+ // Theory of operation:
+ // InputStream to String
+ // Decode
+ // String to InputStream
+
+ InputStream requestBody = t.getRequestBody();
+ Reader in = new InputStreamReader(requestBody, config.getCharset());
+ BufferedReader br = new BufferedReader(in);
+ StringBuilder sb = new StringBuilder();
+ String tmp = br.readLine();
+ while (tmp != null) {
+ sb.append(tmp);
+ tmp = br.readLine();
+ }
+ String incoming = sb.toString();
+ incoming = URLDecoder.decode(incoming, config.getCharset());
+
+ // System.err.println(incoming.toCharArray());
+
+ in = new InputStreamReader(new ByteArrayInputStream(incoming.getBytes()));
+
+ Response response;
+
+ try {
+ response = compress(in, config);
+ } catch (EvaluatorException ex) {
+ // Your fault.
+ abort("Syntax error", ex, HttpURLConnection.HTTP_BAD_REQUEST, config, t);
+ return;
+ } catch (IOException ex) {
+ // My fault.
+ abort("Compressor failed", ex, HttpURLConnection.HTTP_INTERNAL_ERROR, config, t);
+ return;
+ }
+
+ respond(HttpURLConnection.HTTP_OK, response, config, t);
+ }
+
+ private void respond (int httpCode, Response response, Configuration config, HttpExchange t) {
+ try {
+
+ OutputStream body = t.getResponseBody();
+
+ String outputFormat = config.getOutput();
+
+ boolean json = false;
+ if (outputFormat.equals("json")) json = true;
+
+ byte[] resultBytes;
+ String str;
+ if (json) {
+ str = response.toJSONString();
+ resultBytes = str.getBytes();
+ } else {
+ if (httpCode != HttpURLConnection.HTTP_OK) {
+ str = "Error: " + response.getErrors().getFirst();
+ resultBytes = str.getBytes();
+ } else {
+ resultBytes = response.getResult().toByteArray();
+ str = new String(resultBytes);
+ }
+ }
+
+ t.sendResponseHeaders(httpCode, str.length());
+
+ body.write(resultBytes);
+ body.close();
+
+ } catch (Exception ex) {
+ // We can't really recover.
+ System.err.println("Fatal error in HTTP server while responding to the request.");
+ ex.printStackTrace();
+ }
+ }
+
+ private void abort (String message, Exception ex, int httpCode, Configuration config, HttpExchange t)
+ throws IOException {
+ String error = message + ": " + ex.getMessage();
+ // System.err.println(error);
+
+ Response response = new Response();
+ LinkedList<String> errors = new LinkedList<String>();
+ errors.add(error);
+ response.setErrors(errors);
+
+ respond(httpCode, response, config, t);
+ }
+
+ private Configuration parseOptions (Configuration config, HttpExchange t)
+ throws ConfigurationException, IOException {
+ Map<String, String> query = (Map<String, String>) t.getAttribute("query");
+
+ for (String key : query.keySet()) {
+ String value = query.get(key);
+ key = key.toLowerCase();
+ value = value.toLowerCase();
+ // System.err.println("parseOptions: " + key + " = " + value);
+ if (key.equals("charset")) {
+ config.setCharset(value);
+ } else if (key.equals("output")) {
+ config.setOutput(value);
+ } else if (key.equals("type")) {
+ config.setInputType(value);
+ } else if (key.equals("lineBreak")) {
+ config.setLineBreak(value);
+ } else if (key.equals("semicolons")) {
+ config.setPreserveSemicolons(value.equals(""));
+ } else if (key.equals("munge")) {
+ config.setMunge(value.equals("1") || value.equals("true"));
+ } else if (key.equals("optimize")) {
+ config.setOptimize(value.equals("1") || value.equals("true"));
+ }
+ }
+
+ return config;
+ }
+
+ private Response compress (Reader in, Configuration config) throws IOException {
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
+ Writer streamWriter = new OutputStreamWriter(result, config.getCharset());
+
+ Response response = new Response();
+
+ if (config.isCss()) {
+
+ CssCompressor compressor = new CssCompressor(in);
+ compressor.compress(streamWriter, config.getLineBreak());
+
+ } else { // config.isJavascript() may also be unset. assume JS anyway.
+
+ CompressorErrorReporter reporter = new CompressorErrorReporter();
+ JavaScriptCompressor compressor = new JavaScriptCompressor(in, reporter);
+ compressor.compress(streamWriter, config);
+ response.setErrors(reporter.getErrors());
+ response.setWarnings(reporter.getWarnings());
+
+ }
+
+ streamWriter.close();
+ response.setResult(result);
+
+ return response;
+ }
+
+}
View
235 src/com/yahoo/platform/yui/compressor/Configuration.java
@@ -0,0 +1,235 @@
+package com.yahoo.platform.yui.compressor;
+
+import jargs.gnu.CmdLineParser;
+
+import java.nio.charset.Charset;
+import java.util.List;
+
+public class Configuration implements Cloneable {
+
+ protected String inputType;
+ protected boolean javascript;
+ protected boolean css;
+
+ protected boolean verbose;
+ protected boolean munge;
+ protected int lineBreak;
+ protected boolean preserveSemicolons;
+ protected boolean optimize;
+ protected boolean help;
+ protected String charset;
+
+ protected int serverPort;
+
+ protected String output;
+ protected List files;
+
+ public Configuration (String args[]) throws ConfigurationException {
+ CmdLineParser cliParser = new CmdLineParser();
+
+ CmdLineParser.Option typeOpt = cliParser.addStringOption("type");
+ CmdLineParser.Option verboseOpt = cliParser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option nomungeOpt = cliParser.addBooleanOption("nomunge");
+ CmdLineParser.Option linebreakOpt = cliParser.addStringOption("line-break");
+ CmdLineParser.Option preserveSemiOpt = cliParser.addBooleanOption("preserve-semi");
+ CmdLineParser.Option disableOptimizationsOpt = cliParser.addBooleanOption("disable-optimizations");
+ CmdLineParser.Option helpOpt = cliParser.addBooleanOption('h', "help");
+ CmdLineParser.Option charsetOpt = cliParser.addStringOption("charset");
+ CmdLineParser.Option serverOpt = cliParser.addStringOption("server");
+ CmdLineParser.Option outputFilenameOpt = cliParser.addStringOption('o', "output");
+
+ try {
+ cliParser.parse(args);
+ } catch (CmdLineParser.OptionException ex) {
+ throw new ConfigurationException("Command line parse failed.");
+ }
+
+ setInputType((String) cliParser.getOptionValue(typeOpt));
+ setVerbose(cliParser.getOptionValue(verboseOpt) != null);
+ setMunge(cliParser.getOptionValue(nomungeOpt) == null);
+ setLineBreak((String) cliParser.getOptionValue(linebreakOpt));
+ setPreserveSemicolons(cliParser.getOptionValue(preserveSemiOpt) != null);
+ setOptimize(cliParser.getOptionValue(disableOptimizationsOpt) == null);
+ setHelp(cliParser.getOptionValue(helpOpt) != null);
+ setCharset((String) cliParser.getOptionValue(charsetOpt));
+ setServerPort((String) cliParser.getOptionValue(serverOpt));
+ setOutput((String) cliParser.getOptionValue(outputFilenameOpt));
+ setFiles(cliParser.getRemainingArgs());
+ }
+
+ // Used by CompressorHttpHandler to provide a request-level
+ // Configuration object that inherits defaults from the CLI config.
+ public Configuration clone () {
+ try {
+ return (Configuration) super.clone();
+ } catch (CloneNotSupportedException e) {
+ return null;
+ }
+ }
+
+ public String getInputType() {
+ return inputType;
+ }
+
+ public void setInputType(String inputType) throws ConfigurationException {
+ if (inputType != null) {
+ inputType = inputType.toLowerCase();
+ if (inputType.equals("js")) {
+ setJavascript(true);
+ setCss(false);
+ } else if (inputType.equals("css")) {
+ setCss(true);
+ setJavascript(false);
+ } else {
+ throw new ConfigurationException("Bad type option.");
+ }
+ }
+ this.inputType = inputType;
+ }
+
+ public boolean isJavascript() {
+ return javascript;
+ }
+
+ public void setJavascript(boolean javascript) {
+ this.javascript = javascript;
+ }
+
+ public boolean isCss() {
+ return css;
+ }
+
+ public void setCss(boolean css) {
+ this.css = css;
+ }
+
+ public boolean isVerbose() {
+ return verbose;
+ }
+
+ public void setVerbose(boolean verbose) {
+ this.verbose = verbose;
+ }
+
+ public boolean isMunge() {
+ return munge;
+ }
+
+ public void setMunge(boolean munge) {
+ this.munge = munge;
+ }
+
+ public int getLineBreak() {
+ return lineBreak;
+ }
+
+ public void setLineBreak(int lineBreak) {
+ this.lineBreak = lineBreak;
+ }
+
+ public void setLineBreak(String lineBreak) throws ConfigurationException {
+ if (lineBreak == null) {
+ setLineBreak(-1);
+ } else {
+ try {
+ setLineBreak(Integer.parseInt(lineBreak, 10));
+ } catch (NumberFormatException ex) {
+ throw new ConfigurationException("Line break option is not a number.");
+ }
+ }
+ }
+
+ public boolean isPreserveSemicolons() {
+ return preserveSemicolons;
+ }
+
+ public void setPreserveSemicolons(boolean preserveSemicolons) {
+ this.preserveSemicolons = preserveSemicolons;
+ }
+
+ public boolean isOptimize() {
+ return optimize;
+ }
+
+ public void setOptimize(boolean optimize) {
+ this.optimize = optimize;
+ }
+
+ public boolean isHelp() {
+ return help;
+ }
+
+ public void setHelp(boolean help) {
+ this.help = help;
+ }
+
+ public String getCharset() {
+ return charset;
+ }
+
+ public void setCharset(String charset) {
+ if (charset == null || !Charset.isSupported(charset)) {
+ charset = "UTF-8";
+ // System.err.println("Using UTF-8");
+ if (this.isVerbose()) {
+ // TODO Log!
+ }
+ }
+ this.charset = charset;
+ }
+
+ public void setServerPort(String serverPort) throws ConfigurationException {
+ if (serverPort == null) return;
+ try {
+ setServerPort(Integer.parseInt(serverPort, 10));
+ } catch (NumberFormatException ex) {
+ throw new ConfigurationException("Server port is not a number.");
+ }
+ }
+
+ public void setServerPort(int serverPort) {
+ this.serverPort = serverPort;
+ }
+
+ public int getServerPort() {
+ return serverPort;
+ }
+
+ public String getOutput() {
+ return output;
+ }
+
+ public void setOutputRaw(String output) {
+ this.output = output;
+ }
+
+ public void setOutput(String output) throws ConfigurationException {
+ if (output != null) {
+ output = output.toLowerCase();
+ if (
+ !( output.equals("json") || output.equals("raw") )
+ && (getServerPort() > 0)
+ ) {
+ throw new ConfigurationException("In server mode, only json or raw output types are allowed.");
+ }
+ }
+ setOutputRaw(output);
+ }
+
+ public List getFiles() {
+ return files;
+ }
+
+ public void setFiles(List files) {
+ if (files.isEmpty()) {
+ files = new java.util.ArrayList();
+ files.add("-"); // read from stdin
+ }
+ this.files = files;
+ }
+
+ public void setFiles(String[] files) {
+ setFiles(java.util.Arrays.asList(files));
+ }
+
+}
View
11 src/com/yahoo/platform/yui/compressor/ConfigurationException.java
@@ -0,0 +1,11 @@
+package com.yahoo.platform.yui.compressor;
+
+public class ConfigurationException extends Exception {
+ public ConfigurationException() {
+ super();
+ }
+
+ public ConfigurationException(String message) {
+ super(message);
+ }
+}
View
64 src/com/yahoo/platform/yui/compressor/HttpQueryFilter.java
@@ -0,0 +1,64 @@
+package com.yahoo.platform.yui.compressor;
+
+import com.sun.net.httpserver.Filter;
+import com.sun.net.httpserver.HttpExchange;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+public class HttpQueryFilter extends Filter {
+
+ public static final String ENCODING = "UTF-8";
+
+ public String description () {
+ return "Parses the query from a request.";
+ }
+
+ public void doFilter (HttpExchange t, Chain chain) throws IOException {
+ try {
+ parseGET(t);
+ } catch (UnsupportedEncodingException ex) {
+ // No UTF-8? Um, ok.
+ throw new IOException(ENCODING + " is not supported.", ex);
+ }
+ chain.doFilter(t);
+ }
+
+ public void parseGET (HttpExchange t) throws UnsupportedEncodingException {
+ URI request = t.getRequestURI();
+ String rawQuery = request.getRawQuery();
+ Map<String, String> query = parser(rawQuery);
+ t.setAttribute("query", query);
+ }
+
+ public Map<String, String> parser (String rawQuery) throws UnsupportedEncodingException {
+ Map<String, String> query = new HashMap<String, String>();
+ if (rawQuery == null) return query;
+
+ StringTokenizer pairs = new StringTokenizer(rawQuery, "&");
+
+ while (pairs.hasMoreTokens()) {
+ StringTokenizer pair = new StringTokenizer(pairs.nextToken(), "=");
+
+ String key = null;
+ String val = "";
+
+ if (pair.hasMoreTokens()) {
+ key = URLDecoder.decode(pair.nextToken(), ENCODING);
+ }
+ if (pair.hasMoreTokens()) {
+ val = URLDecoder.decode(pair.nextToken(), ENCODING);
+ }
+
+ query.put(key, val);
+ }
+
+ return query;
+ }
+
+}
View
11 src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
@@ -533,6 +533,17 @@ public JavaScriptCompressor(Reader in, ErrorReporter reporter)
this.tokens = parse(in, reporter);
}
+ public void compress(Writer out, Configuration config) throws IOException {
+ int linebreakpos = config.getLineBreak();
+ boolean munge = config.isMunge();
+ boolean preserveSemicolons = config.isPreserveSemicolons();
+ boolean disableOptimizations = !config.isOptimize();
+ boolean verbose = config.isVerbose();
+
+ compress(out, linebreakpos, munge, verbose,
+ preserveSemicolons, disableOptimizations);
+ }
+
public void compress(Writer out, int linebreak, boolean munge, boolean verbose,
boolean preserveAllSemiColons, boolean disableOptimizations)
throws IOException {
View
292 src/com/yahoo/platform/yui/compressor/YUICompressor.java
@@ -1,184 +1,148 @@
/*
* YUI Compressor
* Author: Julien Lecomte - http://www.julienlecomte.net/
- * Copyright (c) 2009 Yahoo! Inc. All rights reserved.
+ * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
package com.yahoo.platform.yui.compressor;
-import jargs.gnu.CmdLineParser;
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
-import java.io.*;
-import java.nio.charset.Charset;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.InetSocketAddress;
+import java.util.List;
public class YUICompressor {
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);
+ Configuration config = new Configuration(args);
- Boolean help = (Boolean) parser.getOptionValue(helpOpt);
- if (help != null && help.booleanValue()) {
+ if (config.isHelp()) {
usage();
System.exit(0);
}
- boolean verbose = parser.getOptionValue(verboseOpt) != null;
+ if (config.getServerPort() > 0) {
+ server(config);
+ } else if (config.getFiles().isEmpty()) {
+ throw new ConfigurationException("Filename or server option required.");
+ } else {
+ compress(config);
+ }
- String charset = (String) parser.getOptionValue(charsetOpt);
- if (charset == null || !Charset.isSupported(charset)) {
- // charset = System.getProperty("file.encoding");
- // if (charset == null) {
- // charset = "UTF-8";
- // }
+ } catch (ConfigurationException e) {
+ System.err.println("Error: " + e.getMessage());
+ usage();
+ System.exit(1);
+ } catch (IOException e) {
+ System.err.println("IO Error: " + e.getMessage());
+ System.exit(1);
+ }
+ }
- // UTF-8 seems to be a better choice than what the system is reporting
- charset = "UTF-8";
+ private static void server(Configuration config) throws IOException {
+ System.err.println("Server starting on port " + config.getServerPort() + ".");
+ HttpHandler compressor = new CompressorHttpHandler(config);
+ HttpServer server = HttpServer.create(new InetSocketAddress(config.getServerPort()), 0);
+ HttpContext context = server.createContext("/compress", compressor);
+ context.getFilters().add(0, new HttpQueryFilter());
+ server.setExecutor(null);
+ server.start();
+ }
+ private static void compress(Configuration config) {
+ Reader in = null;
+ Writer out = null;
- if (verbose) {
- System.err.println("\n[INFO] Using charset " + charset);
- }
- }
+ String charset = config.getCharset();
- 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);
- }
- }
+ int linebreakpos = config.getLineBreak();
- String type = (String) parser.getOptionValue(typeOpt);
- if (type != null && !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
- usage();
- System.exit(1);
- }
+ String type = config.getInputType();
- String[] fileArgs = parser.getRemainingArgs();
- java.util.List files = java.util.Arrays.asList(fileArgs);
- if (files.isEmpty()) {
- files = new java.util.ArrayList();
- files.add("-"); // read from stdin
- }
+ List files = config.getFiles();
- String output = (String) parser.getOptionValue(outputFilenameOpt);
- String pattern[] = output != null ? output.split(":") : new String[0];
+ String output = config.getOutput();
+ String pattern[] = output != null ? output.split(":") : new String[0];
- java.util.Iterator filenames = files.iterator();
- while(filenames.hasNext()) {
- String inputFilename = (String)filenames.next();
+ java.util.Iterator filenames = files.iterator();
- try {
- if (inputFilename.equals("-")) {
+ while(filenames.hasNext()) {
- in = new InputStreamReader(System.in, charset);
+ String inputFilename = (String)filenames.next();
- } else {
+ try {
+ if (inputFilename.equals("-")) {
- if (type == null) {
- int idx = inputFilename.lastIndexOf('.');
- if (idx >= 0 && idx < inputFilename.length() - 1) {
- type = inputFilename.substring(idx + 1);
- }
- }
+ in = new InputStreamReader(System.in, charset);
- if (type == null || !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
- usage();
- System.exit(1);
- }
+ } else {
- in = new InputStreamReader(new FileInputStream(inputFilename), charset);
+ if (type == null) {
+ int idx = inputFilename.lastIndexOf('.');
+ if (idx >= 0 && idx < inputFilename.length() - 1) {
+ try {
+ config.setInputType(inputFilename.substring(idx + 1));
+ } catch (ConfigurationException e) {
+ usage();
+ System.exit(1);
+ }
+ }
}
- String outputFilename = output;
- // if a substitution pattern was passed in
- if (pattern.length > 1 && files.size() > 1) {
- outputFilename = inputFilename.replaceFirst(pattern[0], pattern[1]);
- }
+ in = new InputStreamReader(new FileInputStream(inputFilename), charset);
+ }
- if (type.equalsIgnoreCase("js")) {
+ String outputFilename = output;
+ // if a substitution pattern was passed in
+ if (pattern.length > 1 && files.size() > 1) {
+ outputFilename = inputFilename.replaceFirst(pattern[0], pattern[1]);
+ }
- try {
+ if (config.isJavascript()) {
- JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {
+ try {
- 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);
- }
- }
+ JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {
- 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 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 EvaluatorException runtimeError(String message, String sourceName,
- int line, String lineSource, int lineOffset) {
- error(message, sourceName, line, lineSource, lineOffset);
- return new EvaluatorException(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);
}
- });
-
- // 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);
+ 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.
@@ -190,37 +154,60 @@ public EvaluatorException runtimeError(String message, String sourceName,
out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
}
- compressor.compress(out, linebreakpos);
+ boolean munge = !config.isMunge();
+ boolean preserveSemicolons = config.isPreserveSemicolons();
+ boolean disableOptimizations = config.isOptimize();
+ boolean verbose = config.isVerbose();
+ compressor.compress(out, linebreakpos, munge, verbose,
+ preserveSemicolons, disableOptimizations);
+
+ } catch (EvaluatorException e) {
+
+ e.printStackTrace();
+ // Return a special error code used specifically by the web front-end.
+ System.exit(2);
+
}
- } catch (IOException e) {
+ } else if (config.isCss()) {
- e.printStackTrace();
- System.exit(1);
+ CssCompressor compressor = new CssCompressor(in);
- } finally {
+ // 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 (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ if (outputFilename == null) {
+ out = new OutputStreamWriter(System.out, charset);
+ } else {
+ out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
}
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ compressor.compress(out, linebreakpos);
+ }
+ } catch (IOException e) {
+
+ e.printStackTrace();
+ System.exit(1);
+
+ } finally {
+
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
}
}
- }
- } catch (CmdLineParser.OptionException e) {
- usage();
- System.exit(1);
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
}
@@ -233,6 +220,7 @@ private static void usage() {
+ " --type <js|css> Specifies the type of the input file\n"
+ " --charset <charset> Read the input file using <charset>\n"
+ " --line-break <column> Insert a line break after the specified column number\n"
+ + " --server <port> Start a server on <port>.\n"
+ " -v, --verbose Display informational messages and warnings\n"
+ " -o <file> Place the output into <file>. Defaults to stdout.\n"
+ " Multiple files can be processed using the following syntax:\n"

0 comments on commit b135774

Please sign in to comment.
Something went wrong with that request. Please try again.