1 16 17 import java.io.StringWriter ; 18 import java.util.Properties ; 19 import org.apache.velocity.app.Velocity; 20 import org.apache.velocity.VelocityContext; 21 22 import org.apache.velocity.exception.ParseErrorException; 23 import org.apache.velocity.exception.MethodInvocationException; 24 25 import org.apache.velocity.runtime.log.LogSystem; 26 import org.apache.velocity.runtime.RuntimeServices; 27 28 import org.apache.velocity.app.event.EventCartridge; 29 import org.apache.velocity.app.event.ReferenceInsertionEventHandler; 30 import org.apache.velocity.app.event.MethodExceptionEventHandler; 31 import org.apache.velocity.app.event.NullSetEventHandler; 32 33 import org.apache.velocity.context.Context; 34 35 44 45 public class EventExample implements ReferenceInsertionEventHandler, 46 NullSetEventHandler, MethodExceptionEventHandler, 47 LogSystem 48 { 49 50 private boolean logOutput = false; 51 private boolean exceptionSwitch = false; 52 53 public static void main( String args[] ) 54 { 55 EventExample ee = new EventExample(); 56 } 57 58 public EventExample() 59 { 60 try 61 { 62 66 67 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this ); 68 Velocity.init(); 69 } 70 catch(Exception e) 71 { 72 System.out.println("Problem initializing Velocity : " + e ); 73 return; 74 } 75 76 79 80 VelocityContext context = new VelocityContext(); 81 82 context.put("name", "Velocity"); 83 84 89 90 EventCartridge ec = new EventCartridge(); 91 ec.addEventHandler(this); 92 ec.attachToContext( context ); 93 94 try 95 { 96 102 103 System.out.println(""); 104 System.out.println("Velocity Event Handling Demo"); 105 System.out.println("============================"); 106 System.out.println(""); 107 108 String s = "The word 'Velocity' should be bounded by emoticons : $name."; 109 110 StringWriter w = new StringWriter (); 111 Velocity.evaluate( context, w, "mystring", s ); 112 113 System.out.println("Reference Insertion Test : "); 114 System.out.println(" " + w.toString()); 115 System.out.println(""); 116 117 121 122 s = "There is no reference $floobie, $nullvalue or anything in the brackets : >$!silentnull<"; 123 124 w = new StringWriter (); 125 Velocity.evaluate( context, w, "mystring", s ); 126 127 System.out.println("Reference Insertion Test with null references : "); 128 System.out.println(" " + w.toString()); 129 System.out.println(""); 130 131 136 137 logOutput = true; 138 139 s = "#set($settest = $NotAReference)"; 140 w = new StringWriter (); 141 142 System.out.println("NullSetEventHandler test : " ); 143 System.out.print(" There should be nothing between >"); 144 Velocity.evaluate( context, w, "mystring", s ); 145 System.out.println("< the brackets."); 146 System.out.println(""); 147 148 152 153 s = "#set($logthis = $NotAReference)"; 154 w = new StringWriter (); 155 156 System.out.println("NullSetEventHandler test : " ); 157 System.out.print(" There should be a log message between >"); 158 Velocity.evaluate( context, w, "mystring", s ); 159 System.out.println("< the brackets."); 160 System.out.println(""); 161 162 logOutput = false; 163 164 174 175 exceptionSwitch = true; 176 177 context.put("this", this ); 178 179 s = " $this.throwException()"; 180 w = new StringWriter (); 181 182 System.out.println("MethodExceptionEventHandler test : " ); 183 System.out.print(" This exception will be controlled and converted into a string : "); 184 Velocity.evaluate( context, w, "mystring", s ); 185 System.out.println(" " + w.toString()); 186 System.out.println(""); 187 188 193 194 exceptionSwitch = false; 195 196 s = " $this.throwException()"; 197 w = new StringWriter (); 198 199 System.out.println("MethodExceptionEventHandler test : " ); 200 System.out.println(" This exception will NOT be controlled. " 201 + " The next thing you should see is the catch() output "); 202 Velocity.evaluate( context, w, "mystring", s ); 203 System.out.println("If you see this, it didn't work!"); 204 205 } 206 catch( ParseErrorException pee ) 207 { 208 212 System.out.println("ParseErrorException : " + pee ); 213 } 214 catch( MethodInvocationException mee ) 215 { 216 223 System.out.println(" Catch Block : MethodInvocationException : " + mee ); 224 } 225 catch( Exception e ) 226 { 227 System.out.println("Exception : " + e ); 228 } 229 } 230 231 235 public void throwException() 236 throws Exception 237 { 238 throw new Exception ("Hello from throwException()"); 239 } 240 241 244 public Object referenceInsert( String reference, Object value ) 245 { 246 250 251 String s = null; 252 253 if( value != null ) 254 { 255 s = " ;) " + value.toString() + " :-)"; 256 } 257 else 258 { 259 263 if ( reference.equals("floobie") ) 264 { 265 s = "<no floobie value>"; 266 } 267 } 268 return s; 269 } 270 271 277 public boolean shouldLogOnNullSet( String lhs, String rhs ) 278 { 279 if (lhs.equals("$settest")) 280 return false; 281 282 return true; 283 } 284 285 public Object methodException( Class claz, String method, Exception e ) 286 throws Exception 287 { 288 291 292 if( exceptionSwitch && method.equals("throwException")) 293 { 294 return "Hello from the methodException() event handler method."; 295 } 296 297 throw e; 298 } 299 300 304 public void init( RuntimeServices rs ) 305 { 306 return; 307 } 308 309 313 public void logVelocityMessage(int level, String message) 314 { 315 if (logOutput) 316 { 317 System.out.print("Velocity Log : level : " + level + " msg : " + message); 318 } 319 } 320 321 } 322 | Popular Tags |