js、css处理工具【压缩、加密、混淆】

/**
 * js、css处理工具【压缩、加密、混淆】
 *
 * 需要 yuicompressor-2.4.2.jar支持
 *
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

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

/**
 * js、css处理工具【压缩、加密、混淆】
 * 
 * 需要 yuicompressor-2.4.2.jar支持
 * 
 *
 */
public class JsCssPluginsUtil {

	public static final int JS = 0;
	public static final int CSS = 1;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path = "G:/ini.js";
		String topath = "G:/ini_main.js";
		JsCssPluginsUtil.compress(JS, path, topath);
	}

	/**
	 * 压缩方法
	 * @param type
	 * @param path
	 * @param topath
	 * 
	 * md发现一个漏洞 当遇到文件无法解析或有问题时 会报空指针 擦 解决方法 将文件修复为标准无问题  20130119 
	 * 
	 */
	public static void compress(int type, String path, String topath){
		
		if(type == JsCssPluginsUtil.CSS){
			
			InputStreamReader in = null;
			OutputStreamWriter out = null;
			
			try {
				
			in = new InputStreamReader(new FileInputStream(path), "utf-8");
			CssCompressor jsc = new CssCompressor(in);
			//String compressedFileName = file.getParent() + File.separator + name.substring(13);
			out = new OutputStreamWriter(new FileOutputStream(topath), "utf-8");
			
			jsc.compress(out, -1);
			
			
			out.flush();
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(type == JsCssPluginsUtil.JS){

			
			InputStreamReader in = null;
			OutputStreamWriter out = null;
			
			try {
			in = new InputStreamReader(new FileInputStream(path), "utf-8");
			JavaScriptCompressor jsc = new JavaScriptCompressor(in, new JavaScriptErrorReporter());
			//String compressedFileName = file.getParent() + File.separator + name.substring(13);
			out = new OutputStreamWriter(new FileOutputStream(topath), "utf-8");
			
			jsc.compress(out, -1, true, true, true, true);
			
			
			out.flush();
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}else{
			new Exception("不支持此类型的文件压缩!");
		}
	}
}

 

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

public class JavaScriptErrorReporter implements ErrorReporter {

	@Override
	public void error(String arg0, String arg1, int arg2, String arg3, int arg4) {
		// TODO Auto-generated method stub

	}

	@Override
	public EvaluatorException runtimeError(String arg0, String arg1, int arg2,
			String arg3, int arg4) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void warning(String arg0, String arg1, int arg2, String arg3,
			int arg4) {
		// TODO Auto-generated method stub

	}

}

 

你可能感兴趣的:(yuiCompressor)