一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 通過Java壓縮JavaScript代碼實例分享

通過Java壓縮JavaScript代碼實例分享

2021-02-28 11:11wgd7 Java教程

這篇文章主要介紹了通過Java壓縮JavaScript代碼實例分享,具有一定參考價值,需要的朋友可以了解下。

通過移除空行和注釋來壓縮 JavaScript 代碼

?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
 * This file is part of the Echo Web Application Framework (hereinafter \"Echo\").
 * Copyright (C) 2002-2009 NextApp, Inc.
 *
 * Compresses a String containing JavaScript by removing comments and whitespace.
 */
public class JavaScriptCompressor {
    private static final char LINE_FEED = \'\\n\';
    private static final char CARRIAGE_RETURN = \'\\r\';
    private static final char SPACE = \' \';
    private static final char TAB = \'\\t\';
    /**
   * Compresses a String containing JavaScript by removing comments and
   * whitespace.
   *
   * @param script the String to compress
   * @return a compressed version
   */
    public static String compress(String script) {
        JavaScriptCompressor jsc = new JavaScriptCompressor(script);
        return jsc.outputBuffer.toString();
    }
    /** Original JavaScript text. */
    private String script;
    /**
   * Compressed output buffer.
   * This buffer may only be modified by invoking the <code>append()</code>
   * method.
   */
    private StringBuffer outputBuffer;
    /** Current parser cursor position in original text. */
    private int pos;
    /** Character at parser cursor position. */
    private char ch;
    /** Last character appended to buffer. */
    private char lastAppend;
    /** Flag indicating if end-of-buffer has been reached. */
    private Boolean endReached;
    /** Flag indicating whether content has been appended after last identifier. */
    private Boolean contentAppendedAfterLastIdentifier = true;
    /**
   * Creates a new <code>JavaScriptCompressor</code> instance.
   *
   * @param script
   */
    private JavaScriptCompressor(String script) {
        this.script = script;
        outputBuffer = new StringBuffer(script.length());
        nextchar();
        while (!endReached) {
            if (Character.isJavaIdentifierStart(ch)) {
                renderIdentifier();
            } else if (ch == \' \') {
                skipWhiteSpace();
            } else if (isWhitespace()) {
                // Compress whitespace
                skipWhiteSpace();
            } else if ((ch == \'\"\') || (ch == \'\\\'\')) {
        // Handle strings
        renderString();
      } else if (ch == \'/\') {
        // Handle comments
        nextChar();
        if (ch == \'/\') {
          nextChar();
          skipLineComment();
        } else if (ch == \'*\') {
          nextChar();
          skipBlockComment();
        } else {
          append(\'/\');
        }
      } else {
        append(ch);
        nextChar();
      }
    }
  }
  /**
   * Append character to output.
   *
   * @param ch the character to append
   */
  private void append(char ch) {
    lastAppend = 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() {
    if (!endReached) {
      if (pos < script.length()) {
        ch = script.charAt(pos++);
      } else {
        endReached = true;
        ch = 0;
      }
    }
  }
  /**
   * Adds an identifier to output.
   */
  private void renderIdentifier() {
    if (!contentAppendedAfterLastIdentifier)
      append(SPACE);
    append(ch);
    nextChar();
    while (Character.isJavaIdentifierPart(ch)) {
      append(ch);
      nextChar();
    }
    contentAppendedAfterLastIdentifier = false;
  }
  /**
   * Adds quoted String starting at current character to output.
   */
  private void renderString() {
    char startCh = ch; // Save quote char
    append(ch);
    nextChar();
    while (true) {
      if ((ch == LINE_FEED) || (ch == CARRIAGE_RETURN) || (endReached)) {
        // JavaScript error: string not terminated
        return;
      } else {
        if (ch == \'\\\\\') {
          append(ch);
          nextChar();
          if ((ch == LINE_FEED) || (ch == CARRIAGE_RETURN) || (endReached)) {
            // JavaScript error: string not terminated
            return;
          }
          append(ch);
          nextChar();
        } else {
          append(ch);
          if (ch == startCh) {
            nextChar();
            return;
          }
          nextChar();
        }
      }
    }
  }
  /**
   * Moves cursor past a line comment.
   */
  private void skipLineComment() {
    while ((ch != CARRIAGE_RETURN) && (ch != LINE_FEED)) {
      if (endReached) {
        return;
      }
      nextChar();
    }
  }
  /**
   * Moves cursor past a block comment.
   */
  private void skipBlockComment() {
    while (true) {
      if (endReached) {
        return;
      }
      if (ch == \'*\') {
        nextChar();
        if (ch == \'/\') {
          nextChar();
          return;
        }
      } else
        nextChar();
    }
  }
  /**
   * Renders a new line character, provided previously rendered character
   * is not a newline.
   */
  private void renderNewLine() {
    if (lastAppend != \'\\n\' && lastAppend != \'\\r\') {
      append(\'\\n\');
    }
  }
  /**
   * Moves cursor past white space (including newlines).
   */
  private void skipWhiteSpace() {
    if (ch == LINE_FEED || ch == CARRIAGE_RETURN) {
      renderNewLine();
    } else {
      append(ch);
    }
    nextChar();
    while (ch == LINE_FEED || ch == CARRIAGE_RETURN || ch == SPACE || ch == TAB) {
      if (ch == LINE_FEED || ch == CARRIAGE_RETURN) {
        renderNewLine();
      }
      nextChar();
    }
  }
}

總結

以上就是本文關于通過Java壓縮JavaScript代碼實例分享的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://www.open-open.com/code/view/1426343830373

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 韩国三级日本三级香港三级黄 | 日韩经典在线 | 欧美精品久久一区二区三区 | 亚洲午夜视频 | 精品推荐国产麻豆剧传媒 | 毛片在线观看网站 | 日本一区二区免费在线观看 | 亚洲精品国产A久久久久久 亚洲精品福利一区二区在线观看 | www.大逼色 | 天堂资源8中文最新版 | 四虎国产精品免费入口 | 欧美xbxbxbbxxbb精品 | 亚洲日日操 | 天天综合色天天综合网 | 日本护士xxxx爽爽爽 | 姐姐不~不可以动漫在线观看 | 国产精品青青在线观看香蕉 | 国产成人福利美女观看视频 | 青青草综合网 | 91精品手机国产在线观 | 美国女艳星brandilove | 精品视频在线观看免费 | 91麻豆精品国产片在线观看 | 国产中文字幕 | 扒开腿开嫩苞 | 亚洲国产欧美在线人成aaa | 午夜欧美精品久久久久久久久 | 99视频久久精品久久 | ccc在线在线36| 国内精品久久久久久久久久久久 | 日本嫩模 | 日韩欧美一区黑人vs日本人 | 亚洲国产精品久久久久 | 12-14娇小videos | 日韩欧美国产成人 | 国产成年人在线观看 | 2020国产精品永久在线观看 | 国产成人h综合亚洲欧美在线 | 香蕉精品国产高清自在自线 | 婚前试爱全集免费观看 | 激情视频图片小说qvdo |