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.RuntimeSingleton; 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 71 public class Velocity implements RuntimeConstants 72 { 73 77 public static void init() 78 throws Exception 79 { 80 RuntimeSingleton.init(); 81 } 82 83 90 public static void init( String propsFilename ) 91 throws Exception 92 { 93 RuntimeSingleton.init(propsFilename); 94 } 95 96 103 public static void init( Properties p ) 104 throws Exception 105 { 106 RuntimeSingleton.init( p ); 107 } 108 109 115 public static void setProperty(String key, Object value) 116 { 117 RuntimeSingleton.setProperty(key,value); 118 } 119 120 126 public static void addProperty(String key, Object value) 127 { 128 RuntimeSingleton.addProperty(key,value); 129 } 130 131 136 public static void clearProperty(String key) 137 { 138 RuntimeSingleton.clearProperty(key); 139 } 140 141 152 public static void setConfiguration(Configuration configuration) 153 { 154 159 160 ExtendedProperties ep = configuration.getExtendedProperties(); 161 162 RuntimeSingleton.setConfiguration( ep ); 163 } 164 165 174 public static void setExtendedProperties( ExtendedProperties configuration) 175 { 176 RuntimeSingleton.setConfiguration( configuration ); 177 } 178 179 186 public static Object getProperty( String key ) 187 { 188 return RuntimeSingleton.getProperty( key ); 189 } 190 191 205 public static 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 static 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 = RuntimeSingleton.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 static 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 = RuntimeSingleton.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, RuntimeSingleton.getRuntimeServices() ); 305 } 306 catch( Exception e ) 307 { 308 RuntimeSingleton.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 343 public static boolean invokeVelocimacro( String vmName, String logTag, 344 String params[], Context context, 345 Writer writer ) 346 { 347 350 351 if ( vmName == null || params == null || context == null 352 || writer == null || logTag == null) 353 { 354 RuntimeSingleton.error( "Velocity.invokeVelocimacro() : invalid parameter"); 355 return false; 356 } 357 358 361 362 if (!RuntimeSingleton.isVelocimacro( vmName, logTag )) 363 { 364 RuntimeSingleton.error( "Velocity.invokeVelocimacro() : VM '"+ vmName 365 + "' not registered."); 366 return false; 367 } 368 369 372 373 StringBuffer construct = new StringBuffer ("#"); 374 375 construct.append( vmName ); 376 construct.append( "(" ); 377 378 for( int i = 0; i < params.length; i++) 379 { 380 construct.append( " $" ); 381 construct.append( params[i] ); 382 } 383 384 construct.append(" )"); 385 386 try 387 { 388 boolean retval = evaluate( context, writer, 389 logTag, construct.toString() ); 390 391 return retval; 392 } 393 catch( Exception e ) 394 { 395 RuntimeSingleton.error( "Velocity.invokeVelocimacro() : error " + e ); 396 } 397 398 return false; 399 } 400 401 414 public static boolean mergeTemplate( String templateName, 415 Context context, Writer writer ) 416 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception 417 { 418 return mergeTemplate( templateName, RuntimeSingleton.getString(INPUT_ENCODING,ENCODING_DEFAULT), 419 context, writer ); 420 } 421 422 435 public static boolean mergeTemplate( String templateName, String encoding, 436 Context context, Writer writer ) 437 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception 438 { 439 Template template = RuntimeSingleton.getTemplate(templateName, encoding); 440 441 if ( template == null ) 442 { 443 RuntimeSingleton.error("Velocity.parseTemplate() failed loading template '" 444 + templateName + "'" ); 445 return false; 446 } 447 else 448 { 449 template.merge(context, writer); 450 return true; 451 } 452 } 453 454 466 public static Template getTemplate(String name) 467 throws ResourceNotFoundException, ParseErrorException, Exception 468 { 469 return RuntimeSingleton.getTemplate( name ); 470 } 471 472 487 public static Template getTemplate(String name, String encoding) 488 throws ResourceNotFoundException, ParseErrorException, Exception 489 { 490 return RuntimeSingleton.getTemplate( name, encoding ); 491 } 492 493 507 public static boolean resourceExists(String resourceName) 508 { 509 return (RuntimeSingleton.getLoaderNameForResource(resourceName) != null); 510 } 511 512 517 public static void warn(Object message) 518 { 519 RuntimeSingleton.warn( message ); 520 } 521 522 527 public static void info(Object message) 528 { 529 RuntimeSingleton.info( message ); 530 } 531 532 537 public static void error(Object message) 538 { 539 RuntimeSingleton.error( message ); 540 } 541 542 547 public static void debug(Object message) 548 { 549 RuntimeSingleton.debug( message ); 550 } 551 552 573 public static void setApplicationAttribute( Object key, Object value ) 574 { 575 RuntimeSingleton.getRuntimeInstance().setApplicationAttribute( key, value); 576 } 577 578 582 public static boolean templateExists(String resourceName) 583 { 584 return resourceExists(resourceName); 585 } 586 } 587 | Popular Tags |