KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > filter > FilterSmileyFace


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.filter;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.nemesis.forum.Message;
32 import org.nemesis.forum.MessageFilter;
33
34 /**
35  * A ForumMessageFilter that converts ASCII faces into images.
36  * The faces currently handled are -- are :) , :( , and 8)
37  */

38 public class FilterSmileyFace extends MessageFilter {
39
40     /**
41      * Property values of the filter.
42      */

43     private Properties JavaDoc props;
44     // AJOUT
45
public Map JavaDoc getFilterProperties(){
46                  return props;
47          }
48     /**
49      * Property descriptions of the filter.
50      */

51     private Properties JavaDoc propDescriptions;
52 // AJOUT
53
public Map JavaDoc getFilterPropertiesDescription(){
54                       return propDescriptions;
55               }
56     /**
57      * Creates a new filter not associated with a message. This is
58      * generally only useful for defining a template filter that other
59      * fitlers will be cloned from.
60      */

61     public FilterSmileyFace() {
62         super();
63         props = new Properties JavaDoc();
64         propDescriptions = new Properties JavaDoc();
65         initializeProperties();
66     }
67
68     /**
69      * Creates a new filter wrapped around the specified message. This
70      * constructor is normally called when cloning a filter template.
71      *
72      * @param message the ForumMessage to wrap the new filter around.
73      * @param properties the property values for the filter.
74      * @param propertyDescriptions the property descriptions for the filter.
75      */

76     public FilterSmileyFace(Message message, Properties JavaDoc props, Properties JavaDoc propDescriptions) {
77         super(message);
78         this.props = new Properties JavaDoc(props);
79         this.propDescriptions = new Properties JavaDoc(propDescriptions);
80     }
81
82     /**
83      * Clones a new filter that will have the same properties and that
84      * will wrap around the specified message.
85      *
86      * @param message the ForumMessage to wrap the new filter around.
87      */

88     public MessageFilter clone(Message message) {
89         return new FilterSmileyFace(message, props, propDescriptions);
90     }
91
92     /**
93      * Returns the name of the filter.
94      */

95     public String JavaDoc getName() {
96         return "ASCII Face Converter";
97     }
98
99     /**
100      * Returns a description of the filter.
101      */

102     public String JavaDoc getDescription() {
103         return "Converts ASCII faces to images. The faces currently " + "handled are :) , :( , and 8)";
104     }
105
106     /**
107      * Returns the author of the filter.
108      */

109     public String JavaDoc getAuthor() {
110         return "CoolServlets.com";
111     }
112
113     /**
114      * Returns the major version number of the filter.
115      */

116     public int getMajorVersion() {
117         return 1;
118     }
119
120     /**
121      * Returns the minor version number of the filter.
122      */

123     public int getMinorVersion() {
124         return 0;
125     }
126
127     /**
128      * Returns the value of a property of the filter.
129      *
130      * @param name the name of the property.
131      * @returns the value of the property.
132      */

133     public String JavaDoc getFilterProperty(String JavaDoc name) {
134         return props.getProperty(name);
135     }
136
137     /**
138      * Returns the description of a property of the filter.
139      *
140      * @param name the name of the property.
141      * @return the description of the property.
142      */

143     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
144         return propDescriptions.getProperty(name);
145     }
146
147     /**
148      * Returns an Enumeration of all the property names.
149      */

150     public Enumeration JavaDoc getFilterPropertyNames() {
151         return props.propertyNames();
152     }
153
154     /**
155      * Sets a property of the filter. Each filter has a set number of
156      * properties that are determined by the filter author.
157      *
158      * @param name the name of the property to set.
159      * @param value the new value for the property.
160      *
161      * @throws IllegalArgumentException if the property trying to be set doesn't
162      * exist.
163      */

164     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
165         if (props.getProperty(name) == null) {
166             throw new IllegalArgumentException JavaDoc();
167         }
168         props.put(name, value);
169     }
170
171     /**
172      * <b>Overloaded</b> to return the subject of the message with ASCII
173      * faces converted to images.
174      */

175     public String JavaDoc getSubject() {
176         return addSmileyFace(message.getSubject());
177     }
178
179     /**
180      * <b>Overloaded</b> to return the body of the message with ASCII
181      * faces converted to images.
182      */

183     public String JavaDoc getBody() {
184         return addSmileyFace(message.getBody());
185     }
186
187     /**
188      * Creates properties and sets their descriptions.
189      */

190     private void initializeProperties() {
191         props.put("happyURL", "");
192         props.put("sadURL", "");
193         props.put("coolURL", "");
194
195         propDescriptions.put("happyURL", "URL of the desired :) image");
196         propDescriptions.put("sadURL", "URL of the desired :( image");
197         propDescriptions.put("coolURL", "URL of the desired 8) image");
198     }
199
200     /**
201     * This method takes a string which may contain ':)' and
202     * converts them to smiley face images.
203     *
204     * @param input The text to be converted.
205     * @return The input string with the ':)' replaced
206     * with smiley face images.
207     */

208     private String JavaDoc addSmileyFace(String JavaDoc input) {
209         String JavaDoc happy = props.getProperty("happyURL");
210         String JavaDoc sad = props.getProperty("sadURL");
211         String JavaDoc cool = props.getProperty("coolURL");
212
213         // Check if the string is null or zero length -- if so, return what was sent in.
214
if (input == null || input.length() == 0) {
215             return input;
216         }
217         // Use a StringBuffer in lieu of String concatenation -- it is much more efficient this way.
218
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
219         char ch = ' ', ch2 = ' ';
220         int index;
221         for (index = 0; index < input.length() - 1; index++) {
222             ch = input.charAt(index);
223             ch2 = input.charAt(index + 1);
224             if (happy != null && happy.length() > 0 && ch == ':' && ch2 == ')') {
225                 buf.append("<img SRC=\"").append(happy).append("\">");
226                 index++; // Skip a character
227
} else if (sad != null && sad.length() > 0 && ch == ':' && ch2 == '(') {
228                 buf.append("<img SRC=\"").append(sad).append("\">");
229                 index++; // Skip a character
230
} else if (cool != null && cool.length() > 0 && ch == '8' && ch2 == ')') {
231                 buf.append("<img SRC=\"").append(cool).append("\">");
232                 index++; // Skip a character
233
} else {
234                 buf.append(ch);
235             }
236         }
237         //Append last character if needed.
238
if (index != input.length()) {
239             buf.append(input.charAt(input.length() - 1));
240         }
241         return buf.toString();
242     }
243 }
244
Popular Tags