assert input != null;
assert !StringUtils.isEmpty(inputFilename);
assert debugCodeLength > 0;
try {
Timer timer = new Timer();
// logger.debug( "Starting minification for '" + inputFilename + "'...");
Reader in = new StringReader( input );
JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {
public void warning(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
logger.warn("Minifier Warning: " + message);
} else {
logger.warn("Minifier Warning, " + line + ':' + lineOffset + ':' + message);
}
}
public void error(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
logger.warn("Minifier Error: " + message);
} else {
logger.warn("Minifier 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();
try {
Writer out = new StringWriter();
boolean munge = true;
boolean preserveAllSemiColons = false;
boolean disableOptimizations = false;
boolean verbose = false;
int linebreakpos = 0;
compressor.compress(out, linebreakpos, munge, verbose,
preserveAllSemiColons, disableOptimizations);
out.close();
String result = out.toString();
if( logger.isDebugEnabled() ) {
timer.stop();
int compressionPercentage = 100 - (result.length() * 100 / debugCodeLength);
timer.logDebugTimeInMilliseconds( "Finished minification for '" + inputFilename + "'. Debug code length: " + debugCodeLength + ", Minified length: " + result.length() + ", Compression: " + compressionPercentage + "%. Time");
}
return result;
}
catch (UnsupportedEncodingException e) {
logger.warn( "Unable to minify '" + inputFilename + "'.", e );