KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > filter > DisableHtmlTagFilter


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/DisableHtmlTagFilter.java,v 1.10 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/15 02:59:19 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  * @author: Mai Nguyen
33  */

34 package net.myvietnam.mvncore.filter;
35
36 public final class DisableHtmlTagFilter {
37
38     private DisableHtmlTagFilter() { //prevent instantiation
39
}
40
41     public static String JavaDoc filter(String JavaDoc input) {
42         if (input == null) {
43             return null;
44         }
45
46         char[] s = input.toCharArray();
47         int length = s.length;
48         StringBuffer JavaDoc ret = new StringBuffer JavaDoc(length + 100);// add more room to the result String
49

50         for (int i = 0; i < length; i++) {
51             if (s[i] == '<') {
52                 ret.append("&lt;");
53             } else if (s[i] == '>') {
54                 ret.append("&gt;");
55             } else if (s[i] == '&') {
56                 // this hack the escape for unicode character, eg : &2345;
57
if ( ((i + 3) < length) &&
58                      (s[i+1] == '#') &&
59                      (s[i+2]>='0'&&s[i+1]<='9') &&
60                      (s[i+3]>='0'&&s[i+2]<='9') ) {
61                     ret.append(s[i]);
62                 // hack &lt; (dont escape this char more than once)
63
} else if ( ((i + 3) < length) &&
64                             (s[i+1] == 'l') &&
65                             (s[i+2] == 't') &&
66                             (s[i+3] == ';') ) {
67                     ret.append(s[i]);
68                 // hack &gt; (dont escape this char more than once)
69
} else if ( ((i + 3) < length) &&
70                             (s[i+1] == 'g') &&
71                             (s[i+2] == 't') &&
72                             (s[i+3] == ';') ) {
73                     ret.append(s[i]);
74                 // hack &amp; (dont escape this char more than once)
75
} else if ( ((i + 4) < length) &&
76                             (s[i+1] == 'a') &&
77                             (s[i+2] == 'm') &&
78                             (s[i+3] == 'p') &&
79                             (s[i+4] == ';') ) {
80                     ret.append(s[i]);
81                 // hack &quot; (dont escape this char more than once)
82
} else if ( ((i + 5) < length) &&
83                             (s[i+1] == 'q') &&
84                             (s[i+2] == 'u') &&
85                             (s[i+3] == 'o') &&
86                             (s[i+4] == 't') &&
87                             (s[i+5] == ';') ) {
88                     ret.append(s[i]);
89                 } else {
90                     ret.append("&amp;");
91                 }
92             } else if (s[i] == '"') {
93                 ret.append("&quot;");
94             } else {
95                 ret.append(s[i]);
96             }
97         }// for
98
return ret.toString();
99     }
100 }
101
Popular Tags