KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/EnableHtmlTagFilter.java,v 1.7 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.7 $
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 EnableHtmlTagFilter {
37
38     private EnableHtmlTagFilter() { //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);
49
50         for (int i = 0; i < length; i++) {
51             if (s[i] == '&') {
52                 if ( ((i + 3) < length) &&
53                      (s[i + 1] == 'l') &&
54                      (s[i + 2] == 't') &&
55                      (s[i + 3] == ';')) { // &lt; = <
56
ret.append('<');
57                     i += 3;
58                 } else if ( ((i + 3) < length) &&
59                             (s[i+1] == 'g') &&
60                             (s[i+2] == 't') &&
61                             (s[i+3] == ';') ) { // &gt; = >
62
ret.append('>');
63                     i += 3;
64                 } else if ( ((i + 4) < length) &&
65                             (s[i+1] == 'a') &&
66                             (s[i+2] == 'm') &&
67                             (s[i+3] == 'p') &&
68                             (s[i+4] == ';') ) { // &amp; = &
69
ret.append('&');
70                     i += 4;
71                 } else if ( ((i + 5) < length) &&
72                             (s[i+1] == 'q') &&
73                             (s[i+2] == 'u') &&
74                             (s[i+3] == 'o') &&
75                             (s[i+4] == 't') &&
76                             (s[i+5] == ';') ) { // &quot; = "
77
ret.append('"');
78                     i += 5;
79                 } else {
80                     ret.append('&');
81                 }
82             } else {
83                 ret.append(s[i]);
84             }
85         }// for
86
return ret.toString();
87     }
88
89     /*
90     public static void main(String[] args) {
91         //String input = " &lt; &gt; &amp; &quot;";
92         String input = "&lt&gt; &lt;mvn&gt; &amp; &amp;amp;&quot ;";
93         System.out.println("input = '" + input + "' length = " + input.length());
94         EnableHtmlTagFilter enableEmotionFilter = new EnableHtmlTagFilter();
95
96         long start = System.currentTimeMillis();
97         String output = null;
98         for (int i = 0; i <100; i++) {
99             output = enableEmotionFilter.filter(input);
100         }
101         long time = System.currentTimeMillis() - start;
102         System.out.println("total time = " + time);
103
104         System.out.println(output);
105     }
106     */

107 }
108
Popular Tags