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 import java.util.StringTokenizer ; 32 33 import org.nemesis.forum.Message; 34 import org.nemesis.forum.MessageFilter; 35 import org.nemesis.forum.util.tool.CodeViewer; 36 37 41 public class FilterCodeHighlight extends MessageFilter implements Serializable { 42 43 private static final String NEW_LINE = System.getProperty("line.separator"); 44 45 48 private Properties props; 49 50 53 private Properties propDescriptions; 54 55 59 private transient CodeViewer cv=new CodeViewer();; 60 61 66 public FilterCodeHighlight() { 67 super(); 68 props = new Properties (); 69 propDescriptions = new Properties (); 70 cv = new CodeViewer(); 71 initializeProperties(); 72 } 73 74 82 public FilterCodeHighlight(Message message, Properties properties, Properties propertyDescriptions) { 83 super(message); 84 this.props = new Properties (properties); 85 this.propDescriptions = new Properties (propertyDescriptions); 86 cv = new CodeViewer(); 87 applyProperties(); 88 } 89 90 96 public MessageFilter clone(Message message) { 97 return new FilterCodeHighlight(message, props, propDescriptions); 98 } 99 100 103 public String getName() { 104 return "Java Code Syntax Highlighter"; 105 } 106 107 110 public String getDescription() { 111 return "Highlights Java code that appears between [code][/code] tags"; 112 } 113 114 117 public String getAuthor() { 118 return "CoolServlets.com"; 119 } 120 121 124 public int getMajorVersion() { 125 return 1; 126 } 127 128 131 public int getMinorVersion() { 132 return 0; 133 } 134 135 141 public String getFilterProperty(String name) { 142 return props.getProperty(name); 143 } 144 145 151 public String getFilterPropertyDescription(String name) { 152 return propDescriptions.getProperty(name); 153 } 154 155 158 public Enumeration getFilterPropertyNames() { 159 return props.propertyNames(); 160 } 161 162 public Map getFilterProperties(){ 164 return props; 165 } 166 public Map getFilterPropertiesDescription(){ 167 return propDescriptions; 168 } 169 170 180 public void setFilterProperty(String name, String value) throws IllegalArgumentException { 181 if (props.getProperty(name) == null) { 182 throw new IllegalArgumentException (); 183 } 184 props.put(name, value); 185 applyProperties(); 186 } 187 188 192 public String getBody() { 193 return highlightCode(message.getBody()); 194 } 195 196 199 private void initializeProperties() { 200 props.put("commentStart", cv.getCommentStart()); 201 props.put("commentEnd", cv.getCommentEnd()); 202 props.put("stringStart", cv.getStringStart()); 203 props.put("stringEnd", cv.getStringEnd()); 204 props.put("reservedWordStart", cv.getReservedWordStart()); 205 props.put("reservedWordEnd", cv.getReservedWordEnd()); 206 207 propDescriptions.put("commentStart", "A HTML start tag that determines how comments will be displayed"); 208 propDescriptions.put("commentEnd", "A HTML end tag that should correspond to the commentStart tag"); 209 propDescriptions.put("stringStart", "A HTML start tag that determines how strings will be displayed"); 210 propDescriptions.put("stringEnd", "A HTML end tag that should correspond to the stringStart tag"); 211 propDescriptions.put( 212 "reservedWordStart", 213 "A HTML start tag that determines how reserved words will be displayed"); 214 propDescriptions.put("reservedWordEnd", "A HTML end tag that should correspond to the reservedWordStart tag"); 215 } 216 217 private void applyProperties() { 218 cv.setCommentStart(props.getProperty("commentStart")); 219 cv.setCommentEnd(props.getProperty("commentEnd")); 220 cv.setStringStart(props.getProperty("stringStart")); 221 cv.setStringEnd(props.getProperty("stringEnd")); 222 cv.setReservedWordStart(props.getProperty("reservedWordStart")); 223 cv.setReservedWordEnd(props.getProperty("reservedWordEnd")); 224 } 225 226 233 private String highlightCode(String input) { 234 if (input == null || input.length() == 0) { 236 return input; 237 } else { 238 StringBuffer buf = new StringBuffer (); 239 int i = 0, j = 0, oldend = 0; 240 241 while ((i = input.indexOf("[code]", oldend)) >= 0) { 242 if ((j = input.indexOf("[/code]", i + 6)) < 0) { 244 j = input.length() - 7; 246 } 247 buf.append(input.substring(oldend, i)); 249 buf.append("<pre>"); 250 StringTokenizer tokens = new StringTokenizer (input.substring(i + 6, j), NEW_LINE); 253 while (tokens.hasMoreTokens()) { 254 buf.append(cv.syntaxHighlight(tokens.nextToken())); 255 buf.append(NEW_LINE); 256 } 257 buf.append("</pre>"); 258 oldend = j + 7; 260 } 261 buf.append(input.substring(oldend, input.length())); 262 return buf.toString(); 263 } 264 } 265 } 266 | Popular Tags |