import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
import jargs.gnu.CmdLineParser;
import java.io.File;
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.nio.charset.Charset;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
public class YUIExecutor {
public void run(String inputFolder, String outputFolder) throws Exception{
File folder = new File(inputFolder);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
process(inputFolder + listOfFiles[i].getName(), outputFolder + listOfFiles[i].getName());
}
}
}
private void process(String inputJSFile, String outputJSFile) {
String[] args = new String[3];
args[0] = inputJSFile;
args[1] = "-o";
args[2] = outputJSFile;
CmdLineParser parser = new CmdLineParser();
jargs.gnu.CmdLineParser.Option typeOpt = parser.addStringOption("type");
jargs.gnu.CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
jargs.gnu.CmdLineParser.Option nomungeOpt = parser.addBooleanOption("nomunge");
jargs.gnu.CmdLineParser.Option linebreakOpt = parser.addStringOption("line-break");
jargs.gnu.CmdLineParser.Option preserveSemiOpt = parser.addBooleanOption("preserve-semi");
jargs.gnu.CmdLineParser.Option disableOptimizationsOpt = parser.addBooleanOption("disable-optimizations");
jargs.gnu.CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
jargs.gnu.CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
jargs.gnu.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 _ex) {
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);
}
});
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();
System.exit(2);
}
} else if (type.equalsIgnoreCase("css")) {
CssCompressor compressor = new CssCompressor(in);
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 (jargs.gnu.CmdLineParser.OptionException _ex) {
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();
}
}
}
return;
}
private static void usage() {
System.out.println("\nUsage: java -jar yuicompressor-x.y.z.jar [options] [input file]\n\nGlobal Options\n -h, --help Displays this information\n --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 -v, --verbose Display informational messages and warnings\n -o <file> Place the output into <file>. Defaults to stdout.\n\nJavaScript Options\n --nomunge Minify only, do not obfuscate\n --preserve-semi Preserve all semicolons\n --disable-optimizations Disable all micro optimizations\n\nIf no input file is specified, it defaults to stdin. In this case, the 'type'\noption is required. Otherwise, the 'type' option is required only if the input\nfile extension is neither 'js' nor 'css'.");
}
}
Link download yuicompressor component: here
Không có nhận xét nào:
Đăng nhận xét