一、前言
关于htmlcompressor源码包中com.googlecode.htmlcompressor.compressor.YuiJavaScriptCompressor、com.googlecode.htmlcompressor.compressor.Compressor定义javascript脚本压缩接口及实现类,可以对空格、回车、换行及注释等进行过滤压缩处理。
二、源码说明
1.Compressor接口
package com.googlecode.htmlcompressor.compressor;@b@@b@public abstract interface Compressor@b@{@b@ public abstract String compress(String paramString);@b@}
2.YuiJavaScriptCompressor实现类
package com.googlecode.htmlcompressor.compressor;@b@@b@import com.yahoo.platform.yui.compressor.JavaScriptCompressor;@b@import java.io.IOException;@b@import java.io.PrintStream;@b@import java.io.StringReader;@b@import java.io.StringWriter;@b@import org.mozilla.javascript.ErrorReporter;@b@import org.mozilla.javascript.EvaluatorException;@b@@b@public class YuiJavaScriptCompressor@b@ implements Compressor@b@{@b@ private boolean noMunge = false;@b@ private boolean preserveAllSemiColons = false;@b@ private boolean disableOptimizations = false;@b@ private int lineBreak = -1;@b@ private ErrorReporter errorReporter = new DefaultErrorReporter();@b@@b@ public String compress(String source)@b@ {@b@ StringWriter result = new StringWriter();@b@ try {@b@ JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(source), this.errorReporter);@b@ compressor.compress(result, this.lineBreak, (!(this.noMunge)) ? 1 : false, false, this.preserveAllSemiColons, this.disableOptimizations);@b@ } catch (IOException e) {@b@ result.write(source);@b@ e.printStackTrace();@b@ }@b@ return result.toString();@b@ }@b@@b@ public boolean isNoMunge()@b@ {@b@ return this.noMunge;@b@ }@b@@b@ public void setNoMunge(boolean noMunge)@b@ {@b@ this.noMunge = noMunge;@b@ }@b@@b@ public boolean isPreserveAllSemiColons()@b@ {@b@ return this.preserveAllSemiColons;@b@ }@b@@b@ public void setPreserveAllSemiColons(boolean preserveAllSemiColons)@b@ {@b@ this.preserveAllSemiColons = preserveAllSemiColons;@b@ }@b@@b@ public boolean isDisableOptimizations()@b@ {@b@ return this.disableOptimizations;@b@ }@b@@b@ public void setDisableOptimizations(boolean disableOptimizations)@b@ {@b@ this.disableOptimizations = disableOptimizations;@b@ }@b@@b@ public int getLineBreak()@b@ {@b@ return this.lineBreak;@b@ }@b@@b@ public void setLineBreak(int lineBreak)@b@ {@b@ this.lineBreak = lineBreak;@b@ }@b@@b@ public ErrorReporter getErrorReporter()@b@ {@b@ return this.errorReporter;@b@ }@b@@b@ public void setErrorReporter(ErrorReporter errorReporter)@b@ {@b@ this.errorReporter = errorReporter;@b@ }@b@@b@ public static class DefaultErrorReporter@b@ implements ErrorReporter@b@ {@b@ public void warning(String message, String sourceName, int line, String lineSource, int lineOffset)@b@ {@b@ if (line < 0)@b@ System.err.println("[WARNING] HtmlCompressor: \"" + message + "\" during JavaScript compression");@b@ else@b@ System.err.println("[WARNING] HtmlCompressor: \"" + message + "\" at line [" + line + ":" + lineOffset + "] during JavaScript compression" + ((lineSource != null) ? ": " + lineSource : ""));@b@ }@b@@b@ public void error(String message, String sourceName, int line, String lineSource, int lineOffset)@b@ {@b@ if (line < 0)@b@ System.err.println("[ERROR] HtmlCompressor: \"" + message + "\" during JavaScript compression");@b@ else@b@ System.err.println("[ERROR] HtmlCompressor: \"" + message + "\" at line [" + line + ":" + lineOffset + "] during JavaScript compression" + ((lineSource != null) ? ": " + lineSource : ""));@b@ }@b@@b@ public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset)@b@ {@b@ error(message, sourceName, line, lineSource, lineOffset);@b@ return new EvaluatorException(message);@b@ }@b@ }@b@}