KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.nemesis.forum.Message;
33 import org.nemesis.forum.MessageFilter;
34
35 /**
36  * A ForumMessageFilter that Translates normal text into 'Hacker Speak'.
37  */

38 public class FilterHackerSpeak extends MessageFilter implements Serializable JavaDoc {
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 FilterHackerSpeak() {
62         super();
63         props = new Properties JavaDoc();
64         propDescriptions = new Properties JavaDoc();
65     }
66
67     /**
68      * Creates a new filter wrapped around the specified message. This
69      * constructor is normally called when cloning a filter template.
70      *
71      * @param message the ForumMessage to wrap the new filter around.
72      * @param properties the property values for the filter.
73      * @param propertyDescriptions the property descriptions for the filter.
74      */

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

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

94     public String JavaDoc getName() {
95         return "Hacker Speak";
96     }
97
98     /**
99      * Returns a description of the filter.
100      */

101     public String JavaDoc getDescription() {
102         return "Translates normal text into 'Hacker Speak'.";
103     }
104
105     /**
106      * Returns the author of the filter.
107      */

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

115     public int getMajorVersion() {
116         return 1;
117     }
118
119     /**
120      * Returns a description of the filter.
121      */

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

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

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

149     public Enumeration JavaDoc getFilterPropertyNames() {
150         //No properties, so return null.
151
return null;
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 HTML tags
173      * escaped.
174      */

175     public String JavaDoc getSubject() {
176         return makeHacker(message.getSubject());
177     }
178
179     /**
180      * <b>Overloaded</b> to return the body of the message with HTML tags
181      * escaped.
182      */

183     public String JavaDoc getBody() {
184         return makeHacker(message.getBody());
185     }
186
187     /**
188      * This method takes a string which may contain HTML tags (ie, <b>, <table>,
189      * etc) and converts the '<' and '>' characters to their HTML escape
190      * sequences.
191      *
192      * @param input The text to be converted.
193      * @return The input string with the characters '<' and '>' replaced with
194      * &lt; and &gt; respectively.
195      */

196     private String JavaDoc makeHacker(String JavaDoc input) {
197         //Check if the string is null or zero length -- if so, return
198
//what was sent in.
199
if (input == null || input.length() == 0) {
200             return input;
201         }
202         //Use a StringBuffer in lieu of String concatenation -- it is
203
//much more efficient this way.
204
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(input.length());
205         char ch = ' ';
206         for (int i = 0; i < input.length(); i++) {
207             ch = input.charAt(i);
208             if (Math.random() < .20) {
209                 if (ch == 'a') {
210                     buf.append("@");
211                 } else if (ch == 'e') {
212                     buf.append("3");
213                 } else if (ch == 'g') {
214                     buf.append("6");
215                 } else if (ch == 'i') {
216                     buf.append("1");
217                 } else if (ch == 'o') {
218                     buf.append("0");
219                 } else if (ch == 't') {
220                     buf.append("7");
221                 } else if (ch == 's') {
222                     buf.append("$");
223                 } else {
224                     buf.append(ch);
225                 }
226             } else {
227                 buf.append(ch);
228             }
229         }
230         return buf.toString();
231     }
232 }
233
Popular Tags