1 package org.apache.velocity.app; 2 3 18 19 import java.io.Writer ; 20 import java.util.Properties ; 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.BufferedReader ; 25 import java.io.StringReader ; 26 import java.io.InputStreamReader ; 27 import java.io.UnsupportedEncodingException ; 28 29 import org.apache.velocity.context.Context; 30 import org.apache.velocity.Template; 31 import org.apache.velocity.context.InternalContextAdapterImpl; 32 import org.apache.velocity.runtime.RuntimeInstance; 33 import org.apache.velocity.runtime.RuntimeConstants; 34 import org.apache.velocity.runtime.parser.node.SimpleNode; 35 import org.apache.velocity.runtime.configuration.Configuration; 36 37 import org.apache.velocity.exception.ResourceNotFoundException; 38 import org.apache.velocity.exception.ParseErrorException; 39 import org.apache.velocity.exception.MethodInvocationException; 40 41 import org.apache.velocity.runtime.parser.ParseException; 42 43 import org.apache.commons.collections.ExtendedProperties; 44 45 69 public class VelocityEngine implements RuntimeConstants 70 { 71 private RuntimeInstance ri = new RuntimeInstance(); 72 73 77 public void init() 78 throws Exception 79 { 80 ri.init(); 81 } 82 83 90 public void init( String propsFilename ) 91 throws Exception 92 { 93 ri.init(propsFilename); 94 } 95 96 103 public void init( Properties p ) 104 throws Exception 105 { 106 ri.init( p ); 107 } 108 109 115 public void setProperty(String key, Object value) 116 { 117 ri.setProperty(key,value); 118 } 119 120 126 public void addProperty(String key, Object value) 127 { 128 ri.addProperty(key,value); 129 } 130 131 136 public void clearProperty(String key) 137 { 138 ri.clearProperty(key); 139 } 140 141 152 public void setConfiguration(Configuration configuration) 153 { 154 159 160 ExtendedProperties ep = configuration.getExtendedProperties(); 161 162 ri.setConfiguration( ep ); 163 } 164 165 174 public void setExtendedProperties( ExtendedProperties configuration) 175 { 176 ri.setConfiguration( configuration ); 177 } 178 179 186 public Object getProperty( String key ) 187 { 188 return ri.getProperty( key ); 189 } 190 191 205 public boolean evaluate( Context context, Writer out, 206 String logTag, String instring ) 207 throws ParseErrorException, MethodInvocationException, 208 ResourceNotFoundException, IOException 209 { 210 return evaluate( context, out, logTag, new BufferedReader ( new StringReader ( instring )) ); 211 } 212 213 230 public boolean evaluate( Context context, Writer writer, 231 String logTag, InputStream instream ) 232 throws ParseErrorException, MethodInvocationException, 233 ResourceNotFoundException, IOException 234 { 235 238 239 BufferedReader br = null; 240 String encoding = null; 241 242 try 243 { 244 encoding = ri.getString(INPUT_ENCODING,ENCODING_DEFAULT); 245 br = new BufferedReader ( new InputStreamReader ( instream, encoding)); 246 } 247 catch( UnsupportedEncodingException uce ) 248 { 249 String msg = "Unsupported input encoding : " + encoding 250 + " for template " + logTag; 251 throw new ParseErrorException( msg ); 252 } 253 254 return evaluate( context, writer, logTag, br ); 255 } 256 257 273 public boolean evaluate( Context context, Writer writer, 274 String logTag, Reader reader ) 275 throws ParseErrorException, MethodInvocationException, 276 ResourceNotFoundException,IOException 277 { 278 SimpleNode nodeTree = null; 279 280 try 281 { 282 nodeTree = ri.parse( reader, logTag ); 283 } 284 catch ( ParseException pex ) 285 { 286 throw new ParseErrorException( pex.getMessage() ); 287 } 288 289 292 293 if (nodeTree != null) 294 { 295 InternalContextAdapterImpl ica = 296 new InternalContextAdapterImpl( context ); 297 298 ica.pushCurrentTemplateName( logTag ); 299 300 try 301 { 302 try 303 { 304 nodeTree.init( ica, ri ); 305 } 306 catch( Exception e ) 307 { 308 ri.error("Velocity.evaluate() : init exception for tag = " 309 + logTag + " : " + e ); 310 } 311 312 315 316 nodeTree.render( ica, writer ); 317 } 318 finally 319 { 320 ica.popCurrentTemplateName(); 321 } 322 323 return true; 324 } 325 326 return false; 327 } 328 329 330 344 public boolean invokeVelocimacro( String vmName, String logTag, 345 String params[], Context context, 346 Writer writer ) 347 throws Exception 348 { 349 352 353 if ( vmName == null || params == null || context == null 354 || writer == null || logTag == null) 355 { 356 ri.error( "VelocityEngine.invokeVelocimacro() : invalid parameter"); 357 return false; 358 } 359 360 363 364 if (!ri.isVelocimacro( vmName, logTag )) 365 { 366 ri.error( "VelocityEngine.invokeVelocimacro() : VM '"+ vmName 367 + "' not registered."); 368 return false; 369 } 370 371 374 375 StringBuffer construct = new StringBuffer ("#"); 376 377 construct.append( vmName ); 378 construct.append( "(" ); 379 380 for( int i = 0; i < params.length; i++) 381 { 382 construct.append( " $" ); 383 construct.append( params[i] ); 384 } 385 386 construct.append(" )"); 387 388 try 389 { 390 boolean retval = evaluate( context, writer, 391 logTag, construct.toString() ); 392 393 return retval; 394 } 395 catch( Exception e ) 396 { 397 ri.error( "VelocityEngine.invokeVelocimacro() : error " + e ); 398 throw e; 399 } 400 } 401 402 415 public boolean mergeTemplate( String templateName, 416 Context context, Writer writer ) 417 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception 418 { 419 return mergeTemplate( templateName, ri.getString(INPUT_ENCODING,ENCODING_DEFAULT), 420 context, writer ); 421 } 422 423 436 public boolean mergeTemplate( String templateName, String encoding, 437 Context context, Writer writer ) 438 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception 439 { 440 Template template = ri.getTemplate(templateName, encoding); 441 442 if ( template == null ) 443 { 444 ri.error("Velocity.parseTemplate() failed loading template '" 445 + templateName + "'" ); 446 return false; 447 } 448 else 449 { 450 template.merge(context, writer); 451 return true; 452 } 453 } 454 455 467 public Template getTemplate(String name) 468 throws ResourceNotFoundException, ParseErrorException, Exception 469 { 470 return ri.getTemplate( name ); 471 } 472 473 488 public Template getTemplate(String name, String encoding) 489 throws ResourceNotFoundException, ParseErrorException, Exception 490 { 491 return ri.getTemplate( name, encoding ); 492 } 493 494 509 public boolean templateExists( String templateName ) 510 { 511 return (ri.getLoaderNameForResource(templateName) != null); 512 } 513 514 519 public void warn(Object message) 520 { 521 ri.warn( message ); 522 } 523 524 529 public void info(Object message) 530 { 531 ri.info( message ); 532 } 533 534 539 public void error(Object message) 540 { 541 ri.error( message ); 542 } 543 544 549 public void debug(Object message) 550 { 551 ri.debug( message ); 552 } 553 554 575 public void setApplicationAttribute( Object key, Object value ) 576 { 577 ri.setApplicationAttribute( key, value ); 578 } 579 } 580 581 582 583 | Popular Tags |