KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chipchat > StringUtil


1 /*
2  * Created on 2004. 2. 20
3  */

4 package chipchat;
5
6 /**
7  * String utility.
8  * @author Mr.Lee
9  */

10 public final class StringUtil {
11    /**
12     * Convert html special charectors.
13     * @param src Source.
14     * @return Changed String.
15     */

16    public static String JavaDoc htmlSpecialChars(final String JavaDoc src) {
17       return htmlSpecialChars(new StringBuffer JavaDoc(src)).toString();
18    }
19    /**
20     * Convert html special charectors.
21     * @param src Source.
22     * @return Changed String.
23     */

24    public static StringBuffer JavaDoc htmlSpecialChars(final StringBuffer JavaDoc src) {
25       if (src == null) {
26          return null;
27       }
28       int srcLength = src.length();
29
30       for (int i = 0; i < srcLength; i++) {
31          switch (src.charAt(i)) {
32             case '<' :
33                src.replace(i, i + 1, "&lt;");
34                srcLength += 3;
35                i += 3;
36                break;
37             case '>' :
38                src.replace(i, i + 1, "&gt;");
39                srcLength += 3;
40                i += 3;
41                break;
42             case '&' :
43                src.replace(i, i + 1, "&amp;");
44                srcLength += 4;
45                i += 4;
46                break;
47             case '"' :
48                src.replace(i, i + 1, "&quot;");
49                srcLength += 5;
50                i += 5;
51                break;
52             case '\'' :
53                src.replace(i, i + 1, "&#039;");
54                srcLength += 5;
55                i += 5;
56                break;
57             default :
58                break;
59          }
60          if (i > 0 && src.charAt(i - 1) == ' ' && src.charAt(i) == ' ') {
61             src.replace(i - 1, i, "&nbsp;");
62             src.replace(i + 5, i + 6, "&nbsp;");
63             srcLength += 10;
64             i = i + 10;
65          }
66       }
67       return src;
68    }
69 }
70
Popular Tags