国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩中文字幕在线观看视频 | 91精品国产福利在线观看 | 亚洲精品乱码久久久久久蜜糖图片 | 精品亚洲一区二区三区 | 亚洲综合大片69999 | 中文字幕在线一区 | 一区二区三区精品 | 国产精品久久一区二区三区 | 欧美日本精品 | 一级做a爰片久久毛片免费陪 | 狠狠躁夜夜躁人人爽天天高潮 | 亚洲欧美激情精品一区二区 | 亚洲网站在线 | 亚洲国产视频一区 | www.xxx在线观看| 亚洲性网| 欧美日韩在线综合 | 久久综合一 | 亚洲成人一区二区三区在线观看 | 久久久久久久久综合 | 性做久久久久久 | 欧美久久久久 | 亚洲欧美日韩精品久久奇米色影视 | 色婷婷一区二区三区 | 欧美日韩中文 | 日韩在线中文 | 久久精彩 | 欧美午夜在线 | 国内偷拍av | 综合伊人久久 | 一级做a爰片性色毛片2021 | 亚洲乱码一区二区三区在线观看 | 欧美日一区 | 欧美一级在线 | 黄网站在线播放 | av亚洲在线 | 国产91短视频 | 国产精品一区视频 | 日本在线观看一区 | 国产精品自产拍在线观看 | 国产综合网站 |