• R/O
  • SSH
  • HTTPS

amateras: Commit


Commit MetaInfo

Revisión14 (tree)
Tiempo2010-01-29 00:41:51
Autortakezoe

Log Message

HTMLのエスケープ処理をS2Functionsからコピー。

Cambiar Resumen

Diferencia incremental

--- functions/trunk/functions/src/main/java/jp/sf/amateras/functions/utils/StringUtils.java (revision 13)
+++ functions/trunk/functions/src/main/java/jp/sf/amateras/functions/utils/StringUtils.java (revision 14)
@@ -10,6 +10,22 @@
1010 */
1111 public class StringUtils {
1212
13+ private static final int HIGHEST_SPECIAL = '>';
14+
15+ private static String BR = "<br />";
16+
17+ private static String NBSP = "&nbsp;";
18+
19+ private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
20+
21+ static {
22+ specialCharactersRepresentation['&'] = "&amp;".toCharArray();
23+ specialCharactersRepresentation['<'] = "&lt;".toCharArray();
24+ specialCharactersRepresentation['>'] = "&gt;".toCharArray();
25+ specialCharactersRepresentation['"'] = "&#034;".toCharArray();
26+ specialCharactersRepresentation['\''] = "&#039;".toCharArray();
27+ }
28+
1329 private static String defaultEncode = "UTF-8";
1430
1531 public static void setDefaultEncode(String defaultEncode){
@@ -22,18 +38,40 @@
2238 * @param value 変換対象の文字列
2339 * @return 変換後の文字列。引数<code>value</code>が<code>null</code>の場合は空文字列
2440 */
25- public static String escapeHtml(String value){
26- if(value == null){
41+ public static String escapeHtml(String buffer){
42+ if(buffer == null){
2743 return "";
2844 }
2945
30- value = value.replace("&", "&amp;");
31- value = value.replace("<", "&lt;");
32- value = value.replace(">", "&gt;");
33- value = value.replace("\"", "&quot;");
34-
35- return value;
36- }
46+ int start = 0;
47+ int length = buffer.length();
48+ char[] arrayBuffer = buffer.toCharArray();
49+ StringBuilder escapedBuffer = null;
50+
51+ for (int i = 0; i < length; i++) {
52+ char c = arrayBuffer[i];
53+ if (c <= HIGHEST_SPECIAL) {
54+ char[] escaped = specialCharactersRepresentation[c];
55+ if (escaped != null) {
56+ if (start == 0) {
57+ escapedBuffer = new StringBuilder(length + 5);
58+ }
59+ if (start < i) {
60+ escapedBuffer.append(arrayBuffer, start, i - start);
61+ }
62+ start = i + 1;
63+ escapedBuffer.append(escaped);
64+ }
65+ }
66+ }
67+ if (start == 0) {
68+ return buffer;
69+ }
70+ if (start < length) {
71+ escapedBuffer.append(arrayBuffer, start, length - start);
72+ }
73+ return escapedBuffer.toString();
74+ }
3775
3876 /**
3977 * デフォルトのエンコーディングでURLエンコードを行います。
@@ -77,9 +115,9 @@
77115 return "";
78116 }
79117
80- value = value.replace("\r\n", "\n");
81- value = value.replace("\r", "\n");
82- value = value.replace("\n", "<br>");
118+ value = value.replace("\r\n", BR);
119+ value = value.replace("\r", BR);
120+ value = value.replace("\n", BR);
83121
84122 return value;
85123 }
@@ -112,7 +150,7 @@
112150 sb.append(c);
113151 flag = false;
114152 } else {
115- sb.append("&nbsp;");
153+ sb.append(NBSP);
116154 }
117155 } else {
118156 sb.append(c);
Show on old repository browser