01: /*
02: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/DisableHtmlTagFilter.java,v 1.11 2007/01/15 10:31:10 dungbtm Exp $
03: * $Author: dungbtm $
04: * $Revision: 1.11 $
05: * $Date: 2007/01/15 10:31:10 $
06: *
07: * ====================================================================
08: *
09: * Copyright (C) 2002-2007 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 filter(String input) {
42: if (input == null) {
43: return null;
44: }
45:
46: char[] s = input.toCharArray();
47: int length = s.length;
48: StringBuffer ret = new StringBuffer(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("<");
53: } else if (s[i] == '>') {
54: ret.append(">");
55: } else if (s[i] == '&') {
56: // this hack the escape for unicode character, eg : &2345;
57: if (((i + 3) < length) && (s[i + 1] == '#')
58: && (s[i + 2] >= '0' && s[i + 1] <= '9')
59: && (s[i + 3] >= '0' && s[i + 2] <= '9')) {
60: ret.append(s[i]);
61: // hack < (dont escape this char more than once)
62: } else if (((i + 3) < length) && (s[i + 1] == 'l')
63: && (s[i + 2] == 't') && (s[i + 3] == ';')) {
64: ret.append(s[i]);
65: // hack > (dont escape this char more than once)
66: } else if (((i + 3) < length) && (s[i + 1] == 'g')
67: && (s[i + 2] == 't') && (s[i + 3] == ';')) {
68: ret.append(s[i]);
69: // hack & (dont escape this char more than once)
70: } else if (((i + 4) < length) && (s[i + 1] == 'a')
71: && (s[i + 2] == 'm') && (s[i + 3] == 'p')
72: && (s[i + 4] == ';')) {
73: ret.append(s[i]);
74: // hack " (dont escape this char more than once)
75: } else if (((i + 5) < length) && (s[i + 1] == 'q')
76: && (s[i + 2] == 'u') && (s[i + 3] == 'o')
77: && (s[i + 4] == 't') && (s[i + 5] == ';')) {
78: ret.append(s[i]);
79: } else {
80: ret.append("&");
81: }
82: } else if (s[i] == '"') {
83: ret.append(""");
84: } else {
85: ret.append(s[i]);
86: }
87: }// for
88: return ret.toString();
89: }
90: }
|