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 ; 7 import java.util.Properties ; 8 import java.util.Enumeration ; 9 import java.util.regex.Pattern ; 10 import java.util.regex.Matcher ; 11 12 64 65 public class FilterImageResize extends ForumMessageFilter implements Serializable { 66 69 private Properties props; 70 71 74 private Properties propDescriptions; 75 76 81 public FilterImageResize() { 82 super(); 83 props = new Properties (); 84 props.put("MaxWidth","200"); 85 propDescriptions = new Properties (); 86 propDescriptions.put("MaxWidth","Maximum width of an image tag in the post"); 87 } 88 94 public FilterImageResize(ForumMessage message, Properties props, 95 Properties propDescriptions) 96 { 97 super(message); 98 this.props = new Properties (props); 99 this.propDescriptions = new Properties (propDescriptions); 100 } 101 102 108 public ForumMessageFilter clone(ForumMessage message){ 109 return new FilterImageResize(message,props,propDescriptions); 110 } 111 112 115 public String getName() { 116 return "Image Resize Filter"; 117 } 118 119 122 public String getDescription() { 123 return "This filter will look for the <img tag and replace the width with the maximum size allowed and remove the height"; 124 } 125 126 129 public String getAuthor() { 130 return "www.yasna.com"; 131 } 132 133 136 public int getMajorVersion() { 137 return 1; 138 } 139 140 143 public int getMinorVersion() { 144 return 0; 145 } 146 147 152 public String getFilterProperty(String name) { 153 return props.getProperty(name); 154 } 155 156 162 public String getFilterPropertyDescription(String name) { 163 return propDescriptions.getProperty(name); 164 } 165 166 169 public Enumeration filterPropertyNames() { 170 return props.propertyNames(); 172 } 173 174 184 public void setFilterProperty(String name, String value) 185 throws IllegalArgumentException 186 { 187 if (props.getProperty(name) == null || Integer.parseInt(value) < 10) { 188 throw new IllegalArgumentException (); 189 } 190 props.put(name, value); 191 } 192 193 197 public String getSubject() { 198 return checkimagewidth(message.getSubject()); 199 } 200 201 205 public String getBody() { 206 return checkimagewidth(message.getBody()); 207 } 208 209 216 private String checkimagewidth( String input ) { 217 if( input == null || input.length() == 0 ) { 220 return input; 221 } 222 Pattern pa = Pattern.compile("\\<\\/?(\\s*)(img)(\\s.*?)(([a-z]+)\\s*=\\s*\"([^\"]+)\")(\\s.*?)?\\>",Pattern.CASE_INSENSITIVE); 223 Pattern paW = Pattern.compile("(width)\\s*=\\s*(\"|')?([0-9]+)(\"|')?",Pattern.CASE_INSENSITIVE); 224 Pattern paH = Pattern.compile("(height)\\s*=\\s*(\"|')?([0-9]+)(\"|')?",Pattern.CASE_INSENSITIVE); 225 Matcher match = pa.matcher(input); 226 StringBuffer sb=new StringBuffer (); 227 int maxw = Integer.parseInt(props.getProperty("MaxWidth")); 228 while (match.find()) { 229 String tmp = match.group(0); 230 Matcher matchW = paW.matcher(tmp); 231 if(matchW.find()){ if(Integer.parseInt(matchW.group(3).trim())>maxw){ 233 tmp=matchW.replaceAll("width=\""+maxw+"\""); 234 Matcher matchH = paH.matcher(tmp); 236 if(matchH.find()){ 237 tmp = matchH.replaceAll(""); 238 } 239 match.appendReplacement(sb,tmp); 241 } 242 243 } 244 } 245 match.appendTail(sb); 246 247 return sb.toString(); 248 } 249 250 } 251 | Popular Tags |