1 53 54 106 107 package com.Yasna.forum.filter; 108 109 import java.io.*; 110 import java.util.*; 111 import com.Yasna.forum.*; 112 import com.Yasna.codeviewer.*; 113 114 118 public class FilterCodeHighlight extends ForumMessageFilter 119 implements Serializable 120 { 121 122 private static final String NEW_LINE = System.getProperty("line.separator"); 123 124 127 private Properties props; 128 129 132 private Properties propDescriptions; 133 134 138 private transient CodeViewer cv; 139 140 145 public FilterCodeHighlight() { 146 super(); 147 props = new Properties(); 148 propDescriptions = new Properties(); 149 cv = new CodeViewer(); 150 initializeProperties(); 151 } 152 153 161 public FilterCodeHighlight(ForumMessage message, Properties properties, 162 Properties propertyDescriptions) 163 { 164 super(message); 165 this.props = new Properties(properties); 166 this.propDescriptions = new Properties(propertyDescriptions); 167 cv = new CodeViewer(); 168 applyProperties(); 169 } 170 171 177 public ForumMessageFilter clone(ForumMessage message){ 178 return new FilterCodeHighlight(message, props, propDescriptions); 179 } 180 181 184 public String getName() { 185 return "Java Code Syntax Highlighter"; 186 } 187 188 191 public String getDescription() { 192 return "Highlights Java code that appears between [code][/code] tags"; 193 } 194 195 198 public String getAuthor() { 199 return "CoolServlets.com"; 200 } 201 202 205 public int getMajorVersion() { 206 return 1; 207 } 208 209 212 public int getMinorVersion() { 213 return 0; 214 } 215 216 222 public String getFilterProperty(String name) { 223 return props.getProperty(name); 224 } 225 226 232 public String getFilterPropertyDescription(String name) { 233 return propDescriptions.getProperty(name); 234 } 235 236 239 public Enumeration filterPropertyNames() { 240 return props.propertyNames(); 241 } 242 243 253 public void setFilterProperty(String name, String value) 254 throws IllegalArgumentException 255 { 256 if (props.getProperty(name) == null) { 257 throw new IllegalArgumentException (); 258 } 259 props.put(name, value); 260 applyProperties(); 261 } 262 263 267 public String getBody() { 268 return highlightCode(message.getBody()); 269 } 270 271 274 private void initializeProperties() { 275 props.put("commentStart",cv.getCommentStart()); 276 props.put("commentEnd",cv.getCommentEnd()); 277 props.put("stringStart",cv.getStringStart()); 278 props.put("stringEnd",cv.getStringEnd()); 279 props.put("reservedWordStart",cv.getReservedWordStart()); 280 props.put("reservedWordEnd",cv.getReservedWordEnd()); 281 282 propDescriptions.put("commentStart","A HTML start tag that determines how comments will be displayed"); 283 propDescriptions.put("commentEnd","A HTML end tag that should correspond to the commentStart tag"); 284 propDescriptions.put("stringStart","A HTML start tag that determines how strings will be displayed"); 285 propDescriptions.put("stringEnd","A HTML end tag that should correspond to the stringStart tag"); 286 propDescriptions.put("reservedWordStart","A HTML start tag that determines how reserved words will be displayed"); 287 propDescriptions.put("reservedWordEnd","A HTML end tag that should correspond to the reservedWordStart tag"); 288 } 289 290 private void applyProperties() { 291 cv.setCommentStart(props.getProperty("commentStart")); 292 cv.setCommentEnd(props.getProperty("commentEnd")); 293 cv.setStringStart(props.getProperty("stringStart")); 294 cv.setStringEnd(props.getProperty("stringEnd")); 295 cv.setReservedWordStart(props.getProperty("reservedWordStart")); 296 cv.setReservedWordEnd(props.getProperty("reservedWordEnd")); 297 } 298 299 306 private String highlightCode( String input ) { 307 if( input == null || input.length() == 0 ) { 309 return input; 310 } 311 else { 312 StringBuffer buf = new StringBuffer (); 313 int i = 0, j = 0, oldend = 0; 314 315 while ( ( i=input.indexOf( "[code]", oldend ) ) >= 0 ) { 316 if ( (j=input.indexOf( "[/code]", i+6 ) ) < 0) { 318 j = input.length()-7; 320 } 321 buf.append(input.substring(oldend,i)); 323 buf.append("<pre>"); 324 StringTokenizer tokens = new StringTokenizer(input.substring(i+6,j), NEW_LINE); 327 while (tokens.hasMoreTokens()) { 328 buf.append(cv.syntaxHighlight(tokens.nextToken())); 329 buf.append(NEW_LINE); 330 } 331 buf.append("</pre>"); 332 oldend = j+7; 334 } 335 buf.append(input.substring(oldend,input.length())); 336 return buf.toString(); 337 } 338 } 339 } 340 | Popular Tags |