KickJava   Java API By Example, From Geeks To Geeks.

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


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

63
64 public class FilterJavaScript extends ForumMessageFilter implements Serializable JavaDoc {
65     /**
66        * Property values of the filter.
67        */

68       private Properties JavaDoc props;
69
70       /**
71        * Property descriptions of the filter.
72        */

73       private Properties JavaDoc propDescriptions;
74
75       /**
76        * Creates a new filter not associated with a message. This is
77        * generally only useful for defining a template filter that other
78        * fitlers will be cloned from.
79        */

80       public FilterJavaScript() {
81           super();
82           props = new Properties JavaDoc();
83           propDescriptions = new Properties JavaDoc();
84       }
85    /**
86      * Creates a new filter wrapped around the specified message. This
87      * constructor is normally called when cloning a filter template.
88      *
89      * @param message the ForumMessage to wrap the new filter around.
90      */

91     public FilterJavaScript(ForumMessage message, Properties JavaDoc props,
92                             Properties JavaDoc propDescriptions)
93     {
94         super(message);
95         this.props = new Properties JavaDoc(props);
96         this.propDescriptions = new Properties JavaDoc(propDescriptions);
97     }
98
99     /**
100      * Clones a new filter that will have the same properties and that
101      * will wrap around the specified message.
102      *
103      * @param message the ForumMessage to wrap the new filter around.
104      */

105     public ForumMessageFilter clone(ForumMessage message){
106         return new FilterJavaScript(message,props,propDescriptions);
107     }
108
109     /**
110      * Returns the name of the filter.
111      */

112     public String JavaDoc getName() {
113         return "JavaScript Filter";
114     }
115
116     /**
117      * Returns a description of the filter.
118      */

119     public String JavaDoc getDescription() {
120         return "Escapes Script tags by removing everything between &lt;script.. &gt; and &lt;\\script&gt; tags ";
121     }
122
123     /**
124      * Returns the author of the filter.
125      */

126     public String JavaDoc getAuthor() {
127         return "www.yasna.com";
128     }
129
130     /**
131      * Returns the major version number of the filter.
132      */

133     public int getMajorVersion() {
134         return 1;
135     }
136
137     /**
138      * Returns a description of the filter.
139      */

140     public int getMinorVersion() {
141         return 0;
142     }
143
144     /**
145      * Returns the value of a property of the filter.
146      *
147      * @param name the name of the property.
148      */

149     public String JavaDoc getFilterProperty(String JavaDoc name) {
150         return props.getProperty(name);
151     }
152
153     /**
154      * Returns the description of a property of the filter.
155      *
156      * @param name the name of the property.
157      * @return the description of the property.
158      */

159     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
160         return propDescriptions.getProperty(name);
161     }
162
163     /**
164      * Returns an Enumeration of all the property names.
165      */

166     public Enumeration JavaDoc filterPropertyNames() {
167         //No properties, so return null.
168
return null;
169     }
170
171     /**
172      * Sets a property of the filter. Each filter has a set number of
173      * properties that are determined by the filter author.
174      *
175      * @param name the name of the property to set.
176      * @param value the new value for the property.
177      *
178      * @throws IllegalArgumentException if the property trying to be set doesn't
179      * exist.
180      */

181     public void setFilterProperty(String JavaDoc name, String JavaDoc value)
182             throws IllegalArgumentException JavaDoc
183     {
184         if (props.getProperty(name) == null) {
185             throw new IllegalArgumentException JavaDoc();
186         }
187         props.put(name, value);
188     }
189
190     /**
191      * <b>Overloaded</b> to return the subject of the message with HTML tags
192      * escaped.
193      */

194     public String JavaDoc getSubject() {
195         return escapeScriptTags(message.getSubject());
196     }
197
198     /**
199      * <b>Overloaded</b> to return the body of the message with HTML tags
200      * escaped.
201      */

202     public String JavaDoc getBody() {
203         return escapeScriptTags(message.getBody());
204     }
205
206     /**
207      * This method takes a string which may contain script tags (ie, <script>, </script>,
208      * javascript) and removes the characters.
209      *
210      * @param input The text to be converted.
211      * @return The input string with the tags removed
212      */

213     private String JavaDoc escapeScriptTags( String JavaDoc input ) {
214         //Check if the string is null or zero length -- if so, return
215
//what was sent in.
216
if( input == null || input.length() == 0 || input.toLowerCase().indexOf("script")<0 || input.toLowerCase().indexOf("javascript")<0) {
217             return input;
218         }
219         return Pattern.compile("\\<(\\s*)(script)(\\s.*?)?\\>((\\s|.)*?)\\<\\/(\\s*)(script)(\\s.*?)?\\>",Pattern.CASE_INSENSITIVE).matcher(input).replaceAll("");
220     }
221
222 }
223
Popular Tags