KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > filter > FilterSmileyFace


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.filter;
108
109 import java.util.*;
110
111 import com.Yasna.forum.*;
112
113 /**
114  * A ForumMessageFilter that converts ASCII faces into images.
115  * The faces currently handled are -- are :) , :( , and 8)
116  */

117 public class FilterSmileyFace extends ForumMessageFilter {
118
119     /**
120      * Property values of the filter.
121      */

122     private Properties props;
123
124     /**
125      * Property descriptions of the filter.
126      */

127     private Properties propDescriptions;
128
129     /**
130      * Creates a new filter not associated with a message. This is
131      * generally only useful for defining a template filter that other
132      * fitlers will be cloned from.
133      */

134     public FilterSmileyFace() {
135         super();
136         props = new Properties();
137         propDescriptions = new Properties();
138         initializeProperties();
139     }
140
141     /**
142      * Creates a new filter wrapped around the specified message. This
143      * constructor is normally called when cloning a filter template.
144      *
145      * @param message the ForumMessage to wrap the new filter around.
146      * @param properties the property values for the filter.
147      * @param propertyDescriptions the property descriptions for the filter.
148      */

149     public FilterSmileyFace(ForumMessage message, Properties props,
150             Properties propDescriptions)
151     {
152         super(message);
153         this.props = new Properties(props);
154         this.propDescriptions = new Properties(propDescriptions);
155     }
156
157     /**
158      * Clones a new filter that will have the same properties and that
159      * will wrap around the specified message.
160      *
161      * @param message the ForumMessage to wrap the new filter around.
162      */

163     public ForumMessageFilter clone(ForumMessage message){
164         return new FilterSmileyFace(message, props, propDescriptions);
165     }
166
167     /**
168      * Returns the name of the filter.
169      */

170     public String JavaDoc getName() {
171         return "ASCII Face Converter";
172     }
173
174     /**
175      * Returns a description of the filter.
176      */

177     public String JavaDoc getDescription() {
178         return "Converts ASCII faces to images. The faces currently " +
179                 "handled are :) , :( , and 8)";
180     }
181
182     /**
183      * Returns the author of the filter.
184      */

185     public String JavaDoc getAuthor() {
186         return "CoolServlets.com";
187     }
188
189     /**
190      * Returns the major version number of the filter.
191      */

192     public int getMajorVersion() {
193         return 1;
194     }
195
196     /**
197      * Returns the minor version number of the filter.
198      */

199     public int getMinorVersion() {
200         return 0;
201     }
202
203     /**
204      * Returns the value of a property of the filter.
205      *
206      * @param name the name of the property.
207      * @returns the value of the property.
208      */

209     public String JavaDoc getFilterProperty(String JavaDoc name) {
210         return props.getProperty(name);
211     }
212
213     /**
214      * Returns the description of a property of the filter.
215      *
216      * @param name the name of the property.
217      * @return the description of the property.
218      */

219     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
220         return propDescriptions.getProperty(name);
221     }
222
223     /**
224      * Returns an Enumeration of all the property names.
225      */

226     public Enumeration filterPropertyNames() {
227         return props.propertyNames();
228     }
229
230     /**
231      * Sets a property of the filter. Each filter has a set number of
232      * properties that are determined by the filter author.
233      *
234      * @param name the name of the property to set.
235      * @param value the new value for the property.
236      *
237      * @throws IllegalArgumentException if the property trying to be set doesn't
238      * exist.
239      */

240     public void setFilterProperty(String JavaDoc name, String JavaDoc value)
241             throws IllegalArgumentException JavaDoc
242     {
243         if (props.getProperty(name) == null) {
244             throw new IllegalArgumentException JavaDoc();
245         }
246         props.put(name, value);
247     }
248
249     /**
250      * <b>Overloaded</b> to return the subject of the message with ASCII
251      * faces converted to images.
252      */

253     public String JavaDoc getSubject() {
254         return addSmileyFace(message.getSubject());
255     }
256
257     /**
258      * <b>Overloaded</b> to return the body of the message with ASCII
259      * faces converted to images.
260      */

261     public String JavaDoc getBody() {
262         return addSmileyFace(message.getBody());
263     }
264
265     /**
266      * Creates properties and sets their descriptions.
267      */

268     private void initializeProperties() {
269         props.put("happyURL","");
270         props.put("sadURL","");
271         props.put("coolURL","");
272
273         propDescriptions.put("happyURL","URL of the desired :) image");
274         propDescriptions.put("sadURL","URL of the desired :( image");
275         propDescriptions.put("coolURL","URL of the desired 8) image");
276     }
277
278     /**
279     * This method takes a string which may contain ':)' and
280     * converts them to smiley face images.
281     *
282     * @param input The text to be converted.
283     * @return The input string with the ':)' replaced
284     * with smiley face images.
285     */

286     private String JavaDoc addSmileyFace( String JavaDoc input ) {
287         String JavaDoc happy = props.getProperty("happyURL");
288         String JavaDoc sad = props.getProperty("sadURL");
289         String JavaDoc cool = props.getProperty("coolURL");
290
291         // Check if the string is null or zero length -- if so, return what was sent in.
292
if( input == null || input.length() == 0 ) {
293             return input;
294         }
295         // Use a StringBuffer in lieu of String concatenation -- it is much more efficient this way.
296
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
297         char ch = ' ', ch2 = ' ';
298         int index;
299         for(index = 0; index<input.length()-1; index++ ) {
300             ch = input.charAt(index); ch2 = input.charAt(index+1);
301             if (happy != null && happy.length() > 0 && ch == ':' && ch2 == ')') {
302                 buf.append("<img SRC=\"").append(happy).append("\">");
303                 index++; // Skip a character
304
}
305             else if (sad != null && sad.length() > 0 && ch == ':' && ch2 == '(') {
306                 buf.append("<img SRC=\"").append(sad).append("\">");
307                 index++; // Skip a character
308
}
309             else if (cool != null && cool.length() > 0 && ch == '8' && ch2 == ')') {
310                 buf.append("<img SRC=\"").append(cool).append("\">");
311                 index++; // Skip a character
312
}
313             else {
314                 buf.append( ch );
315             }
316         }
317         //Append last character if needed.
318
if (index != input.length()) {
319             buf.append(input.charAt(input.length()-1));
320         }
321         return buf.toString();
322     }
323 }
324
Popular Tags