記憶盒子

把記憶裝進(jìn)灰色的盒子,封裝起來,那年那月,重新拾起。

首頁搜索目錄
search
當(dāng)前主題: 軟件編程

java實(shí)現(xiàn)的js代碼壓縮程序

作者:Kaka    時(shí)間:2012-8-12 0:42:36    瀏覽:    評(píng)論:16

      wabkaka之前新增js代碼壓縮功能后,收到一些網(wǎng)友的來信,給我反饋了一些意見。在談到實(shí)現(xiàn)方法的時(shí)候,網(wǎng)友也談到了自己的心得,比如什么方法實(shí)現(xiàn)起來比較簡(jiǎn)單,什么方法執(zhí)行起來速度最快,什么方法的壓縮率最高,等等。

      一位網(wǎng)友給我提供了一份用java實(shí)現(xiàn)的js代碼壓縮程序,因我機(jī)器沒有配置java環(huán)境,故沒有測(cè)試,現(xiàn)收藏起來供自己日后有需要時(shí)使用,有興趣的網(wǎng)友也可以拿去研究研究吧。

      代碼如下:

package app.user.model;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import org.junit.Test;
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{
 File dir=new File("E:\\workspace\\JSCompress\\test.js");
 int linebreakpos = -1;
 boolean munge=true;
 boolean verbose=false;
 boolean preserveAllSemiColons=false;
 boolean disableOptimizations=false;
 @Test
 public void testMain() throws Exception{
  checkFile(dir);
 }
 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();
  System.out.println(fileName);
  if(fileName.endsWith(".js")==false&&fileName.endsWith(".css")==false){
   return;
  }
  Reader in=new FileReader(file);
  String filePath=file.getAbsolutePath();
  File tempFile=new File(filePath+".tempFile");
  Writer out=new FileWriter(tempFile);
  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();
 }
}

      該程序使用了yahoo的YUI組件,下載地址:http://yuilibrary.com/downloads/#yuicompressor,英文幫助文檔地址:http://developer.yahoo.com/yui/compressor/,所以測(cè)試該代碼的時(shí)候請(qǐng)先去下載該組件。

x

標(biāo)簽: Java  

※ 網(wǎng)站速度慢?試試網(wǎng)站自動(dòng)優(yōu)化工具 ※

上一篇: 百度SEO:標(biāo)題帶網(wǎng)址屬于過度優(yōu)化行為
下一篇: windows xp iis5.1安裝配置php環(huán)境

發(fā)表評(píng)論:

◎歡迎參與討論,請(qǐng)?jiān)谶@里發(fā)表您的看法、交流您的觀點(diǎn)。

訂閱博客                   QQ交流群(312716741)

  • 通過Google訂閱本站 通過鮮果訂閱本站 通過抓蝦訂閱本站
  • 通過QQ郵箱訂閱本站 通過Yahoo訂閱本站 通過有道訂閱本站

Search

最新評(píng)論及回復(fù)

最近留言

網(wǎng)站分類

Powered By Z-Blog 1.8 Walle Build 91204 Designed by Han'space

Copyright @2009-2024 All Rights Reserved. 粵ICP備14028160號(hào)-1

Powered By Z-Blog 1.8 Walle Build 91204
Copyright @2009-2024 All Rights Reserved.