YUI Compressor Java代码方式实现

Here’s how you can use the YUI Compressor  in your Java code. This is useful if you need to integrate JavaScript compression into your application instead of simply adding it as a step in the build process. Parts of the code below have been taken from the actual YUI Compressor source code.

1. Download the YUI Compressor from the YUI Library Downloads  page. Extract the YUI Compressor JAR file and add it to your classpath. The JAR filename follows the pattern yuicompressor-*.jar. The JAR file we used for this blog post is yuicompressor-2.4.2.jar.

2. You can use the command line program called com.yahoo.platform.yui.compressor.YUICompressor, passing parameters to main(), or you can use the class com.yahoo.platform.yui.compressor.JavaScriptCompressor. For this blog post, we will use the JavaScriptCompressor class directly.

3. Before we look into the compressor, let’s setup our sample program. I’ve included the import statements of the libraries we will be using as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.ws.common.util;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.apache.commons.io.IOUtils;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
 
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
 
public class YuiCompressor {
     public static void main(String args[]) {
         try {
 
         } catch (Exception e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
         }
     }
 
     private static Logger logger = Logger.getLogger(YuiCompressor. class .getName());
}

4. Next let’s add an inner private static class that implements org.mozilla.javascript.ErrorReporter. This will contain callback functions that will be called when the compressor finds warnings or errors in the JavaScript code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private static class YuiCompressorErrorReporter implements ErrorReporter {
     public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
         if (line < 0 ) {
             logger.log(Level.WARNING, message);
         } else {
             logger.log(Level.WARNING, line + ':' + lineOffset + ':' + message);
         }
     }
 
     public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
         if (line < 0 ) {
             logger.log(Level.SEVERE, message);
         } else {
             logger.log(Level.SEVERE, 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);
     }
}

5. Next, we will setup the different options we can pass to the JavaScript compressor. To make things simple, I have created an inner static public struct with all the default settings.

1
2
3
4
5
6
7
8
public static class Options {
     public String charset = "UTF-8" ;
     public int lineBreakPos = - 1 ;
     public boolean munge = true ;
     public boolean verbose = false ;
     public boolean preserveAllSemiColons = false ;
     public boolean disableOptimizations = false ;
}

6. Now let’s go the compressor. We create a method called compressJavaScript that calls JavaScriptCompressor. The JavaScriptCompressor constructor takes in a java.io.Reader and its compress method takes in a java.io.Writer. We close the Reader before calling the compress method so we can have the output filename to be the same as the input filename if we wanted to. We also use Commons IO to close the character streams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void compressJavaScript(String inputFilename, String outputFilename, Options o) throws IOException {
     Reader in = null ;
     Writer out = null ;
     try {
         in = new InputStreamReader( new FileInputStream(inputFilename), o.charset);
 
         JavaScriptCompressor compressor = new JavaScriptCompressor(in, new YuiCompressorErrorReporter());
         in.close(); in = null ;
 
         out = new OutputStreamWriter( new FileOutputStream(outputFilename), o.charset);
         compressor.compress(out, o.lineBreakPos, o.munge, o.verbose, o.preserveAllSemiColons, o.disableOptimizations);
     } finally {
         IOUtils.closeQuietly(in);
         IOUtils.closeQuietly(out);
     }
}

7. Finally, specify the input and output JavaScript files and call our compressJavaScript() method.

1
2
3
4
String inputFilename = "/original.js" ;
String outputFilename = "/compressed.js" ;
Options o = new Options(); // use defaults
YuiCompressor.compressJavaScript(inputFilename, outputFilename, o);

That’s it. You can now compress JavaScript in your Java code. You can also download the complete YuiCompressor.java example source code. If you would like to use the YUI CSS compressor, look at the class com.yahoo.platform.yui.compressor.CssCompressor. The code you will have to write is very similar to the code above.

转至 : http://blog.teamextension.com/yui-compressor-in-java-246
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值