KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/EnableMVNCodeFilter.java,v 1.15 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.15 $
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 EnableMVNCodeFilter {
37
38     private EnableMVNCodeFilter() { //prevent instantiation
39
}
40
41     static String JavaDoc[][] mvnCode = {
42         { "[b]", "<b>" },
43         { "[/b]", "</b>" },
44         { "[i]", "<i>" },
45         { "[/i]", "</i>" },
46         { "[u]", "<u>" },
47         { "[/u]", "</u>" },
48         { "[s]", "<s>" },
49         { "[/s]", "</s>" },
50
51         { "[hr]", "<hr>" },
52         { "[br]", "<br>" },
53         { "[p]", "<p>" },
54
55         { "[h1]", "<h1>" },
56         { "[/h1]", "</h1>" },
57         { "[h2]", "<h2>" },
58         { "[/h2]", "</h2>" },
59         { "[h3]", "<h3>" },
60         { "[/h3]", "</h3>" },
61         { "[h4]", "<h4>" },
62         { "[/h4]", "</h4>" },
63         { "[h5]", "<h5>" },
64         { "[/h5]", "</h5>" },
65         { "[h6]", "<h6>" },
66         { "[/h6]", "</h6>" },
67
68         { "[/size]", "</font>" },
69         { "[size=+1]", "<font size=\"+1\">" },
70         { "[size=+2]", "<font size=\"+2\">" },
71         { "[size=+3]", "<font size=\"+3\">" },
72         { "[size=+4]", "<font size=\"+4\">" },
73         { "[size=+5]", "<font size=\"+5\">" },
74         { "[size=+6]", "<font size=\"+6\">" },
75
76         { "[size=1]", "<font size=\"1\">" },
77         { "[size=2]", "<font size=\"2\">" },
78         { "[size=3]", "<font size=\"3\">" },
79         { "[size=4]", "<font size=\"4\">" },
80         { "[size=5]", "<font size=\"5\">" },
81         { "[size=6]", "<font size=\"6\">" },
82
83         { "[size=-1]", "<font size=\"-1\">" },
84         { "[size=-2]", "<font size=\"-2\">" },
85         { "[size=-3]", "<font size=\"-3\">" },
86         { "[size=-4]", "<font size=\"-4\">" },
87         { "[size=-5]", "<font size=\"-5\">" },
88         { "[size=-6]", "<font size=\"-6\">" },
89
90         { "[/font]", "</font>" },
91         { "[font=arial]", "<font face=\"arial\">" },
92         { "[font=times new roman]", "<font face=\"times new roman\">" },
93         { "[font=courier new]", "<font face=\"courier new\">" },
94         { "[font=century gothic]", "<font face=\"Century Gothic\">" },
95
96         { "[/color]", "</span>" },
97         { "[color=skyblue]", "<span style=\"color: skyblue\">" },
98         { "[color=royalblue]", "<span style=\"color: royalblue\">" },
99         { "[color=blue]", "<span style=\"color: blue\">" },
100         { "[color=darkblue]", "<span style=\"color: darkblue\">" },
101         { "[color=orange]", "<span style=\"color: orange\">" },
102         { "[color=orangered]", "<span style=\"color: orangered\">" },
103         { "[color=crimson]", "<span style=\"color: crimson\">" },
104         { "[color=red]", "<span style=\"color: red\">" },
105         { "[color=firebrick]", "<span style=\"color: firebrick\">" },
106         { "[color=darkred]", "<span style=\"color: darkred\">" },
107         { "[color=green]", "<span style=\"color: green\">" },
108         { "[color=limegreen]", "<span style=\"color: limegreen\">" },
109         { "[color=seagreen]", "<span style=\"color: seagreen\">" },
110         { "[color=deeppink]", "<span style=\"color: deeppink\">" },
111         { "[color=tomato]", "<span style=\"color: tomato\">" },
112         { "[color=coral]", "<span style=\"color: coral\">" },
113         { "[color=purple]", "<span style=\"color: purple\">" },
114         { "[color=indigo]", "<span style=\"color: indigo\">" },
115         { "[color=burlywood]", "<span style=\"color: burlywood\">" },
116         { "[color=sandybrown]", "<span style=\"color: sandybrown\">" },
117         { "[color=sienna]", "<span style=\"color: sienna\">" },
118         { "[color=chocolate]", "<span style=\"color: chocolate\">" },
119         { "[color=teal]", "<span style=\"color: teal\">" },
120         { "[color=silver]", "<span style=\"color: silver\">" },
121         { "[color=brown]", "<span style=\"color: brown\">" },
122         { "[color=yellow]", "<span style=\"color: yellow\">" },
123         { "[color=olive]", "<span style=\"color: olive\">" },
124         { "[color=cyan]", "<span style=\"color: cyan\">" },
125         { "[color=violet]", "<span style=\"color: violet\">" },
126         { "[color=white]", "<span style=\"color: white\">" },
127         { "[color=black]", "<span style=\"color: black\">" },
128         { "[color=pink]", "<span style=\"color: pink\">" },
129         { "[color=purple]", "<span style=\"color: purple\">" },
130         { "[color=navy]", "<span style=\"color: navy\">" },
131         { "[color=beige]", "<span style=\"color: beige\">" },
132
133         { "[list]", "<ul>" },
134         { "[/list]", "</ul>" },
135
136         { "[list=1]", "<ul type=\"1\">" },
137         { "[/list=1]", "</ul>" },
138
139         { "[list=a]", "<ul type=\"a\">" },
140         { "[/list=a]", "</ul>" },
141
142         { "[list=A]", "<ul type=\"A\">" },
143         { "[/list=A]", "</ul>" },
144
145         { "[list=i]", "<ul type=\"i\">" },
146         { "[/list=i]", "</ul>" },
147
148         { "[list=I]", "<ul type=\"I\">" },
149         { "[/list=I]", "</ul>" },
150
151         { "[*]", "<li>" },
152
153         { "[code]", "<pre>" },
154         { "[/code]", "</pre>" },
155
156         { "[quote]" , "<table width='96%' cellspacing='1' cellpadding='3' border='0' align='center'><tr><td>" +
157 // "<span class='genmed'><b>##### wrote:</b></span>" + There can be something like 'wassup on Mon 12 Jan 2003 wrote:'
158
"&nbsp;"+
159                                 "</td></tr><tr><td class='quote'>"},
160         { "[/quote]" , "</td></tr></table>"},
161     };
162
163     public static String JavaDoc filter(String JavaDoc input) {
164         int beginIndex = 0;
165         int currentBracketIndex = 0;
166         int inputLength = input.length();
167         int mvnCodeLength = mvnCode.length;
168         StringBuffer JavaDoc output = new StringBuffer JavaDoc(inputLength * 2);// is it the best init value ?
169

170         while(beginIndex < inputLength) {
171             currentBracketIndex = input.indexOf('[', beginIndex);
172             if (currentBracketIndex == -1) { // cannot find bracket
173
String JavaDoc remain = input.substring(beginIndex, inputLength);// slow here !
174
output.append(remain);
175                 break;
176             }
177
178             // now it means we found the bracket
179
String JavaDoc remain = input.substring(beginIndex, currentBracketIndex);// slow here !
180
output.append(remain);
181             boolean matchFound = false;
182
183             // try to find if it matchs any mvnCode
184
for (int i = 0; i < mvnCodeLength; i++) {
185                 String JavaDoc currentEmotion = mvnCode[i][0];
186                 int endIndex = currentBracketIndex + currentEmotion.length();
187                 if (endIndex > inputLength) continue;
188                 String JavaDoc match = input.substring(currentBracketIndex, endIndex);// too slow here !!!
189
if (currentEmotion.equals(match)) {
190                     output.append(mvnCode[i][1]);
191                     beginIndex = currentBracketIndex + currentEmotion.length();
192                     matchFound = true;
193                     break;
194                 }
195             }// for
196

197             if (matchFound == false) {
198                 beginIndex = currentBracketIndex + 1;
199                 output.append('[');
200             }
201         }// while
202

203         return output.toString();
204     }
205
206     /*
207     public static void main(String[] args) {
208         String input = " [][b]smile[/b] [ib][/i]]/b]) grin[)) sad = -[(cry[((minh[[>[)bdfdfc[";
209         System.out.println("input = '" + input + "' length = " + input.length());
210         EnableMVNCodeFilter enableMVNCodeFilter = new EnableMVNCodeFilter();
211
212         long start = System.currentTimeMillis();
213         String output = null;
214         for (int i = 0; i <10000; i++) {
215             output = enableMVNCodeFilter.filter(input);
216         }
217         long time = System.currentTimeMillis() - start;
218         System.out.println("total time = " + time);
219
220         System.out.println("output= '" + output + "'");
221     }
222     */

223 }
224
Popular Tags