日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

壓縮jsp,html中的代碼,去掉所有空白符、換行符

張軍 7631 0

壓縮jsp,html中的代碼,去掉所有空白符、換行符

package zj.compress.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*******************************************
 * 壓縮jsp,html中的代碼,去掉所有空白符、換行符
 * 
 * @author 張軍
 * @version 0.1
 * @date 2010-5-13
 *******************************************/
public class HtmlCompressor {
	private static String tempPreBlock = "%%%HTMLCOMPRESS~PRE&&&";
	private static String tempTextAreaBlock = "%%%HTMLCOMPRESS~TEXTAREA&&&";
	private static String tempScriptBlock = "%%%HTMLCOMPRESS~SCRIPT&&&";
	private static String tempStyleBlock = "%%%HTMLCOMPRESS~STYLE&&&";
	private static String tempJspBlock = "%%%HTMLCOMPRESS~JSP&&&";
	/**
	 * 
	 * 
	 * @see flag的取值范圍如下:
	 * @see Pattern.CANON_EQ 當(dāng)且僅當(dāng)兩個(gè)字符的"正規(guī)分解(canonical decomposition)"都完全相同的情況下,才認(rèn)定匹配。比如用了這個(gè)標(biāo)志之后,表達(dá)式"a\u030A"會(huì)匹配"?"。默認(rèn)情況下,不考慮"規(guī) 范相等性(canonical equivalence)"。
	 * @see Pattern.CASE_INSENSITIVE(?i) 默認(rèn)情況下,大小寫(xiě)不明感的匹配只適用于US-ASCII字符集。這個(gè)標(biāo)志能讓表達(dá)式忽略大小寫(xiě)進(jìn)行匹配。要想對(duì)Unicode字符進(jìn)行大小不明感的匹 配,只要將UNICODE_CASE與這個(gè)標(biāo)志合起來(lái)就行了。
	 * @see Pattern.COMMENTS(?x) 在這種模式下,匹配時(shí)會(huì)忽略(正則表達(dá)式里的)空格字符(譯者注:不是指表達(dá)式里的"\\s",而是指表達(dá)式里的空格,tab,回車(chē)之類(lèi))。注釋從#開(kāi)始,一直到這行結(jié)束。可以通過(guò)嵌入式的標(biāo)志來(lái)啟用Unix行模式。
	 * @see Pattern.DOTALL(?s) 在這種模式下,表達(dá)式'.'可以匹配任意字符,包括表示一行的結(jié)束符。默認(rèn)情況下,表達(dá)式'.'不匹配行的結(jié)束符。
	 * @see Pattern.MULTILINE (?m) 在這種模式下,'^'和'$'分別匹配一行的開(kāi)始和結(jié)束。此外,'^'仍然匹配字符串的開(kāi)始,'$'也匹配字符串的結(jié)束。默認(rèn)情況下,這兩個(gè)表達(dá)式僅僅匹配字符串的開(kāi)始和結(jié)束。
	 * @see Pattern.UNICODE_CASE (?u) 在這個(gè)模式下,如果你還啟用了CASE_INSENSITIVE標(biāo)志,那么它會(huì)對(duì)Unicode字符進(jìn)行大小寫(xiě)不明感的匹配。默認(rèn)情況下,大小寫(xiě)不敏感的匹配只適用于US-ASCII字符集。
	 * @see Pattern.UNIX_LINES(?d) 在這個(gè)模式下,只有'\n'才被認(rèn)作一行的中止,并且與'.','^',以及'$'進(jìn)行匹配。
	 */
	// 注釋
	private static Pattern commentPattern = Pattern.compile("<!--\\s*[^\\[].*?-->", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// 換行
	private static Pattern itsPattern = Pattern.compile(">\\s+?<", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// pre標(biāo)簽
	private static Pattern prePattern = Pattern.compile("<pre[^>]*?>.*?</pre>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// textarea標(biāo)簽
	private static Pattern taPattern = Pattern.compile("<textarea[^>]*?>.*?</textarea>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// jsp標(biāo)簽
	private static Pattern jspPattern = Pattern.compile("<%([^-@][\\w\\W]*?)%>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// <script></script>
	private static Pattern scriptPattern = Pattern.compile("(?:<script\\s*>|<script type=['\"]text/javascript['\"]\\s*>)(.*?)</script>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// style標(biāo)簽
	private static Pattern stylePattern = Pattern.compile("<style[^>()]*?>(.+)</style>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	// 單行注釋?zhuān)?
	private static Pattern signleCommentPattern = Pattern.compile("//.*");
	// 字符串匹配
	private static Pattern stringPattern = Pattern.compile("(\"[^\"\\n]*?\"|'[^'\\n]*?')");
	// trim去空格和換行符
	private static Pattern trimPattern = Pattern.compile("\\n\\s*", Pattern.MULTILINE);
	private static Pattern trimPattern2 = Pattern.compile("\\s*\\r", Pattern.MULTILINE);
	// 多行注釋
	private static Pattern multiCommentPattern = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	private static String tempSingleCommentBlock = "%%%HTMLCOMPRESS~SINGLECOMMENT&&&"; // //占位符
	private static String tempMulitCommentBlock1 = "%%%HTMLCOMPRESS~MULITCOMMENT1&&&"; // /*占位符
	private static String tempMulitCommentBlock2 = "%%%HTMLCOMPRESS~MULITCOMMENT2&&&"; // */占位符

	public static String compress(String html) throws Exception {
		if (html == null || html.length() == 0) {
			return html;
		}
		List<String> preBlocks = new ArrayList<String>();
		List<String> taBlocks = new ArrayList<String>();
		List<String> scriptBlocks = new ArrayList<String>();
		List<String> styleBlocks = new ArrayList<String>();
		List<String> jspBlocks = new ArrayList<String>();
		String result = html;
		// preserve inline java code
		Matcher jspMatcher = jspPattern.matcher(result);
		while (jspMatcher.find()) {
			jspBlocks.add(jspMatcher.group(0));
		}
		result = jspMatcher.replaceAll(tempJspBlock);
		// preserve PRE tags
		Matcher preMatcher = prePattern.matcher(result);
		while (preMatcher.find()) {
			preBlocks.add(preMatcher.group(0));
		}
		result = preMatcher.replaceAll(tempPreBlock);
		// preserve TEXTAREA tags
		Matcher taMatcher = taPattern.matcher(result);
		while (taMatcher.find()) {
			taBlocks.add(taMatcher.group(0));
		}
		result = taMatcher.replaceAll(tempTextAreaBlock);
		// preserve SCRIPT tags
		Matcher scriptMatcher = scriptPattern.matcher(result);
		while (scriptMatcher.find()) {
			scriptBlocks.add(scriptMatcher.group(0));
		}
		result = scriptMatcher.replaceAll(tempScriptBlock);
		// don't process inline css
		Matcher styleMatcher = stylePattern.matcher(result);
		while (styleMatcher.find()) {
			styleBlocks.add(styleMatcher.group(0));
		}
		result = styleMatcher.replaceAll(tempStyleBlock);
		// process pure html
		result = processHtml(result);
		// process preserved blocks
		result = processPreBlocks(result, preBlocks);
		result = processTextareaBlocks(result, taBlocks);
		result = processScriptBlocks(result, scriptBlocks);
		result = processStyleBlocks(result, styleBlocks);
		result = processJspBlocks(result, jspBlocks);
		preBlocks = taBlocks = scriptBlocks = styleBlocks = jspBlocks = null;
		return result.trim();
	}

	private static String processHtml(String html) {
		String result = html;
		// remove comments
		// if(removeComments) {
		result = commentPattern.matcher(result).replaceAll("");
		// }
		// remove inter-tag spaces
		// if(removeIntertagSpaces) {
		result = itsPattern.matcher(result).replaceAll("><");
		// }
		// remove multi whitespace characters
		// if(removeMultiSpaces) {
		result = result.replaceAll("\\s{2,}", " ");
		// }
		return result;
	}

	private static String processJspBlocks(String html, List<String> blocks) {
		String result = html;
		for (int i = 0; i < blocks.size(); i++) {
			blocks.set(i, compressJsp(blocks.get(i)));
		}
		// put preserved blocks back
		while (result.contains(tempJspBlock)) {
			result = result.replaceFirst(tempJspBlock, Matcher.quoteReplacement(blocks.remove(0)));
		}
		return result;
	}

	private static String processPreBlocks(String html, List<String> blocks) throws Exception {
		String result = html;
		// put preserved blocks back
		while (result.contains(tempPreBlock)) {
			result = result.replaceFirst(tempPreBlock, Matcher.quoteReplacement(blocks.remove(0)));
		}
		return result;
	}

	private static String processTextareaBlocks(String html, List<String> blocks) throws Exception {
		String result = html;
		// put preserved blocks back
		while (result.contains(tempTextAreaBlock)) {
			result = result.replaceFirst(tempTextAreaBlock, Matcher.quoteReplacement(blocks.remove(0)));
		}
		return result;
	}

	private static String processScriptBlocks(String html, List<String> blocks) throws Exception {
		String result = html;
		// if(compressJavaScript) {
		for (int i = 0; i < blocks.size(); i++) {
			blocks.set(i, compressJavaScript(blocks.get(i)));
		}
		// }
		// put preserved blocks back
		while (result.contains(tempScriptBlock)) {
			result = result.replaceFirst(tempScriptBlock, Matcher.quoteReplacement(blocks.remove(0)));
		}
		return result;
	}

	private static String processStyleBlocks(String html, List<String> blocks) throws Exception {
		String result = html;
		// if(compressCss) {
		for (int i = 0; i < blocks.size(); i++) {
			blocks.set(i, compressCssStyles(blocks.get(i)));
		}
		// }
		// put preserved blocks back
		while (result.contains(tempStyleBlock)) {
			result = result.replaceFirst(tempStyleBlock, Matcher.quoteReplacement(blocks.remove(0)));
		}
		return result;
	}

	private static String compressJsp(String source) {
		// check if block is not empty
		Matcher jspMatcher = jspPattern.matcher(source);
		if (jspMatcher.find()) {
			String result = compressJspJs(jspMatcher.group(1));
			return (new StringBuilder(source.substring(0, jspMatcher.start(1))).append(result).append(source.substring(jspMatcher.end(1)))).toString();
		} else {
			return source;
		}
	}

	private static String compressJavaScript(String source) {
		// check if block is not empty
		Matcher scriptMatcher = scriptPattern.matcher(source);
		if (scriptMatcher.find()) {
			String result = compressJspJs(scriptMatcher.group(1));
			return (new StringBuilder(source.substring(0, scriptMatcher.start(1))).append(result).append(source.substring(scriptMatcher.end(1)))).toString();
		} else {
			return source;
		}
	}

	private static String compressCssStyles(String source) {
		// check if block is not empty
		Matcher styleMatcher = stylePattern.matcher(source);
		if (styleMatcher.find()) {
			// 去掉注釋?zhuān)瑩Q行
			String result = multiCommentPattern.matcher(styleMatcher.group(1)).replaceAll("");
			result = trimPattern.matcher(result).replaceAll("");
			result = trimPattern2.matcher(result).replaceAll("");
			return (new StringBuilder(source.substring(0, styleMatcher.start(1))).append(result).append(source.substring(styleMatcher.end(1)))).toString();
		} else {
			return source;
		}
	}

	private static String compressJspJs(String source) {
		String result = source;
		// 因注釋符合有可能出現(xiàn)在字符串中,所以要先把字符串中的特殊符好去掉
		Matcher stringMatcher = stringPattern.matcher(result);
		while (stringMatcher.find()) {
			String tmpStr = stringMatcher.group(0);
			if (tmpStr.indexOf("//") != -1 || tmpStr.indexOf("/*") != -1 || tmpStr.indexOf("*/") != -1) {
				String blockStr = tmpStr.replaceAll("//", tempSingleCommentBlock).replaceAll("/\\*", tempMulitCommentBlock1).replaceAll("\\*/", tempMulitCommentBlock2);
				result = result.replace(tmpStr, blockStr);
			}
		}
		// 去掉注釋
		result = signleCommentPattern.matcher(result).replaceAll("");
		result = multiCommentPattern.matcher(result).replaceAll("");
		result = trimPattern2.matcher(result).replaceAll("");
		result = trimPattern.matcher(result).replaceAll(" ");
		// 恢復(fù)替換掉的字符串
		result = result.replaceAll(tempSingleCommentBlock, "//").replaceAll(tempMulitCommentBlock1, "/*").replaceAll(tempMulitCommentBlock2, "*/");
		return result;
	}
}



更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!??!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 会理县| 丹棱县| 乾安县| 伊金霍洛旗| 稷山县| 南岸区| 乐业县| 莫力| 得荣县| 宁武县| 宝鸡市| 桐乡市| 喀喇沁旗| 白山市| 沂南县| 手游| 射阳县| 开远市| 黄浦区| 西峡县| 湘潭市| 榆树市| 兴业县| 海安县| 二连浩特市| 洛隆县| 兴城市| 崇州市| 宝山区| 顺昌县| 西充县| 金山区| 北碚区| 绵阳市| 乐都县| 卓尼县| 乐东| 茂名市| 临西县| 仁寿县| 右玉县|