通过移除空行和注释来压缩javascript代码
/***このファイルは、Echo Webアプリケーションフレームワークの一部です(以下 /"Echo /")。 * Copyright(c)2002-2009 NextApp、Inc。 * *コメントと空白を削除してJavaScriptを含む文字列を圧縮します。 */public class javascriptcompressor {private static final char line_feed =/'// n/'; private static final char caririage_return =/'// r/'; private static final char space =/'/'; private static final char tab =/'// t/';/** *は、コメントを削除したjavascriptを含む文字列を圧縮します。 * * @paramスクリプト圧縮する文字列 * @returnの圧縮バージョン */public static string compress(string script){javascriptcompressor jsc = new javascriptcompressor(script); return jsc.outputbuffer.tostring();}/**元のjavascriptテキスト。 */プライベート文字列スクリプト;/***圧縮出力バッファー。 *このバッファーは、<code> append()</code> *メソッドを呼び出すことによってのみ変更できます。 */private stringbuffer outputbuffer;/**元のテキストの現在のパーサーカーソル位置。 */private int pos;/**パーサーカーソル位置での文字。 */private char ch;/**バッファに追加された最後のキャラクター。 */private char lastAppend;/**バッファの終了に到達したかどうかを示すフラグ。 */private boolean endreached;/**フラグは、最後の識別子の後にコンテンツが追加されたかどうかを示すフラグ。 */private boolean contentappendedafterlastidentifier = true;/***新しい<code> javascriptcompressor </code>インスタンスを作成します。 * * @param Script * /private javascriptcompressor(string script){this.script = script; outputbuffer = new Stringbuffer(script.length()); nextchar(); while(!endReached){if(character.isjavaidentifierstart(ch)){renderidentier(); else if(iswhitespace()){// compress whiteSpaceskipwhitespace();} else if((ch ==/'/"/')||(ch ==/'///'/')){//ハンドルストリングスストリング();} else(ch ==/'){// NextChar() ch; outputBuffer.append(ch); contentAppendedAfterLastIdentifier = true; } /** * Determines if current character is whitespace. * * @return true if the character is whitespace */ private boolean isWhitespace() { return ch == CARRIAGE_RETURN || ch == SPACE || ch == TAB || ch == LINE_FEED; } /** * Load next character. */ private void nextchar(!endReach){pos <script.lengty.charat(pos ++); whiled.isjavaidentiferpart(ch); line_feed ||(carirage_return){// javascriptエラー:if(ch ==/'/////')終了したreturn(ch); (reture){) }/** *以前にレンダリングされた文字をレンダリングします */private void rendernewline skipwhiteSpace(){ch == line_feed || carirage_return){rendernewline(); rendernewline();总结
以上就是本文关于通过java压缩javascript