1 25 package org.nemesis.forum.filter; 26 27 import java.io.Serializable ; 28 import java.util.Enumeration ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 import org.nemesis.forum.Message; 33 import org.nemesis.forum.MessageFilter; 34 import org.nemesis.forum.util.StringUtils; 35 36 41 public class FilterFontStyle extends MessageFilter implements Serializable { 42 43 46 private Properties props; 47 48 51 private Properties propDescriptions; 52 public Map getFilterPropertiesDescription(){ 54 return propDescriptions; 55 } 56 61 public FilterFontStyle() { 62 super(); 63 this.props = new Properties (props); 64 this.propDescriptions = new Properties (propDescriptions); 65 initializeProperties(); 66 } 67 68 public Map getFilterProperties(){ 70 return props; 71 } 72 73 81 public FilterFontStyle(Message message, Properties props, Properties propDescriptions) { 82 super(message); 83 this.props = new Properties (props); 84 this.propDescriptions = new Properties (propDescriptions); 85 } 86 87 93 public MessageFilter clone(Message message) { 94 return new FilterFontStyle(message, props, propDescriptions); 95 } 96 97 100 public String getName() { 101 return "Font Stylizer"; 102 } 103 104 107 public String getDescription() { 108 return "Converts custom font tags into HTML equivalents. It " + "currently supports [b][/b] and [i][/i] tags."; 109 } 110 111 114 public String getAuthor() { 115 return "CoolServlets.com"; 116 } 117 118 121 public int getMajorVersion() { 122 return 1; 123 } 124 125 128 public int getMinorVersion() { 129 return 0; 130 } 131 132 138 public String getFilterProperty(String name) { 139 return props.getProperty(name); 140 } 141 142 148 public String getFilterPropertyDescription(String name) { 149 return propDescriptions.getProperty(name); 150 } 151 152 155 public Enumeration getFilterPropertyNames() { 156 return props.propertyNames(); 157 } 158 159 169 public void setFilterProperty(String name, String value) throws IllegalArgumentException { 170 if (props.getProperty(name) == null) { 171 throw new IllegalArgumentException (); 172 } 173 props.put(name, value); 174 } 175 176 180 public String getBody() { 181 return fontStyleToHTML(message.getBody()); 182 } 183 184 187 private void initializeProperties() { 188 props.put("Bold", "on"); 189 props.put("Italics", "on"); 190 191 propDescriptions.put("Bold", "Toggles translation of [b][/b] to HTML bold tags."); 192 propDescriptions.put("Italics", "Toggles translation of [i][/i] to HTML bold tags."); 193 } 194 195 205 private String fontStyleToHTML(String input) { 206 if (input == null || input.length() == 0) { 208 return input; 209 } else { 210 int[] boldStartCount = new int[1]; 213 int[] italicsStartCount = new int[1]; 214 int[] boldEndCount = new int[1]; 215 int[] italicsEndCount = new int[1]; 216 if (props.getProperty("Bold").equals("on")) { 217 input = StringUtils.replace(input, "[b]", "<b>", boldStartCount); 218 input = StringUtils.replace(input, "[/b]", "</b>", boldEndCount); 219 int bStartCount = boldStartCount[0]; 220 int bEndCount = boldEndCount[0]; 221 while (bStartCount > bEndCount) { 222 input = input.concat("</b>"); 223 bEndCount++; 224 } 225 } 226 if (props.getProperty("Italics").equals("on")) { 227 input = StringUtils.replace(input, "[i]", "<i>", italicsStartCount); 228 input = StringUtils.replace(input, "[/i]", "</i>", italicsEndCount); 229 int iStartCount = italicsStartCount[0]; 230 int iEndCount = italicsEndCount[0]; 231 while (iStartCount > iEndCount) { 232 input = input.concat("</i>"); 233 iEndCount++; 234 } 235 } 236 } 237 return input; 238 } 239 } 240 | Popular Tags |