KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

81       public FilterImageResize() {
82           super();
83           props = new Properties JavaDoc();
84           props.put("MaxWidth","200");
85           propDescriptions = new Properties JavaDoc();
86           propDescriptions.put("MaxWidth","Maximum width of an image tag in the post");
87       }
88    /**
89      * Creates a new filter wrapped around the specified message. This
90      * constructor is normally called when cloning a filter template.
91      *
92      * @param message the ForumMessage to wrap the new filter around.
93      */

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

108     public ForumMessageFilter clone(ForumMessage message){
109         return new FilterImageResize(message,props,propDescriptions);
110     }
111
112     /**
113      * Returns the name of the filter.
114      */

115     public String JavaDoc getName() {
116         return "Image Resize Filter";
117     }
118
119     /**
120      * Returns a description of the filter.
121      */

122     public String JavaDoc getDescription() {
123         return "This filter will look for the &lt;img tag and replace the width with the maximum size allowed and remove the height";
124     }
125
126     /**
127      * Returns the author of the filter.
128      */

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

136     public int getMajorVersion() {
137         return 1;
138     }
139
140     /**
141      * Returns a description of the filter.
142      */

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

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

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

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

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

197     public String JavaDoc getSubject() {
198         return checkimagewidth(message.getSubject());
199     }
200
201     /**
202      * <b>Overloaded</b> to return the body of the message with HTML tags
203      * escaped.
204      */

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

216     private String JavaDoc checkimagewidth( String JavaDoc input ) {
217         //Check if the string is null or zero length -- if so, return
218
//what was sent in.
219
if( input == null || input.length() == 0 ) {
220             return input;
221         }
222         Pattern JavaDoc pa = Pattern.compile("\\<\\/?(\\s*)(img)(\\s.*?)(([a-z]+)\\s*=\\s*\"([^\"]+)\")(\\s.*?)?\\>",Pattern.CASE_INSENSITIVE);
223         Pattern JavaDoc paW = Pattern.compile("(width)\\s*=\\s*(\"|')?([0-9]+)(\"|')?",Pattern.CASE_INSENSITIVE);
224         Pattern JavaDoc paH = Pattern.compile("(height)\\s*=\\s*(\"|')?([0-9]+)(\"|')?",Pattern.CASE_INSENSITIVE);
225         Matcher JavaDoc match = pa.matcher(input);
226         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
227         int maxw = Integer.parseInt(props.getProperty("MaxWidth"));
228                 while (match.find()) {
229                     String JavaDoc tmp = match.group(0);
230                     Matcher JavaDoc matchW = paW.matcher(tmp);
231                     if(matchW.find()){ //look for width
232
if(Integer.parseInt(matchW.group(3).trim())>maxw){
233                          tmp=matchW.replaceAll("width=\""+maxw+"\"");
234                           //we now remove the height tag
235
Matcher JavaDoc matchH = paH.matcher(tmp);
236                           if(matchH.find()){
237                               tmp = matchH.replaceAll("");
238                           }
239                           //we now replace the original tag with the new tag
240
match.appendReplacement(sb,tmp);
241                       }
242
243                     }
244                  }
245         match.appendTail(sb);
246
247         return sb.toString();
248     }
249
250 }
251
Popular Tags