KickJava   Java API By Example, From Geeks To Geeks.

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


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 escapes all HTML tags to prevent a user
37  * from embedding arbitrary HTML in a message.
38  */

39 public class FilterHtml extends MessageFilter implements Serializable JavaDoc {
40
41     /**
42      * Property values of the filter.
43      */

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

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

62     public FilterHtml() {
63         super();
64         props = new Properties JavaDoc();
65         propDescriptions = new Properties JavaDoc();
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 FilterHtml(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 FilterHtml(message, props, propDescriptions);
90     }
91
92     /**
93      * Returns the name of the filter.
94      */

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

102     public String JavaDoc getDescription() {
103         return "Escapes HTML tags by converting < and > characters " + "into HTML esacpe sequences.";
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 a description 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         //No properties, so return null.
152
return null;
153     }
154
155     /**
156      * Sets a property of the filter. Each filter has a set number of
157      * properties that are determined by the filter author.
158      *
159      * @param name the name of the property to set.
160      * @param value the new value for the property.
161      *
162      * @throws IllegalArgumentException if the property trying to be set doesn't
163      * exist.
164      */

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

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

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

197     private String JavaDoc escapeHTMLTags(String JavaDoc input) {
198         //Check if the string is null or zero length -- if so, return
199
//what was sent in.
200
if (input == null || input.length() == 0) {
201             return input;
202         }
203         //Use a StringBuffer in lieu of String concatenation -- it is
204
//much more efficient this way.
205
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(input.length() + 6);
206         char ch = ' ';
207         for (int i = 0; i < input.length(); i++) {
208             ch = input.charAt(i);
209             if (ch == '<') {
210                 buf.append("&lt;");
211             } else if (ch == '>') {
212                 buf.append("&gt;");
213             } else {
214                 buf.append(ch);
215             }
216         }
217         return buf.toString();
218     }
219 }
220
Popular Tags