YUI Compressor Java代码方式实现
来源:互联网 发布:什么软件下载快 编辑:程序博客网 时间:2024/05/04 02:26
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.
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.
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.
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.
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.
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代码方式实现
- Yui Compressor Java压缩实践
- YUI Compressor
- YUI Compressor
- YUI Compressor完成代码混淆示例
- phpstorm 结合 YUI Compressor 实现自动压缩文件
- 使用Yahoo.Yui.Compressor实现自己的Css&JS压缩工具
- YUI compressor使用说明
- YUI Compressor源代码分析
- javascript 压缩工具-YUI-compressor
- YUI compressor的使用
- javascript压缩工具-YUI-compressor
- js压缩 --- YUI Compressor
- YUI Compressor 使用说明
- 牛刀小试YUI compressor
- 牛刀小试YUI compressor
- YUI Compressor快速使用指南
- 源码安装yui compressor
- c++的对象计数问题的多线程实现
- 游戏主循环
- webservice理解
- 又见一帘幽梦
- Spring配置Bug,浪费了一天,谨记
- YUI Compressor Java代码方式实现
- OpenCV 基础- Mat IplImage 最近用到的一些转换 以及初始化
- JSP 上传文档有特殊字符
- 8种常见的Java不规范代码
- Visuals studio 2005项目属性配置要点
- 回调函数透彻理解Java
- find到带空格文件名用xargs处理的技巧
- Lucene入门教程03
- 风柔月清,吾爱在浅秋