您的位置:首页 > 其它

第3章 MFC框架程序剖析

2010-12-13 13:04 375 查看
应项目需要,采用yuicompressor对js/css文件进行压缩混淆,于是编写了一个java文件,接收参数目前只开放两个,一个为目录(可以是整个项目路径),一个是是否混淆(默认true)。程序会循环目录下所有的js/css文件,已经是min文件的除外,进行压缩之后覆盖原文件,因此一般用于开发完成测试之前做。日志文件会显示出处理的具体文件,以及由于编写不规范导致无法压缩的文件。个人碰到过一个CSS文件,文件末尾多了写了/*!,整个压缩过程就一直停留在这里了。因此使用此工具要求文件编写要规范。

public class JsAndCssZip {

static Logger logger = Logger.getLogger(JsAndCssZip.class.getName());

int linebreakpos = -1;// 无断行

boolean munge = true;// 混淆

boolean verbose = false;// 显示信息性消息和警告

boolean preserveAllSemiColons = true; // 保留所有分号

boolean disableOptimizations = true; // 禁用所有微优化

public static void main(String[] args) {
PropertyConfigurator.configure("./src/log4j.properties");
if (args == null || args.length == 0) {
logger.error("pls input the file or dir params!");
return;
}
File file = new File(args[0]);
logger.debug("zip path:" + args[0]);
if (!file.exists()) {
logger.error("the file or dir do not exist!");
return;
}
JsAndCssZip inst = new JsAndCssZip();
if (args.length > 1) {
inst.munge = args[1].equalsIgnoreCase("false") ? false : true;
}
logger.debug("munge:" + inst.munge);
Date d = new Date();
logger.debug("start time : " + d);
try {
inst.checkFile(file);
}
catch (Exception e) {
e.printStackTrace();
}
Date d1 = new Date();
logger.debug("end time " + d1);
logger.debug("FINISHED AND COST TIME(ms):" + (d1.getTime() - d.getTime()));
}

public void checkFile(File file) throws Exception {
if (file.getName().endsWith(".svn"))
return;
if (file.isFile()) {
jsZip(file);
return;
}
File[] files = file.listFiles();
if (files == null || files.length == 0)
return;
for (File f : files) {
if (file.getName().endsWith(".svn"))
return;
if (file.isFile()) {
jsZip(file);
continue;
}
checkFile(f);
}
}

public void jsZip(File file) throws Exception {
String fileName = file.getName();
if (fileName.endsWith(".js") == false && fileName.endsWith(".css") == false) {
return;
}
if (fileName.endsWith(".min.js") == true || fileName.endsWith(".min.css") == true) {
return;
}
Reader in = new InputStreamReader(new FileInputStream(file), "utf-8");
String filePath = file.getAbsolutePath();
logger.debug("DEAL FILE:" + filePath);
File tempFile = new File(filePath + ".tempFile");
Writer out = new OutputStreamWriter(new FileOutputStream(tempFile), "utf-8");
if (fileName.endsWith(".js")) {
try {
JavaScriptCompressor jscompressor = new JavaScriptCompressor(in, new ErrorReporter() {
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
if (line < 0) {
// logger.error("[WARNING] " + message);
}
else {
// logger.error("[WARNING] " + line + ':' + lineOffset + ':' + message);
}
}

public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
if (line < 0) {
// logger.error("[ERROR] " + message);
}
else {
// logger.error("[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);
}
});
jscompressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations);
}
catch (org.mozilla.javascript.EvaluatorException e) {
logger.error("[ERROR] " + file.getPath());
out.close();
in.close();
tempFile.delete();
return;
}

}
else if (fileName.endsWith(".css")) {
CssCompressor csscompressor = new CssCompressor(in);
csscompressor.compress(out, linebreakpos);
}

out.close();
in.close();
file.delete();
tempFile.renameTo(file);
tempFile.delete();
}

}

  需依赖yuicompressor.jar。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: