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(" [WARNING] " + message);
else
System.err.println(" [WARNING] " + line + ':'
+ lineOffset + ':' + message);
}

public void error(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0)
System.err.println(" [ERROR] " + message);
else
System.err.println(" [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;
}
}

}

Right! is "Fuck GIS",but don't think too much; It means reach a high during playing GIS. Come on!
原文地址:https://www.cnblogs.com/jsbrml/p/5614714.html