一、前言
关于springframework框架spring-web包(4.1.4)的org.springframework.web.util.HtmlUtils页面工具类,进行对html字符串进行加密htmlEscape伪码、对加密html字符串进行解密htmlUnescape等处理。
二、源码说明
1. HtmlUtils类package org.springframework.web.util;@b@@b@import org.springframework.util.Assert;@b@@b@public abstract class HtmlUtils@b@{@b@ private static final HtmlCharacterEntityReferences characterEntityReferences = new HtmlCharacterEntityReferences();@b@@b@ public static String htmlEscape(String input)@b@ {@b@ return htmlEscape(input, "ISO-8859-1");@b@ }@b@@b@ public static String htmlEscape(String input, String encoding)@b@ {@b@ Assert.notNull(encoding, "Encoding is required");@b@ if (input == null)@b@ return null;@b@@b@ StringBuilder escaped = new StringBuilder(input.length() * 2);@b@ for (int i = 0; i
2.HtmlCharacterEntityReferences、HtmlCharacterEntityDecoder类package org.springframework.web.util;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.util.Enumeration;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Properties;@b@import org.springframework.util.Assert;@b@@b@class HtmlCharacterEntityReferences@b@{@b@ private static final String PROPERTIES_FILE = "HtmlCharacterEntityReferences.properties";@b@ static final char REFERENCE_START = 38;@b@ static final String DECIMAL_REFERENCE_START = "";@b@ static final String HEX_REFERENCE_START = "";@b@ static final char REFERENCE_END = 59;@b@ static final char CHAR_NULL = 65535;@b@ private final String[] characterToEntityReferenceMap = new String[3000];@b@ private final Map entityReferenceToCharacterMap = new HashMap(252);@b@@b@ public HtmlCharacterEntityReferences()@b@ {@b@ Properties entityReferences = new Properties();@b@@b@ InputStream is = HtmlCharacterEntityReferences.class.getResourceAsStream("HtmlCharacterEntityReferences.properties");@b@ if (is == null)@b@ throw new IllegalStateException("Cannot find reference definition file [HtmlCharacterEntityReferences.properties] as class path resource");@b@ try@b@ {@b@ try@b@ {@b@ entityReferences.load(is);@b@@b@ is.close(); } finally { is.close();@b@ }@b@ }@b@ catch (IOException ex)@b@ {@b@ throw new IllegalStateException("Failed to parse reference definition file [HtmlCharacterEntityReferences.properties]: " + ex@b@ .getMessage());@b@ }@b@@b@ Enumeration keys = entityReferences.propertyNames();@b@ while (keys.hasMoreElements()) {@b@ String key = (String)keys.nextElement();@b@ int referredChar = Integer.parseInt(key);@b@ Assert.isTrue(((referredChar = 8000) && (referredChar ':@b@ return ">";@b@ case '"':@b@ return """;@b@ case '&':@b@ return "&";@b@ case '\'':@b@ return "'";@b@ }@b@ }@b@ else if ((character = 8000) && (character 3) {@b@ this.decodedMessage.append(this.originalMessage.substring(this.currentPosition, skipUntilIndex));@b@ this.currentPosition = skipUntilIndex;@b@ }@b@ else {@b@ while (this.currentPosition