1 package org.apache.velocity.test.misc; 2 3 18 19 import java.io.BufferedWriter ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.Writer ; 24 import java.io.StringWriter ; 25 26 import java.util.ArrayList ; 27 import java.util.Hashtable ; 28 import java.util.HashMap ; 29 import java.util.Properties ; 30 import java.util.Stack ; 31 import java.util.Vector ; 32 import java.util.Enumeration ; 33 34 import org.apache.velocity.VelocityContext; 35 import org.apache.velocity.Template; 36 37 import org.apache.velocity.app.FieldMethodizer; 38 import org.apache.velocity.app.Velocity; 39 40 import org.apache.velocity.exception.ParseErrorException; 41 import org.apache.velocity.exception.ResourceNotFoundException; 42 import org.apache.velocity.exception.MethodInvocationException; 43 44 import org.apache.velocity.runtime.RuntimeSingleton; 45 import org.apache.velocity.test.provider.TestProvider; 46 47 import org.apache.velocity.app.event.EventCartridge; 48 import org.apache.velocity.app.event.ReferenceInsertionEventHandler; 49 import org.apache.velocity.app.event.MethodExceptionEventHandler; 50 import org.apache.velocity.app.event.NullSetEventHandler; 51 52 import org.apache.velocity.context.Context; 53 54 55 63 public class Test implements ReferenceInsertionEventHandler, 64 NullSetEventHandler, 65 MethodExceptionEventHandler 66 { 67 70 private static Stack writerStack = new Stack (); 71 72 public Test(String templateFile, String encoding) 73 { 74 Writer writer = null; 75 TestProvider provider = new TestProvider(); 76 ArrayList al = provider.getCustomers(); 77 Hashtable h = new Hashtable (); 78 79 82 83 h.put("Bar", "this is from a hashtable!"); 84 h.put("Foo", "this is from a hashtable too!"); 85 86 89 90 Vector v = new Vector (); 91 92 String str = "mystr"; 93 94 v.addElement( new String ("hello") ); 95 v.addElement( new String ("hello2") ); 96 v.addElement( str ); 97 98 try 99 { 100 104 105 Properties p = new Properties (); 106 107 110 111 try 112 { 113 FileInputStream fis = new FileInputStream ( 114 new File ("velocity.properties" )); 115 116 if( fis != null) 117 p.load( fis ); 118 } 119 catch (Exception ex) 120 { 121 122 } 123 124 127 128 for( Enumeration e = p.propertyNames(); e.hasMoreElements(); ) 129 { 130 String el = (String ) e.nextElement(); 131 132 Velocity.setProperty( el, p.getProperty( el ) ); 133 } 134 135 138 139 140 Velocity.setProperty(Velocity.RUNTIME_LOG_ERROR_STACKTRACE, "true"); 141 Velocity.setProperty(Velocity.RUNTIME_LOG_WARN_STACKTRACE, "true"); 142 Velocity.setProperty(Velocity.RUNTIME_LOG_INFO_STACKTRACE, "true"); 143 144 147 148 150 152 155 156 Velocity.init(); 157 158 161 162 if (templateFile == null) 163 { 164 templateFile = "examples/example.vm"; 165 } 166 167 168 Template template = null; 169 170 try 171 { 172 template = RuntimeSingleton.getTemplate(templateFile, encoding); 173 } 174 catch( ResourceNotFoundException rnfe ) 175 { 176 System.out.println("Test : RNFE : Cannot find template " + templateFile ); 177 } 178 catch( ParseErrorException pee ) 179 { 180 System.out.println("Test : Syntax error in template " + templateFile + ":" + pee ); 181 } 182 183 186 187 VelocityContext context = new VelocityContext(); 188 189 context.put("provider", provider); 190 context.put("name", "jason"); 191 context.put("providers", provider.getCustomers2()); 192 context.put("list", al); 193 context.put("hashtable", h); 194 context.put("search", provider.getSearch()); 195 context.put("relatedSearches", provider.getRelSearches()); 196 context.put("searchResults", provider.getRelSearches()); 197 context.put("menu", provider.getMenu()); 198 context.put("stringarray", provider.getArray()); 199 context.put("vector", v); 200 context.put("mystring", new String ()); 201 context.put("hashmap", new HashMap () ); 202 context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.RuntimeSingleton" )); 203 context.put("fmprov", new FieldMethodizer( provider )); 204 context.put("Floog", "floogie woogie"); 205 context.put("geirstring", str ); 206 context.put("mylong", new Long (5) ); 207 208 212 213 int intarr[] = { 10, 20, 30, 40, 50 }; 214 215 Object [] oarr = { "a","b","c","d" } ; 216 217 context.put( "collection", v ); 218 context.put("iterator", v.iterator()); 219 context.put("map", h ); 220 context.put("obarr", oarr ); 221 context.put("intarr", intarr ); 222 223 String stest = " My name is $name -> $Floog"; 224 StringWriter w = new StringWriter (); 225 228 w = new StringWriter (); 229 232 w = new StringWriter (); 233 236 237 240 241 EventCartridge ec = new EventCartridge(); 242 ec.addEventHandler(this); 243 ec.attachToContext( context ); 244 245 248 249 VelocityContext vc = new VelocityContext( context ); 250 251 if( template != null) 252 { 253 writer = new BufferedWriter (new OutputStreamWriter (System.out, encoding)); 254 template.merge( vc , writer); 255 writer.flush(); 256 writer.close(); 257 } 258 259 } 260 catch( MethodInvocationException mie ) 261 { 262 System.out.println("MIE : " + mie ); 263 } 264 catch( Exception e ) 265 { 266 RuntimeSingleton.error( "Test- exception : " + e); 267 e.printStackTrace(); 268 269 } 270 } 271 272 public Object referenceInsert( String reference, Object value ) 273 { 274 if (value != null) 275 ; return value; 277 } 278 279 public boolean shouldLogOnNullSet( String lhs, String rhs ) 280 { 281 283 if (lhs.equals("$woogie")) 284 return false; 285 286 return true; 287 } 288 289 public Object methodException( Class claz, String method, Exception e ) 290 throws Exception 291 { 292 if (method.equals("getThrow")) 293 return "I should have thrown"; 294 295 throw e; 296 } 297 298 299 public static void main(String [] args) 300 { 301 Test t; 302 303 String encoding = "ISO-8859-1"; 304 305 if( args.length > 1 ) 306 encoding = args[1]; 307 308 t = new Test(args[0], encoding); 309 } 310 } 311 312 313 314 315 316 317 318 | Popular Tags |