js 打包 压缩后台代码 (java)

package com.hgisServer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Iterator;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

import net.sf.json.JSONObject;

public class BuildApi {

public static void main(String[] args) {
// TODO Auto-generated method stub
String path=Class.class.getClass().getResource("/").getPath();
int num=path.indexOf("/target");
path=path.substring(1, num);
String basePath = path+"/src/main/webapp";//F:\\Product\\hgisWorkspace\\hgis
// 文件合并后将被存放到这个问件中
File jsDebugfile = new File(basePath + "\\hgis_debug.js");
File jsBasefile = new File(basePath + "\\hgis_base.js");
// 合并且返回是否合并成功的标志
File jscompressedFile = new File(basePath+ "\\hgis.js");
try {
// 在合并和压缩前 清空hgis_debug.js
FileOutputStream outjsDebugfile = new FileOutputStream(jsDebugfile);
outjsDebugfile.write(new String("").getBytes());
outjsDebugfile.close();
// 清空hgis_compressed.js
FileOutputStream outhgis_compressed = new FileOutputStream(
jscompressedFile);
outhgis_compressed.write(new String("").getBytes());
outhgis_compressed.close();
// 开始合并
Boolean isMerged = mergeAPIFiles(jsDebugfile, jsBasefile, basePath);
// 如果合并好了 开始压缩API
if (isMerged) {
compress(jsDebugfile, jscompressedFile);
}

} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 合并API
*
* @param json
* 这个 json对象中包含了所有的API文件路径
* @param mergedFilePath
* 合并后的API
* @return
*/
@SuppressWarnings({ "resource", "rawtypes" })
public static boolean mergeAPIFiles(File jsDebugfile, File jsBasefile,
String basePath) {
// 清空hgis_debug.js文件
try {
FileChannel outChannel = null;
FileOutputStream fileO = null;
fileO = new FileOutputStream(jsDebugfile);
outChannel = fileO.getChannel();
doMerge(outChannel, jsBasefile);
File dir = new File(basePath + "\\gisapi\\extension\\HGIS\\");
scaleFile(outChannel, dir);

} catch (Exception e) {
return false;
}
return true;
};

public static void scaleFile(FileChannel outChannel, File dir) {
// 查找引擎的js路径
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
if (fs[i].isDirectory()) {
if(fs[i].getName().contains("Theme")||fs[i].getName().contains("_test")||fs[i].getName().contains("PlotEx"))
continue;
scaleFile(outChannel, fs[i]);

} else {
if (fs[i].getName().contains("_test")||fs[i].getName().contains("_base"))
continue;
doMerge(outChannel, fs[i]);
}
}
}

@SuppressWarnings("resource")
public static boolean doMerge(FileChannel outChannel, File mergeFile) {
try {
// 开始将jsBasefile 写入到合并的js文件里面
FileInputStream in = new FileInputStream(mergeFile);
FileChannel fc = in.getChannel();
ByteBuffer bb = ByteBuffer.allocate(in.available());
while (fc.read(bb) != -1) {
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
} catch (Exception e) {
return false;
}
return true;
}

public static boolean compress(File jsDebugfile, File jsApiFile) {
String fileName = jsDebugfile.getName();
if (!fileName.endsWith(".js")) {
return false;
}
Reader in;
try {
in = new FileReader(jsDebugfile);
Writer out = new FileWriter(jsApiFile);
if (fileName.endsWith(".js")) {
ErrorReporter errorReport = 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);
}
};
JavaScriptCompressor jscompressor = new JavaScriptCompressor(
in, errorReport);
jscompressor.compress(out, -1, true, false, false, false);
} else if (fileName.endsWith(".css")) {
CssCompressor csscompressor = new CssCompressor(in);
csscompressor.compress(out, -1);
}
out.close();
in.close();
return true;

} catch (Exception e) {
e.printStackTrace();
return false;
}
}

}

转载于:https://www.cnblogs.com/jsbrml/p/5614714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值