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

你可能感兴趣的:(YUI Compressor Java代码方式实现)