KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/EnableEmotionFilter.java,v 1.20 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.20 $
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 EnableEmotionFilter {
37
38     private EnableEmotionFilter() { //prevent instantiation
39
}
40
41     //@todo : localize me
42
static String JavaDoc[][] emotion = {
43     // standard emotion
44
{ "[:))]", "laughing.gif", "laughing"},
45         { "[:)]", "smile.gif", "smile"},
46         { "[:-)]", "smile.gif", "smile"},
47         { "[:((]", "crying.gif", "crying"},
48         { "[:(]", "sad.gif", "sad"},
49         { "[:-(]", "sad.gif", "sad"},
50         { "[;)]", "wink.gif", "wink"},
51         { "[:D]", "biggrin.gif", "biggrin"},
52         { "[;;)]", "batting_eyelashes.gif","batting eyelashes"},
53         { "[:-/]", "confused.gif", "confused"},
54         { "[:x]", "love.gif", "love struck"},
55
56         { "[:\">]", "blushing.gif", "blushing"},
57         { "[:">]", "blushing.gif", "blushing"},
58
59         { "[:p]", "tongue.gif", "tongue"},
60         { "[:*]", "kiss.gif", "kiss"},
61         { "[:O]", "shock.gif", "shock"},
62         { "[X-(]", "angry.gif", "angry"},
63
64         { "[:>]", "smug.gif", "smug"},
65         { "[:>]", "smug.gif", "smug"},
66
67         { "[B-)]", "cool.gif", "cool"},
68         { "[:-s]", "worried.gif", "worried"},
69
70         { "[>:)]", "devilish.gif", "devilish"},
71         { "[>:)]", "devilish.gif", "devilish"},
72
73         { "[:|]", "straight_face.gif", "straight face"},
74         { "[/:)]", "raised_eyebrow.gif", "raised eyebrow"},
75         { "[O:)]", "angel.gif", "angel"},
76         { "[:-B]", "nerd.gif", "nerd"},
77         { "[=;]", "talk_to_the_hand.gif", "talk to the hand"},
78         { "[I-)]", "sleep.gif", "sleep"},
79         { "[8-|]", "rolling_eyes.gif", "rolling eyes"},
80
81         { "[:-&]", "sick.gif", "sick"},
82         { "[:-&]", "sick.gif", "sick"},
83
84         { "[:-$]", "shhh.gif", "shhh"},
85         { "[[-(]", "not_talking.gif", "not talking"},
86         { "[:o)]", "clown.gif", "clown"},
87         { "[8-}]", "silly.gif", "silly"},
88         { "[(:|]", "tired.gif", "tired"},
89         { "[=P~]", "drooling.gif", "drooling"},
90         { "[:-?]", "thinking.gif", "thinking"},
91         { "[#-o]", "d_oh.gif", "d oh"},
92
93         { "[=D>]", "applause.gif", "applause"},
94         { "[=D>]", "applause.gif", "applause"},
95     // hidden emotion
96
{ "[:@)]", "pig.gif", "pig"},
97         { "[3:-O]", "cow.gif", "cow"},
98         { "[:(|)]", "monkey.gif", "monkey"},
99
100         { "[~:>]", "chicken.gif", "chicken"},
101         { "[~:>]", "chicken.gif", "chicken"},
102
103         { "[@};-]", "rose.gif", "rose"},
104         { "[%%-]", "good_luck.gif", "good luck"},
105         { "[**==]", "flag.gif", "flag"},
106         { "[(~~)]", "pumpkin.gif", "pumpkin"},
107         { "[~o)]", "coffee.gif", "coffee"},
108         { "[*-:)]", "idea.gif", "idea"},
109         { "[8-X]", "skull.gif", "skull"},
110         { "[=:)]", "alien_1.gif", "alien 1"},
111
112         { "[>-)]", "alien_2.gif", "alien 2"},
113         { "[>-)]", "alien_2.gif", "alien 2"},
114
115         { "[:-L]", "frustrated.gif", "frustrated"},
116
117         { "[<):)]", "cowboy.gif", "cowboy"},
118         { "[&lt;):)]", "cowboy.gif", "cowboy"},
119
120         { "[[-o<]", "praying.gif", "praying"},
121         { "[[-o&lt;]", "praying.gif", "praying"},
122
123         { "[@-)]", "hypnotized.gif", "hypnotized"},
124         { "[$-)]", "money_eyes.gif", "money eyes"},
125
126         { "[:-\"]", "whistling.gif", "whistling"},
127         { "[:-&quot;]", "whistling.gif", "whistling"},
128
129         { "[:^o]", "liar.gif", "liar"},
130         { "[b-(]", "beat_up.gif", "beat up"},
131
132         { "[:)>-]", "peace.gif", "peace"},
133         { "[:)&gt;-]", "peace.gif", "peace"},
134
135         { "[[-X]", "shame_on_you.gif", "shame on you"},
136         { "[\\:D/]", "dancing.gif", "dancing"},
137
138         { "[>:D<]", "hugs.gif", "hugs"},
139         { "[&gt;:D&lt;]", "hugs.gif", "hugs"},
140     };
141
142     public static String JavaDoc filter(String JavaDoc input, String JavaDoc emotionFolder) {
143         if (input == null) return null;
144
145         int beginIndex = 0;
146         int currentBracketIndex = 0;
147         int inputLength = input.length();
148         int emotionLength = emotion.length;
149         StringBuffer JavaDoc output = new StringBuffer JavaDoc(inputLength * 2);
150
151         if (emotionFolder.endsWith("/") == false) {
152             emotionFolder = emotionFolder + "/";
153         }
154
155         while(beginIndex < inputLength) {
156             currentBracketIndex = input.indexOf('[', beginIndex);
157             if (currentBracketIndex == -1) { // cannot find bracket
158
String JavaDoc remain = input.substring(beginIndex, inputLength);
159                 output.append(remain);
160                 break;
161             }
162
163             // now it means we found the bracket
164
String JavaDoc remain = input.substring(beginIndex, currentBracketIndex);// too slow here !!!
165
output.append(remain);
166             boolean matchFound = false;
167
168             // try to find if it matchs any emotion
169
for (int i = 0; i < emotionLength; i++) {
170                 String JavaDoc currentEmotion = emotion[i][0];
171                 int endIndex = currentBracketIndex + currentEmotion.length();
172                 if (endIndex > inputLength) continue;
173                 String JavaDoc match = input.substring(currentBracketIndex, endIndex);
174                 if (currentEmotion.equals(match)) {
175                     String JavaDoc imgTag = "<img SRC='" + emotionFolder + emotion[i][1] + "' border='0' alt='" + emotion[i][2] + "' title='" + emotion[i][2] + "'>";
176                     output.append(imgTag);
177                     beginIndex = currentBracketIndex + currentEmotion.length();
178                     matchFound = true;
179                     break;
180                 }
181             }// for
182

183             if (matchFound == false) {
184                 beginIndex = currentBracketIndex + 1;
185                 output.append('[');
186             }
187         }// while
188

189         return output.toString();
190     }
191 /*
192     public static void main(String[] args) {
193         String input = " :smile :) grin:)) sad = -:(cry:((minh::>:)bdfdfc:";
194         System.out.println("input = '" + input + "' length = " + input.length());
195         EnableEmotionFilter enableEmotionFilter = new EnableEmotionFilter();
196
197         long start = System.currentTimeMillis();
198         String output = null;
199         for (int i = 0; i <10000; i++) {
200             output = enableEmotionFilter.filter(input, null);
201         }
202         long time = System.currentTimeMillis() - start;
203         System.out.println("total time = " + time);
204
205         System.out.println(output);
206     }
207     */

208 }
209
Popular Tags