一、前言
关于if-util-3.2.8.jar开源包中的com.bill99.seashell.common.util.HtmlUtil工具类实现了常用html标签特殊字符关键词字符过滤escapeHTMLChars、根据Map映射生成表单提交INPUT项html页generateForm及根据Map映射生成URL的入参串generateFullUrl等常用操作。
二、源码说明
package com.bill99.seashell.common.util;@b@@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Set;@b@@b@public final class HtmlUtil@b@{@b@ private static final String[][] ENTITIES = { { "&", "amp" }, { "<", "lt" }, { ">", "gt" }, { "\"", "quot" }, { "", "#131" }, { "", "#132" }, { " ", "#133" }, { "", "#134" }, { "", "#135" }, { "", "#137" }, { "", "#138" }, { "", "#139" }, { "", "#140" }, { "", "#145" }, { "", "#146" }, { "", "#147" }, { "", "#148" }, { "", "#149" }, { "", "#150" }, { "", "#151" }, { "", "#153" }, { "", "#154" }, { "", "#155" }, { "", "#156" }, { "", "#159" }, { " ", "nbsp" }, { "¡", "iexcl" }, { "¢", "cent" }, { "£", "pound" }, { "¤", "curren" }, { "¥", "yen" }, { "¦", "brvbar" }, { "§", "sect" }, { "¨", "uml" }, { "©", "copy" }, { "ª", "ordf" }, { "«", "laquo" }, { "¬", "not" }, { "", "shy" }, { "®", "reg" }, { "¯", "macr" }, { "°", "deg" }, { "±", "plusmn" }, { "²", "sup2" }, { "³", "sup3" }, { "´", "acute" }, { "µ", "micro" }, { "¶", "para" }, { "·", "middot" }, { "¸", "cedil" }, { "¹", "sup1" }, { "º", "ordm" }, { "»", "raquo" }, { "¼", "frac14" }, { "½", "frac12" }, { "¾", "frac34" }, { "¿", "iquest" }, { "À", "Agrave" }, { "Á", "Aacute" }, { "Â", "Acirc" }, { "Ã", "Atilde" }, { "Ä", "Auml" }, { "Å", "Aring" }, { "Æ", "AElig" }, { "Ç", "CCedil" }, { "È", "Egrave" }, { "É", "Eacute" }, { "Ê", "Ecirc" }, { "Ë", "Euml" }, { "Ì", "Igrave" }, { "Í", "Iacute" }, { "Î", "Icirc" }, { "Ï", "Iuml" }, { "Ð", "ETH" }, { "Ñ", "Ntilde" }, { "Ò", "Ograve" }, { "Ó", "Oacute" }, { "Ô", "Ocirc" }, { "Õ", "Otilde" }, { "Ö", "Ouml" }, { "×", "times" }, { "Ø", "Oslash" }, { "Ù", "Ugrave" }, { "Ú", "Uacute" }, { "Û", "Ucirc" }, { "Ü", "Uuml" }, { "Ý", "Yacute" }, { "Þ", "THORN" }, { "ß", "szlig" }, { "à", "agrave" }, { "á", "aacute" }, { "â", "acirc" }, { "ã", "atilde" }, { "ä", "auml" }, { "å", "aring" }, { "æ", "aelig" }, { "ç", "ccedil" }, { "è", "egrave" }, { "é", "eacute" }, { "ê", "ecirc" }, { "ë", "euml" }, { "ì", "igrave" }, { "í", "iacute" }, { "î", "icirc" }, { "ï", "iuml" }, { "ð", "eth" }, { "ñ", "ntilde" }, { "ò", "ograve" }, { "ó", "oacute" }, { "ô", "ocirc" }, { "õ", "otilde" }, { "ö", "ouml" }, { "÷", "divid" }, { "ø", "oslash" }, { "ù", "ugrave" }, { "ú", "uacute" }, { "û", "ucirc" }, { "ü", "uuml" }, { "ý", "yacute" }, { "þ", "thorn" }, { "ÿ", "yuml" } };@b@ @b@ @b@ private static String entityMap;@b@ private static String[] quickEntities;@b@@b@ public static final String escapeHTMLChars(String nonHTML)@b@ {@b@ String nonHTMLsrc = nonHTML;@b@ StringBuffer res = new StringBuffer();@b@ if (nonHTMLsrc == null)@b@ nonHTMLsrc = "";@b@@b@ int l = nonHTMLsrc.length();@b@@b@ for (int i = 0; i < l; ++i) {@b@ char c = nonHTMLsrc.charAt(i);@b@ int idx = entityMap.indexOf(c);@b@ if (idx == -1)@b@ res.append(c);@b@ else@b@ res.append(quickEntities[idx]);@b@ }@b@@b@ return res.toString();@b@ }@b@@b@ public static String generateForm(Map properties, String url, boolean autoSubmit)@b@ {@b@ StringBuffer buf = new StringBuffer();@b@ buf.append("<HTML><HEAD><TITLE></TITLE></HEAD><BODY><FORM name='form1' method='post' action='");@b@@b@ buf.append(url);@b@ buf.append("'>");@b@ Iterator it = properties.keySet().iterator();@b@ while (it.hasNext()) {@b@ String key = (String)it.next();@b@ String value = (String)properties.get(key);@b@ buf.append("<INPUT TYPE='HIDDEN' NAME='").append(key).append("' VALUE='").append(value).append("'/>");@b@ }@b@@b@ buf.append("</FORM></BODY></HTML>");@b@ if (autoSubmit)@b@ buf.append("<SCRIPT LANGUAGE='JAVASCRIPT'>document.form1.submit();</SCRIPT>");@b@@b@ return buf.toString();@b@ }@b@@b@ public static String generateFullUrl(Map properties, String url)@b@ {@b@ StringBuffer buf = new StringBuffer();@b@ buf.append(url);@b@@b@ int index = url.indexOf("?");@b@ if (index <= 0)@b@ buf.append("?");@b@ else@b@ buf.append("&");@b@@b@ Iterator it = properties.keySet().iterator();@b@ while (it.hasNext()) {@b@ String key = (String)it.next();@b@ String value = (String)properties.get(key);@b@ buf.append(key);@b@ buf.append("=");@b@ buf.append(value);@b@ if (it.hasNext())@b@ buf.append("&");@b@@b@ }@b@@b@ return buf.toString();@b@ }@b@@b@ static@b@ {@b@ int l = ENTITIES.length;@b@ StringBuffer temp = new StringBuffer();@b@@b@ quickEntities = new String[l];@b@ for (int i = 0; i < l; ++i) {@b@ temp.append(ENTITIES[i][0]);@b@ quickEntities[i] = "&" + ENTITIES[i][1] + ";";@b@ }@b@ entityMap = temp.toString();@b@ }@b@}