diff --git a/ant.properties b/ant.properties
index 59683d3..f332293 100644
--- a/ant.properties
+++ b/ant.properties
@@ -3,6 +3,6 @@ lib.dir = lib
doc.dir = doc
build.dir = build
product.name = yuicompressor
-version.number = 2.4.5pre
+version.number = 2.4.4
jar.name = ${product.name}-${version.number}.jar
dist.package.name = ${product.name}-${version.number}
diff --git a/build.xml b/build.xml
index 12d52b6..3836cbe 100644
--- a/build.xml
+++ b/build.xml
@@ -20,10 +20,9 @@
includes="**/*.java"
deprecation="off"
debug="on"
- source="6">
+ source="1.4">
-
@@ -35,7 +34,6 @@
(some of our own classes will override the Rhino classes) -->
-
@@ -65,4 +63,4 @@
includes="${dist.package.name}/**/*"/>
-
+
\ No newline at end of file
diff --git a/lib/json.jar b/lib/json.jar
deleted file mode 100644
index 5f808ef..0000000
Binary files a/lib/json.jar and /dev/null differ
diff --git a/src/com/yahoo/platform/yui/compressor/CompressorErrorReporter.java b/src/com/yahoo/platform/yui/compressor/CompressorErrorReporter.java
deleted file mode 100644
index b95bdec..0000000
Binary files a/src/com/yahoo/platform/yui/compressor/CompressorErrorReporter.java and /dev/null differ
diff --git a/src/com/yahoo/platform/yui/compressor/CompressorHttpHandler.java b/src/com/yahoo/platform/yui/compressor/CompressorHttpHandler.java
deleted file mode 100644
index d784a01..0000000
--- a/src/com/yahoo/platform/yui/compressor/CompressorHttpHandler.java
+++ /dev/null
@@ -1,240 +0,0 @@
-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 errors = new LinkedList();
- private LinkedList warnings = new LinkedList();
- private ByteArrayOutputStream result = new ByteArrayOutputStream();
-
- public LinkedList getErrors() {
- return errors;
- }
-
- public void setErrors(LinkedList errors) {
- this.errors = errors;
- }
-
- public LinkedList getWarnings() {
- return warnings;
- }
-
- public void setWarnings(LinkedList 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("PUT")) {
- throw new ConfigurationException("You must PUT 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);
- sb.append("\n");
- tmp = br.readLine();
- }
- String incoming = sb.toString();
- // incoming = URLDecoder.decode(incoming, config.getCharset());
-
- // System.err.print(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 errors = new LinkedList();
- errors.add(error);
- response.setErrors(errors);
-
- respond(httpCode, response, config, t);
- }
-
- private Configuration parseOptions (Configuration config, HttpExchange t)
- throws ConfigurationException, IOException {
- Map query = (Map) 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;
- }
-
-}
diff --git a/src/com/yahoo/platform/yui/compressor/Configuration.java b/src/com/yahoo/platform/yui/compressor/Configuration.java
deleted file mode 100644
index 7392630..0000000
--- a/src/com/yahoo/platform/yui/compressor/Configuration.java
+++ /dev/null
@@ -1,235 +0,0 @@
-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));
- }
-
-}
diff --git a/src/com/yahoo/platform/yui/compressor/ConfigurationException.java b/src/com/yahoo/platform/yui/compressor/ConfigurationException.java
deleted file mode 100644
index 1bde77f..0000000
--- a/src/com/yahoo/platform/yui/compressor/ConfigurationException.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.yahoo.platform.yui.compressor;
-
-public class ConfigurationException extends Exception {
- public ConfigurationException() {
- super();
- }
-
- public ConfigurationException(String message) {
- super(message);
- }
-}
\ No newline at end of file
diff --git a/src/com/yahoo/platform/yui/compressor/HttpQueryFilter.java b/src/com/yahoo/platform/yui/compressor/HttpQueryFilter.java
deleted file mode 100644
index c4d46eb..0000000
--- a/src/com/yahoo/platform/yui/compressor/HttpQueryFilter.java
+++ /dev/null
@@ -1,64 +0,0 @@
-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 query = parser(rawQuery);
- t.setAttribute("query", query);
- }
-
- public Map parser (String rawQuery) throws UnsupportedEncodingException {
- Map query = new HashMap();
- 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;
- }
-
-}
diff --git a/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java b/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
index da85eba..8143208 100644
--- a/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
+++ b/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
@@ -533,17 +533,6 @@ 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 {
diff --git a/src/com/yahoo/platform/yui/compressor/YUICompressor.java b/src/com/yahoo/platform/yui/compressor/YUICompressor.java
index 3c74364..1ec9d66 100644
--- a/src/com/yahoo/platform/yui/compressor/YUICompressor.java
+++ b/src/com/yahoo/platform/yui/compressor/YUICompressor.java
@@ -1,148 +1,184 @@
/*
* YUI Compressor
* Author: Julien Lecomte - http://www.julienlecomte.net/
- * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
+ * Copyright (c) 2009 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 com.sun.net.httpserver.HttpContext;
-import com.sun.net.httpserver.HttpHandler;
-import com.sun.net.httpserver.HttpServer;
+import jargs.gnu.CmdLineParser;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
-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;
+import java.io.*;
+import java.nio.charset.Charset;
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 {
- Configuration config = new Configuration(args);
+ parser.parse(args);
- if (config.isHelp()) {
+ Boolean help = (Boolean) parser.getOptionValue(helpOpt);
+ if (help != null && help.booleanValue()) {
usage();
System.exit(0);
}
- if (config.getServerPort() > 0) {
- server(config);
- } else if (config.getFiles().isEmpty()) {
- throw new ConfigurationException("Filename or server option required.");
- } else {
- compress(config);
- }
+ boolean verbose = parser.getOptionValue(verboseOpt) != null;
- } 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);
- }
- }
-
- 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;
+ String charset = (String) parser.getOptionValue(charsetOpt);
+ if (charset == null || !Charset.isSupported(charset)) {
+ // charset = System.getProperty("file.encoding");
+ // if (charset == null) {
+ // charset = "UTF-8";
+ // }
- String charset = config.getCharset();
+ // UTF-8 seems to be a better choice than what the system is reporting
+ charset = "UTF-8";
- int linebreakpos = config.getLineBreak();
- String type = config.getInputType();
+ if (verbose) {
+ System.err.println("\n[INFO] Using charset " + charset);
+ }
+ }
- List files = config.getFiles();
+ 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 output = config.getOutput();
- String pattern[] = output != null ? output.split(":") : new String[0];
+ String type = (String) parser.getOptionValue(typeOpt);
+ if (type != null && !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
+ usage();
+ System.exit(1);
+ }
- java.util.Iterator filenames = files.iterator();
+ 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
+ }
- while(filenames.hasNext()) {
+ String output = (String) parser.getOptionValue(outputFilenameOpt);
+ String pattern[] = output != null ? output.split(":") : new String[0];
- String inputFilename = (String)filenames.next();
+ java.util.Iterator filenames = files.iterator();
+ while(filenames.hasNext()) {
+ String inputFilename = (String)filenames.next();
- try {
- if (inputFilename.equals("-")) {
+ try {
+ if (inputFilename.equals("-")) {
- in = new InputStreamReader(System.in, charset);
+ in = new InputStreamReader(System.in, charset);
- } else {
+ } else {
- 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);
+ 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);
}
- in = new InputStreamReader(new FileInputStream(inputFilename), charset);
- }
+ String outputFilename = output;
+ // if a substitution pattern was passed in
+ if (pattern.length > 1 && files.size() > 1) {
+ outputFilename = inputFilename.replaceFirst(pattern[0], pattern[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]);
- }
+ if (type.equalsIgnoreCase("js")) {
- if (config.isJavascript()) {
+ try {
- try {
+ JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {
- 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 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 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);
}
- }
+ });
- 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.
@@ -154,60 +190,37 @@ public EvaluatorException runtimeError(String message, String sourceName,
out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
}
- 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);
-
+ compressor.compress(out, linebreakpos);
}
- } else if (config.isCss()) {
-
- 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);
- }
+ } catch (IOException e) {
- compressor.compress(out, linebreakpos);
- }
- } catch (IOException e) {
+ e.printStackTrace();
+ System.exit(1);
- e.printStackTrace();
- System.exit(1);
+ } finally {
- } finally {
-
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
- }
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
}
}
+ } catch (CmdLineParser.OptionException e) {
+
+ usage();
+ System.exit(1);
}
}
@@ -220,7 +233,6 @@ private static void usage() {
+ " --type Specifies the type of the input file\n"
+ " --charset Read the input file using \n"
+ " --line-break Insert a line break after the specified column number\n"
- + " --server Start a server on .\n"
+ " -v, --verbose Display informational messages and warnings\n"
+ " -o Place the output into . Defaults to stdout.\n"
+ " Multiple files can be processed using the following syntax:\n"