当前位置: 代码迷 >> HTML/CSS >> java YUI压缩JS跟CSS
  详细解决方案

java YUI压缩JS跟CSS

热度:630   发布时间:2013-12-23 14:30:52.0
java YUI压缩JS和CSS

项目需要压缩JS和CSS,文件和文件夹太多,一一处理又比较麻烦,写个程序吧:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

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

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


public class TestJsZip {
		//要处理的目录
		static File dir=new File("E:\\js");
		
		static int linebreakpos = -1;
		static boolean munge=true;
		static boolean verbose=false;
		static boolean preserveAllSemiColons=false;
		static boolean disableOptimizations=false;
		public static void main(String[] args) throws Exception{
			checkFile(dir);
		}

		public static void checkFile(File file) throws Exception{
			File[] files=file.listFiles();
			if(files==null||files.length==0)
				return;
			
			for(File f:files){
				if(f.getName().endsWith(".svn")){
					continue;
				}
				if(f.isFile()){
					jsZip(f);
					System.out.println("文件==="+f.getName());
					continue;
				}else{
					System.out.println("目录=="+f.getName());
					checkFile(f);
				}
			}
		}
		public static void jsZip(File file) throws Exception{
			String fileName=file.getName();
			if(!fileName.endsWith(".js")&&!fileName.endsWith(".css")){
				return;
			}
			Reader in = new InputStreamReader(new FileInputStream(file),"utf-8");
			String filePath=file.getAbsolutePath();
			File tempFile=new File(filePath+".tempFile");
			Writer out = new OutputStreamWriter(new FileOutputStream(tempFile),"utf-8");
			if(fileName.endsWith(".js")){
				JavaScriptCompressor jscompressor=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);
		            }
		        });
				
				jscompressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations);
			}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();
		}
}

?

  相关解决方案