1 package org.apache.velocity.app;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.StringReader;
28 import java.io.UnsupportedEncodingException;
29 import java.io.Writer;
30 import java.util.Properties;
31
32 import org.apache.commons.collections.ExtendedProperties;
33 import org.apache.velocity.Template;
34 import org.apache.velocity.context.Context;
35 import org.apache.velocity.context.InternalContextAdapterImpl;
36 import org.apache.velocity.exception.MethodInvocationException;
37 import org.apache.velocity.exception.ParseErrorException;
38 import org.apache.velocity.exception.ResourceNotFoundException;
39 import org.apache.velocity.exception.TemplateInitException;
40 import org.apache.velocity.runtime.RuntimeConstants;
41 import org.apache.velocity.runtime.RuntimeInstance;
42 import org.apache.velocity.runtime.log.Log;
43 import org.apache.velocity.runtime.parser.ParseException;
44 import org.apache.velocity.runtime.parser.node.SimpleNode;
45
46 /**
47 * <p>
48 * This class provides a separate new-able instance of the
49 * Velocity template engine. The alternative model for use
50 * is using the Velocity class which employs the singleton
51 * model.
52 * </p>
53 *
54 * <p>
55 * Please ensure that you call one of the init() variants.
56 * This is critical for proper behavior.
57 * </p>
58 *
59 * <p> Coming soon : Velocity will call
60 * the parameter-less init() at the first use of this class
61 * if the init() wasn't explicitly called. While this will
62 * ensure that Velocity functions, it almost certainly won't
63 * function in the way you intend, so please make sure to
64 * call init().
65 * </p>
66 *
67 * @version $Id: VelocityEngine.java 471381 2006-11-05 08:56:58Z wglass $
68 */
69 public class VelocityEngine implements RuntimeConstants
70 {
71 private RuntimeInstance ri = new RuntimeInstance();
72
73
74 /**
75 * Init-less CTOR
76 */
77 public VelocityEngine()
78 {
79 // do nothing
80 }
81
82 /**
83 * CTOR that invokes an init(String), initializing
84 * the engine using the properties file specified
85 *
86 * @param propsFilename name of properties file to init with
87 * @throws Exception
88 */
89 public VelocityEngine(String propsFilename)
90 throws Exception
91 {
92 ri.init(propsFilename);
93 }
94
95 /**
96 * CTOR that invokes an init(String), initializing
97 * the engine using the Properties specified
98 *
99 * @param p name of properties to init with
100 * @throws Exception
101 */
102 public VelocityEngine(Properties p)
103 throws Exception
104 {
105 ri.init(p);
106 }
107
108 /**
109 * initialize the Velocity runtime engine, using the default
110 * properties of the Velocity distribution
111 * @throws Exception
112 */
113 public void init()
114 throws Exception
115 {
116 ri.init();
117 }
118
119 /**
120 * initialize the Velocity runtime engine, using default properties
121 * plus the properties in the properties file passed in as the arg
122 *
123 * @param propsFilename file containing properties to use to initialize
124 * the Velocity runtime
125 * @throws Exception
126 */
127 public void init(String propsFilename)
128 throws Exception
129 {
130 ri.init(propsFilename);
131 }
132
133 /**
134 * initialize the Velocity runtime engine, using default properties
135 * plus the properties in the passed in java.util.Properties object
136 *
137 * @param p Proprties object containing initialization properties
138 * @throws Exception
139 *
140 */
141 public void init(Properties p)
142 throws Exception
143 {
144 ri.init(p);
145 }
146
147 /**
148 * Set a Velocity Runtime property.
149 *
150 * @param key
151 * @param value
152 */
153 public void setProperty(String key, Object value)
154 {
155 ri.setProperty(key,value);
156 }
157
158 /**
159 * Add a Velocity Runtime property.
160 *
161 * @param key
162 * @param value
163 */
164 public void addProperty(String key, Object value)
165 {
166 ri.addProperty(key,value);
167 }
168
169 /**
170 * Clear a Velocity Runtime property.
171 *
172 * @param key of property to clear
173 */
174 public void clearProperty(String key)
175 {
176 ri.clearProperty(key);
177 }
178
179 /**
180 * Set an entire configuration at once. This is
181 * useful in cases where the parent application uses
182 * the ExtendedProperties class and the velocity configuration
183 * is a subset of the parent application's configuration.
184 *
185 * @param configuration
186 *
187 */
188 public void setExtendedProperties( ExtendedProperties configuration)
189 {
190 ri.setConfiguration( configuration );
191 }
192
193 /**
194 * Get a Velocity Runtime property.
195 *
196 * @param key property to retrieve
197 * @return property value or null if the property
198 * not currently set
199 */
200 public Object getProperty( String key )
201 {
202 return ri.getProperty( key );
203 }
204
205 /**
206 * renders the input string using the context into the output writer.
207 * To be used when a template is dynamically constructed, or want to use
208 * Velocity as a token replacer.
209 *
210 * @param context context to use in rendering input string
211 * @param out Writer in which to render the output
212 * @param logTag string to be used as the template name for log
213 * messages in case of error
214 * @param instring input string containing the VTL to be rendered
215 *
216 * @return true if successful, false otherwise. If false, see
217 * Velocity runtime log
218 * @throws ParseErrorException
219 * @throws MethodInvocationException
220 * @throws ResourceNotFoundException
221 * @throws IOException
222 */
223 public boolean evaluate( Context context, Writer out,
224 String logTag, String instring )
225 throws ParseErrorException, MethodInvocationException,
226 ResourceNotFoundException, IOException
227 {
228 return evaluate( context, out, logTag, new BufferedReader( new StringReader( instring )) );
229 }
230
231 /**
232 * Renders the input stream using the context into the output writer.
233 * To be used when a template is dynamically constructed, or want to
234 * use Velocity as a token replacer.
235 *
236 * @param context context to use in rendering input string
237 * @param writer Writer in which to render the output
238 * @param logTag string to be used as the template name for log messages
239 * in case of error
240 * @param instream input stream containing the VTL to be rendered
241 *
242 * @return true if successful, false otherwise. If false, see
243 * Velocity runtime log
244 * @throws ParseErrorException
245 * @throws MethodInvocationException
246 * @throws ResourceNotFoundException
247 * @throws IOException
248 * @deprecated Use
249 * {@link #evaluate( Context context, Writer writer,
250 * String logTag, Reader reader ) }
251 */
252 public boolean evaluate( Context context, Writer writer,
253 String logTag, InputStream instream )
254 throws ParseErrorException, MethodInvocationException,
255 ResourceNotFoundException, IOException
256 {
257 /*
258 * first, parse - convert ParseException if thrown
259 */
260 BufferedReader br = null;
261 String encoding = null;
262
263 try
264 {
265 encoding = ri.getString(INPUT_ENCODING,ENCODING_DEFAULT);
266 br = new BufferedReader( new InputStreamReader( instream, encoding));
267 }
268 catch( UnsupportedEncodingException uce )
269 {
270 String msg = "Unsupported input encoding : " + encoding
271 + " for template " + logTag;
272 throw new ParseErrorException( msg );
273 }
274
275 return evaluate( context, writer, logTag, br );
276 }
277
278 /**
279 * Renders the input reader using the context into the output writer.
280 * To be used when a template is dynamically constructed, or want to
281 * use Velocity as a token replacer.
282 *
283 * @param context context to use in rendering input string
284 * @param writer Writer in which to render the output
285 * @param logTag string to be used as the template name for log messages
286 * in case of error
287 * @param reader Reader containing the VTL to be rendered
288 *
289 * @return true if successful, false otherwise. If false, see
290 * Velocity runtime log
291 * @throws ParseErrorException
292 * @throws MethodInvocationException
293 * @throws ResourceNotFoundException
294 * @throws IOException
295 *
296 * @since Velocity v1.1
297 */
298 public boolean evaluate(Context context, Writer writer,
299 String logTag, Reader reader)
300 throws ParseErrorException, MethodInvocationException,
301 ResourceNotFoundException,IOException
302 {
303 SimpleNode nodeTree = null;
304
305 try
306 {
307 nodeTree = ri.parse(reader, logTag);
308 }
309 catch (ParseException pex)
310 {
311 throw new ParseErrorException( pex );
312 }
313 catch (TemplateInitException pex)
314 {
315 throw new ParseErrorException( pex );
316 }
317
318 /*
319 * now we want to init and render
320 */
321
322 if (nodeTree != null)
323 {
324 InternalContextAdapterImpl ica =
325 new InternalContextAdapterImpl( context );
326
327 ica.pushCurrentTemplateName( logTag );
328
329 try
330 {
331 try
332 {
333 nodeTree.init( ica, ri );
334 }
335 catch (TemplateInitException pex)
336 {
337 throw new ParseErrorException( pex );
338 }
339 /**
340 * pass through application level runtime exceptions
341 */
342 catch( RuntimeException e )
343 {
344 throw e;
345 }
346 catch(Exception e)
347 {
348 getLog().error("Velocity.evaluate() : init exception for tag = "
349 + logTag, e);
350 }
351
352 /*
353 * now render, and let any exceptions fly
354 */
355
356 nodeTree.render( ica, writer );
357 }
358 finally
359 {
360 ica.popCurrentTemplateName();
361 }
362
363 return true;
364 }
365
366 return false;
367 }
368
369
370 /**
371 * Invokes a currently registered Velocimacro with the parms provided
372 * and places the rendered stream into the writer.
373 *
374 * Note : currently only accepts args to the VM if they are in the context.
375 *
376 * @param vmName name of Velocimacro to call
377 * @param logTag string to be used for template name in case of error
378 * @param params args used to invoke Velocimacro. In context key format :
379 * eg "foo","bar" (rather than "$foo","$bar")
380 * @param context Context object containing data/objects used for rendering.
381 * @param writer Writer for output stream
382 * @return true if Velocimacro exists and successfully invoked, false otherwise.
383 * @throws Exception
384 */
385 public boolean invokeVelocimacro( String vmName, String logTag,
386 String params[], Context context,
387 Writer writer )
388 throws Exception
389 {
390 /*
391 * check parms
392 */
393
394 if ( vmName == null || params == null || context == null
395 || writer == null || logTag == null)
396 {
397 getLog().error("VelocityEngine.invokeVelocimacro() : invalid parameter");
398 return false;
399 }
400
401 /*
402 * does the VM exist?
403 */
404
405 if (!ri.isVelocimacro( vmName, logTag ))
406 {
407 getLog().error("VelocityEngine.invokeVelocimacro() : VM '" + vmName
408 + "' not registered.");
409 return false;
410 }
411
412 /*
413 * now just create the VM call, and use evaluate
414 */
415
416 StringBuffer construct = new StringBuffer("#");
417
418 construct.append( vmName );
419 construct.append( "(" );
420
421 for( int i = 0; i < params.length; i++)
422 {
423 construct.append( " $" );
424 construct.append( params[i] );
425 }
426
427 construct.append(" )");
428
429 try
430 {
431 boolean retval = evaluate( context, writer,
432 logTag, construct.toString() );
433
434 return retval;
435 }
436 /**
437 * pass through application level runtime exceptions
438 */
439 catch( RuntimeException e )
440 {
441 throw e;
442 }
443 catch(Exception e)
444 {
445 getLog().error("VelocityEngine.invokeVelocimacro() : error ", e);
446 throw e;
447 }
448 }
449
450 /**
451 * Merges a template and puts the rendered stream into the writer.
452 * The default encoding that Velocity uses to read template files is defined in
453 * the property input.encoding and defaults to ISO-8859-1.
454 *
455 * @param templateName name of template to be used in merge
456 * @param context filled context to be used in merge
457 * @param writer writer to write template into
458 *
459 * @return true if successful, false otherwise. Errors
460 * logged to velocity log.
461 * @throws ResourceNotFoundException
462 * @throws ParseErrorException
463 * @throws MethodInvocationException
464 * @throws Exception
465 * * @deprecated Use
466 * {@link #mergeTemplate( String templateName, String encoding,
467 * Context context, Writer writer )}
468 */
469 public boolean mergeTemplate( String templateName,
470 Context context, Writer writer )
471 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
472 {
473 return mergeTemplate( templateName, ri.getString(INPUT_ENCODING,ENCODING_DEFAULT),
474 context, writer );
475 }
476
477 /**
478 * merges a template and puts the rendered stream into the writer
479 *
480 * @param templateName name of template to be used in merge
481 * @param encoding encoding used in template
482 * @param context filled context to be used in merge
483 * @param writer writer to write template into
484 *
485 * @return true if successful, false otherwise. Errors
486 * logged to velocity log
487 * @throws ResourceNotFoundException
488 * @throws ParseErrorException
489 * @throws MethodInvocationException
490 * @throws Exception
491 *
492 * @since Velocity v1.1
493 */
494 public boolean mergeTemplate( String templateName, String encoding,
495 Context context, Writer writer )
496 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
497 {
498 Template template = ri.getTemplate(templateName, encoding);
499
500 if ( template == null )
501 {
502 getLog().error("Velocity.mergeTemplate() was unable to load template '"
503 + templateName + "'");
504 return false;
505 }
506 else
507 {
508 template.merge(context, writer);
509 return true;
510 }
511 }
512
513 /**
514 * Returns a <code>Template</code> from the Velocity
515 * resource management system.
516 *
517 * @param name The file name of the desired template.
518 * @return The template.
519 * @throws ResourceNotFoundException if template not found
520 * from any available source.
521 * @throws ParseErrorException if template cannot be parsed due
522 * to syntax (or other) error.
523 * @throws Exception if an error occurs in template initialization
524 */
525 public Template getTemplate(String name)
526 throws ResourceNotFoundException, ParseErrorException, Exception
527 {
528 return ri.getTemplate( name );
529 }
530
531 /**
532 * Returns a <code>Template</code> from the Velocity
533 * resource management system.
534 *
535 * @param name The file name of the desired template.
536 * @param encoding The character encoding to use for the template.
537 * @return The template.
538 * @throws ResourceNotFoundException if template not found
539 * from any available source.
540 * @throws ParseErrorException if template cannot be parsed due
541 * to syntax (or other) error.
542 * @throws Exception if an error occurs in template initialization
543 *
544 * @since Velocity v1.1
545 */
546 public Template getTemplate(String name, String encoding)
547 throws ResourceNotFoundException, ParseErrorException, Exception
548 {
549 return ri.getTemplate( name, encoding );
550 }
551
552 /**
553 * Determines if a resource is accessable via the currently
554 * configured resource loaders.
555 * <br><br>
556 * Note that the current implementation will <b>not</b>
557 * change the state of the system in any real way - so this
558 * cannot be used to pre-load the resource cache, as the
559 * previous implementation did as a side-effect.
560 * <br><br>
561 * The previous implementation exhibited extreme lazyness and
562 * sloth, and the author has been flogged.
563 *
564 * @param resourceName name of the resource to search for
565 * @return true if found, false otherwise
566 */
567 public boolean resourceExists(String resourceName)
568 {
569 return (ri.getLoaderNameForResource(resourceName) != null);
570 }
571
572 /**
573 * @param resourceName
574 * @return True if the template exists.
575 * @see #resourceExists(String)
576 * @deprecated Use resourceExists(String) instead.
577 */
578 public boolean templateExists(String resourceName)
579 {
580 return resourceExists(resourceName);
581 }
582
583
584 /**
585 * Returns a convenient Log instance that wraps the current LogChute.
586 * Use this to log error messages. It has the usual methods you'd expect.
587 * @return A log object.
588 */
589 public Log getLog()
590 {
591 return ri.getLog();
592 }
593
594 /**
595 * @param message
596 * @deprecated Use getLog() and call warn() on it.
597 */
598 public void warn(Object message)
599 {
600 getLog().warn(message);
601 }
602
603 /**
604 * @param message
605 * @deprecated Use getLog() and call warn() on it.
606 */
607 public void info(Object message)
608 {
609 getLog().info(message);
610 }
611
612 /**
613 * @param message
614 * @deprecated Use getLog() and call warn() on it.
615 */
616 public void error(Object message)
617 {
618 getLog().error(message);
619 }
620
621 /**
622 * @param message
623 * @deprecated Use getLog() and call warn() on it.
624 */
625 public void debug(Object message)
626 {
627 getLog().debug(message);
628 }
629
630 /**
631 * <p>
632 * Sets an application attribute (which can be any Object) that will be
633 * accessible from any component of the system that gets a
634 * RuntimeServices. This allows communication between the application
635 * environment and custom pluggable components of the Velocity engine,
636 * such as ResourceLoaders and LogChutes.
637 * </p>
638 *
639 * <p>
640 * Note that there is no enforcement or rules for the key
641 * used - it is up to the application developer. However, to
642 * help make the intermixing of components possible, using
643 * the target Class name (e.g. com.foo.bar ) as the key
644 * might help avoid collision.
645 * </p>
646 *
647 * @param key object 'name' under which the object is stored
648 * @param value object to store under this key
649 */
650 public void setApplicationAttribute( Object key, Object value )
651 {
652 ri.setApplicationAttribute(key, value);
653 }
654
655 /**
656 * <p>
657 * Return an application attribute (which can be any Object)
658 * that was set by the application in order to be accessible from
659 * any component of the system that gets a RuntimeServices.
660 * This allows communication between the application
661 * environment and custom pluggable components of the
662 * Velocity engine, such as ResourceLoaders and LogChutes.
663 * </p>
664 *
665 * @param key object 'name' under which the object is stored
666 * @return value object to store under this key
667 */
668 public Object getApplicationAttribute( Object key )
669 {
670 return ri.getApplicationAttribute(key);
671 }
672
673 }