APPENDIX Velocity Reference

VELOCITY HAS A WIDE RANGE of configuration settings, directives, and resource loaders. This appendix is a reference for aU aspects of Velocity configuration and usage.

Introducing the Directives

The following sections show the syntax for the standard directives included in the Velocity distribution.

#set

The #set directive assigns variables' values. These values can be a simple literal value or another variable.

Syntax

#set($variable= I

Example

#set($myVar = "Hello World")

Output

The #set directive has no output.

347 Appendix #if/#else/#end

The #if/#else/#end directives support conditional processing within your tem• plates. The use of these directives is synonymous with the use of the if/else construct in Java.

Syntax

#if«boolean expression> I I

Example

#set($someVar = true) #if($someVar) Hello World! #else Goodbye Cruel World! #end

Output

Hello World!

#foreach

The #foreach directive iterates over all elements in a collection or an array and performs some processing for each element.

Syntax

#foreach($var in $collectionOrArray) ## do something with $var #end

348 Velocity Reference

Example

#foreach($num in [0 •• 5]) $num #end

Output o 1 2 3 4 5

#include

The #include directive includes some content from another resource in the out• put stream. The content is added as-is with no processing by the Velocity engine.

Syntax

#import«resource_name»

Example

For example, the main template, template. vm, could contain the followingVfL code:

Hello #include("world.txt")

And an external file resource, world. txt, could contain the following static text:

World

Output

Hello World

349 Appendix #parse

The #parse directive functions in a similar manner to the #include directive; however, the external resource is first parsed by the Velocity engine, and any VTL constructs are processed as if they were part of the main template.

Syntax

#parse«resource_name»

Example

For example, the main template, template. vm, could contain the following VTL code:

#set ($who = "World") Hello #parse("world.vm")

And an external template, world.vm, could contain the additionalVTL code:

$who

Output

Hello World

Configuring Velocity

Velocity has a selection of configuration parameters that can control various aspects of the runtime. The following sections provide a quick reference to these configuration parameters.

General Configuration Options

Table A-1 shows the general configuration options.

350 Velocity Reference

TableA-l. General Configuration Options

Parameter Name Description Default Value runtime. log Specifies the name of the log file cre• velocity. log ated by the Velocity log system. File• names that aren't absolute are resolved relative to the current directory of your application. runtime. log. logsystem.class Specifies the class name of the org.apache. LogSystem implementation. Use this velocity. runtime. if you want to use a specific LogSystem log. AvalonLogSystem implementation. runtime. log. error. stacktrace Specifies whether Velocity should out• false put a stack trace for error-level log messages. runtime. log. warn. stacktrace Specifies whether Velocity should out• false put a stack trace for warn-level log messages. runtime. log. info. stacktrace Specifies whether Velocity should out• false put a stack trace for info-level log messages. runtime. log. invalid. Specifies whether a log message true references should be written when an invalid ref- erence is encountered. input. encoding Specifies the encoding to use when ISO-8859-1 reading resource files. output. encoding Specifies the encoding to use when ISO-8859-1 creating output. parser.pool.size Specifies the size of the parser pool. 20 runtime. interpolate. string. Specifies whether string literals should true literals be interpolated by the runtime. runtime.introspector. Specifies the Uberspect implementa• org.apache. uberspect tion class to use. If you choose to velocity. uti!. create your own Uberspector, then you introspection. can replace the default value of this UberspectImpl configuration parameter.

351 Appendix Directive Configuration

Table A-2 shows the directive configuration options.

TableA-2. Directive Configuration Options Parameter Name Description Default Value directive.foreach.counter. Specifies the name of the counter velocityCount name variable used by the #foreach directive. directive.foreach.counter. Set sthe initial value of the counter 1 initial. value variable used by the #foreach directive. directive.include.output. Specifies the token to output before an <1-- include error errormsg. start error message when writing error mes• sages to the output stream. The end token must also be specified for this to take effect. directive.include.output. Specifies the token to output after an see error log --> errormsg.end error message. directive.parse.maxdepth Specifies the maximum parse depth 10 when using the #parse directive. This is relevant only if you use embed tem• plates using #parse that have #parse statements themselves.

ResourceManager Configuration

Table A-3 shows the ResourceManager configuration options.

Table A-3. ResourceManager Configuration Options Parameter Name Description Default Value resource.manager.logwhenfound Specifies whether a log message should true be written when a resource is located by the resource manager. resource. loader A comma-separated list of loader file names. These names correspond to sep• arate loader configurations for each name specified. resource. manager. cache. class Specifies the class implementing the re• org.apache. source manager cache. You can provide velocity. runtime. your own caching implementation using resource. this parameter. ResourceCachelmpl

352 Velocity Reference ResourceLoader Configuration

An important part of configuring your Velocity system is configuring the resource loaders used by your application. The following sections are a quick reference to the configuration parameters for the different resource loaders. For all the configuration parameters, you should replace the variable with the name of the resource loader you want to configure.

Common Configuration

Some of the parameters are considered common to all resource loaders; Table A-4 shows these.

Table A-4. Common ResourceLoader Configuration Options Parameter Name Description Default Value .resource.loader.class The name of the class that implements org.apache.velocity. the resource loader. runtime. resource. loader. FileResourceLoader . resource. loader. cache Specifies whether caching is enabled false for the resource loader.

.resource.loader. Specifies the duration in seconds to wait 2 modificationChecklnterval before checking to see if a cached resource has been modified. Set this to o to prevent checks.

FileResourceLoader

Table A-5 shows the FileResourceLoader-specific configuration options.

TableA-5. FileResourceLoader-Specijic Configuration Options Parameter Name Description Default Value .resource.loader.path The path to search for. This should be a . / directory or list of directories separated by commas. The template name should match a file within one of the specified directories.

353 Appendix

JarResourceLoader

Table A-6 shows the JarResourceLoader-specific configuration options.

Table A-6. JarResourceLoader-Specijic Configuration Options

Parameter Name Description Default Value .resource. loader. path The path to the JAR file (or JAR files) that the runtime loader should search for re- sources in.

ClasspathResourceLoader

The ClasspathResourceLoader has no configuration parameters other than the standard ones.

DataSourceResourceLoader

Table A-7 shows the DataSourceResourceLoader-specific configuration options.

TableA-7. DataSourceResourceLoader-Specific Configuration Options Parameter Name Description . resource .loader. resource. datasource The JNDI name of the DataSource to use to retrieve the resources. . resource. loader. resource. table The name of the table in which the resources are stored. .resource.loader.resource.keycolumn The name of the column that stores the resource name. .resource.loader.resource. The name of the column that stores the actual templatecolumn resource content. .resource.loader.resource. The name of the column that has the time timestamp column stamp for the resource. This is required for caching support to function correctly.

354 Velocity Reference Velocimacro Configuration

Table A-8 shows the Velocimacro configuration parameters.

Table A -8. Velocimacro Configuration Parameters

Parameter Name Description Default Value velocimacro.library Specifies the name of one or more files con - VM _global_ taining global macro definitions. If you library. vm want to use more than one global library file, then split the filenames with a comma. velocimacro.permissions. Specifies whether macros can be defined true allow. inline in line in a template as opposed to in the global library. velocimacro.permissions.allow. Specifies whether an inline macro can re• false inline.to.replace.global place a globally defined macro with the same name. velocimacro.permissions.allow. This parameter specifies whether inline false inline.local.scope macros should be scoped to the local tem- plate only. See Chapter 3 for more details on this parameter. velocimacro. context . locals cope Specifies whether a reference access from false within a macro can change the context. velocimacro . library . autoreload Specifies whether Velocity should check false the globally defined library for changes and reload the library as appropriate. This works only when caching is disabled on the resource loader.

355 Index

Symbols addFieldLabelsO method, calling in e-mail newsletter application, 130 ! (NOT operator), example of, 60 addFromAddressFieldO method, calling ## characters, using with comments, 43 in e-mail newsletter application, $ (dollar symbol) and variable names, 130 outputting, 50-53 addProgressBarO method, calling in % (modulo operator), purpose of, 53 e-mail newsletter application, 133 \ (backslash) AddressException, catch block for, 141 displaying, 51 addSendButtonO method, calling in escaping quotes with, 50 e-mail newsletter application, 133 , specifying view path as, addSubjectFieldO method, calling in 240 .. (double quotes), embedding in strings, e-mail newsletter application, 130 AddToCartAction class, using with 49-50 • (single quotes) Velocity and Struts, 200-202 Anakia embedding in strings, 49-50 creating Hello World with, 216-219 using, 91 features of, 215-216 purpose of, 6 A sample output from, 233 Anakia context data, overview of, abstraction layer 225-229 core goal of, 105 Anakia projects, documenting, 229-233 creating client application for, 115 task tag, example of, 226-227 framework for, 106-107 AnakiaTask tag attributes, list of, using, 110-115 224-225 AbstractNewsletterTemplate class, using Ant. See also Texen with e-mail newsletter application, downloading, 216 143-146 installing, 9-10 AbstractSearchTool, using with resource for, 216 VelocityViewServlet, 167 using with DVSL transformations, AbstractVelocityContentTemplate 258-259 in abstraction layer, 108-109 verifying, 10 implementing, 112 . Ant build script using with e-mail newsletter apphca- for Anakia, 217-218 tion, 142-143, 146 downloading, 12 ActionUstener interface, implementing for Texen, 246 in e-mail newsletter application, . See Ant 129, 134-135 Apache CVS server, logging into, 235 ActionMessagesTool, description of, 206 , downloading, 161 actionPerformedO method, role in API (application programming e-mail newsletter application, 135 interface), disadvantage of, 106, 115 addActionUstenerO method, calling in applications, building with abstraction e-mail newsletter application, 133 layer, 110 addCategoryContentsLabelO method, applyTemplatesO methods, using with calling in e-mail newsletter TransformTool interface, 256 application, 132 Apress Mailer application. See also addCategoryContentsTextBoxO method, e-mail newsletter application calling in e-mail newsletter interface for, 118-119 application, 132 .. running, 151-155 addCategoryUstO method, callmg m arithmetic, performing, 53 e-mail newsletter application, 130-131

357 Index

array variables, declaring with #set casing, significance of, 46 directive, 61 Category class assembleResourceLoaderInitializersO role in domain object model of method, effect of, 281-282 e-mail newsletter application, AST (Abstract Syntax Tree) 122-124 and initDocumentO method, for Widgetarium database, 178-179 305-307 checked exceptions, overuse and significance of, 268 effective use of, 89-90 storing root node of, 308 child template, relationship to #parse ASTprocess class, significance of, 305, directive, 56-57 308 childrenO method of DVSLNode ASTText.initO method, code for, 309 interface, description of, 257 ASTText.renderO method, code for, 309 circular references, relationship to $attrib object, using with DVSL, 257-258 #parse directive, 57-58 attribO method of DVSLNode interface, classes directory, placing templates in, description of, 257 161 Avalon LogKit, significance of, 22-23 ClasspathResourceLoader AvalonLogSystem, significance of, benefits of, 90-91, 323 277-279 configuring, 354 recommendation of, 34 and refreshResourceO method, 300 B testing, 31-32 backslash (\) using, 323 displaying, 51 using with VelocityServlet, 160 escaping quotes with, 50 client application, creating for basedir abstraction layer, 115 AnakiaTask tag attribute, 224 closeSessionO method, using with DVSLTask property, 260 Widgetarium database, 183-184 BeanTool class, using with DVSL, 263 Collections, using #foreach directive Boolean literals, example of, 48 with,60-64 Boolean variables, using with comments, including, 43-44 comparisons, 60 Commons Logging tool, using, 24-25 BUILD FAILED message, comparison operators, support for, 60 troubleshooting, 10 complex processing, avoiding, 93 business tier, creating for Widgetarium conditional blocks, support for, 58 database, 186-188 configuration parameters for ClasspathResourceLoader, 31-32 declaring for JarResourceLoader, C 29-30 CachingClasspathResourceLoader class. setting for Velocity runtime, 19-20 See also ResourceLoader class using with Velocimacro libraries, configuring, 328-329 69-71 enabling caching for, 324 configureO method, using with overview of, 326-328 ConsoleLogSystem class, 321 testing, 329-330 ConsoleLogSystem class Cart class configuring, 323 creating for Widgetarium database, overview of, 320-322 189-190 content template, creating with Texen, using handleShowCartO method 252 with,209-210 ContentResource.processO method, Cart template, creating for Widgetarium code for, 301-302 database, 195-196 ContentTemplate interface CartItem class, creating for Widgetarium in abstraction layer, 107-108 database, 188-189 for e-mail newsletter application, 142 carts ContentType property, role in addingnemsto, 200-202 Subscriber class, 121-122 remOving nems from, 202-203

358 Index

context D merging with templates, 268 DAO (Data Access Object), interface for versus Model, 101-104 176 ' populating, 84-86 using instead ofVelocityContext, DAOFactory class, using with 82-83 Widgetarium database, 186 using Texen to load data into, data access layer, creating for 247-248 Widgetarium database, 175-186 context chaining, using, 17-18 data, accessing within templates, 44-45 context objects, using with DVSL, DataAccessException class, creating for 255-258 Widgetarium database, 181 $context.applyTemplatesO method databases, building for Web using with DVSL, 262 ' applications,173-175 ContextFactory class, creating, 83-84 DataSourceResourceLoader class ContextLoaderServlet, using with configuring, 32-33, 354 Velocity and Spring, 213 disadvantages of, 90 contextProperties property for purpose of, 10-11 TexenTask, description of, 244 data. script, saving, 175 ContextUtils class, code for, 85 $date variable, description of, 226 control template, creating with Texen DateTool, formatting dates with 73-75 251-252 ' design patterns, resource for, 176 Controller in MVC, significance of, destdir AnakiaTask tag attribute, 94-95 description of, 224 controlTemplate property for destdir property of DVSLTask, TexenTask, description of, 244 description of, 260 CookieTool, using with development tools VelocityViewServlet, 167-168 Emacs, 345 copyO method IntelliJ IDEA, 342 of DVSLNode interface, descriptions ]Edit, 342-343 of,257 TextPad,344 using with TransformTool interface UltraEdit, 343-344 256 ' VeloEdit for Eclipse, 340-342 createAndShowGUIO method, role in e• VeloGUI, 339-340 mail newsletter application, 127 Directive class, methods of 331 createContextO hook method, using directives. See also initializ~DirectivesO with VelocityServlet, 163 method createLogSystemO method, effect of, configuring, 352 276-278 creating and customizing, 330-337 createNewParserO method, code for #else, 348 287 ' #end,348 custom tools, building, 171-172. See also #foreach,60-64,77,348-349 tools #foreach and Anakia, 223 #if,348 CVS clients, downloading, 235 CVS repository #if, #else, and #elseif, 58--60 accessing Live Template for Intelli] #include, 54-55, 349 IDEA in, 342 #macro, 64--66 accessing syntax definitions for overview of, 54 UltraEdit in, 343 #parse, 55-58,350 accessing syntax highlighting defini• #set,61,319,347 tions for JEdit in, 343 DispatcherSerlet, using with Velocity accessing syntax highlighting for and Spring, 214 Emacs in, 345 dollar symbol ($) and variable names outputting, 50-53 ' obtaining DVSL code from, 254 Web address for, 342-343 DOM API, using with Anakia, 221 dom4~ distribution, downloading, 254 domam object model and e-I?ail newsletter application, 119 Subscnber class in, 120-122

359 Index

double quotes ("), embedding in strings, examples, compiling and running, 12 49-50 exception handling Driver application, code for, 42 overview of, 89-90 DVSL (Declarative Velocity Style simplifying with abstraction layer, Language) 105 creating Hello World with, 254-255 excludes AnakiaTask tag attribute, features of, 253, 266 description of, 225 modifying Hello World application excludes property of DVSLTask, with, 258-259 description of, 260 obtaining and building, 254 ExtendedProperties class, significance using default context objects with, of, 272-273, 277 255-258 ExtendedProperties.combineO method, using Toolbox with, 258-259 effect of, 275 DVSL example, building on, 261-266 extension AnakiaTask tag attribute, DVSLNode interface, methods of, 257 description of, 224 DVSLTask, properties of, 260 F E FileResourceLoader Eclipse development tool, using, benefits of, 90-91 340-342 configuring, 353 else block, example of, 281 example of, 270 #else directive initialization process for, 289-291 making decisions with, 58-60 recommendation of, 34 syntax of, 348 and refreshResourceO method, 301 #elseif directive, making decisions with, using, 323 59-60 FileResourceLoader.initO method, code Emacs, using Velocity in, 345 for, 290-291 e-mail newsletter application. See also $files context object for TexenTask, Apress Mailer application description of, 245 and domain object model, 119-126 $firstName reference, resolving, 18 features of, 117-118 FlashNewsletterTemplate class, role in generating content for, 141-151 e-mail newsletter application, 147 running, 151-155 floating-point arithmetic, performing sending newsletters from, 137-141 with MathTool, 75-77 user interface for, 126-137 Foo class, code for, 239 and Velocity templates, 147-151 FooServlet class, creating with Veltag, #end directive, syntax of, 348 238-241 Enhydra XMLC template engine, force property of DVSLTask, description description of, 3 of,260 errorO hook method, using with #foreach directive VelocityServlet, 163 drawback of, 77 error messages, generating for #include syntax of, 348-349 directives, 55 using with Anakia, 223 ErrorsTool, description of, 206 using with Collections, 60-64 $escape variable, description of, 226 #foreach loop, example of, 64 evaluateO method, appropriate use of, 91 formal references, explanation of, 50 event handlers Format class, relationship to Subscriber building, 315-316 class, 121-122 overviewof,319-320 FormTool, description of, 206 registering, 315 EventCartridge class, registering event handlers with, 315 G EventHandler constructor $generator context object for TexenTask, registering event handlers with, 315 description of, 245 testing, 317-319 $generator.parseO method, using with events, handling, 314-315 Texen, 247

360 Index

Gentleware Poseidon, description of, 7 H getO methods using with DVSLNode interface 257 handleRequestO method, implementing using with Product class, 62-63' with VelocityServlet, 159 getAllProductsO method, using with handleShowCartO method, example of 209-210 ' Widgetarium database, 183-184 getAppValueO method, using with HashMap ThansformTool interface, 256 storing CartItems in, 191 getCatetoryListO method, relationship using with Spring Framework, 210 to Category class, 124 Hello World application getChildrenO method, using with creating, 13-14 Anakia, 223 creating with Anakia, 216-219 getContentO method, significance of creating with DVSL, 254-255 226 ' creating with Texen, 245-247 getInstanceO method of ContextFactory crea~g with VelocityServlet, 158-162 class, calling, 84 creating with VelocityViewServlet, getLastModifiedO method, using with 164-166 CachingClasspathResourceLoader creating with Veltag, 236-237 class, 327-328 dissecting, 14-15 getLevelFromStringO method, using improving, 15-16 with ConsoleLogSystem class, 321 improving with Texen, 247-248 322 ' modifying with DVSL, 258-259 getLevelNameO method, using with using JAXP with, 220-221 ConsoleLogSystem class, 322 HelloDirective class getMailSessionO method, role in e-mail configuring, 333 newsletter application, 141 overview of, 331-332 getModelO method testing, 332-333 role in e-mail newsletter application helloWorldControl.vm control template 144 ' using with Texen, 246 ' using with Spring Framework, 210 Hibernate getNameO method of Directive class config file for, 180-181 implementing,331-332 ' example of, 176-178 getNewsletterTemplateO method role features of, 173 in e-mail newsletter application, Hibernate sessions, advisory about 183 147 HQL ~Hi?ernate Query Language), ' getProductO method, using with sigruficance of, 184 Widgetarium database, 184 HTML e-mail, example of, 155 HTML templates getPro,ductsInCategoryO method, using With Widgetarium database, 184 creating for e-mail newsletter getResourceO method, using, 294, 296 application, 149-151 getResourceStreamO method, using with using with Anakia, 231 CachingClasspathResourceLoader HttpServletRequest and class, 327 HttpServletResponse, retrieving, 158 getSessionO method, using with Widgetarium database, 183-184 I getTemplateO method example of, 38 IDEA using, 294 description of, 7 using with VelocityServlet, 159-160 using Velocity in, 342 get'IYPeO method of Directive class #if directive implementing,331-332 ' making decisions with, 58-59 getURLO method, using with syntax of, 348 CachingClasspathResourceLoader ImageDirective class class, 328 configuring and testing, 337 GridBagConstraints class, role in e-mail overview of, 333-336 newsletter application, 130 ~plementations, creating, 112-115 tag, using with DVSL, 261

361 Index

ImportTool, using with introspection process, overview of, VelocityViewServlet, 167-169 310-311 in property of DVSI1'ask, description of, IoC (Inversion of Control), using with 260 Spring Framework, 210 #include directive isSourceModifiedO method, effect of, versus #parse, 92-93 300-301,327-328 versus #parse directive, 58 IteratorTool, performing advanced syntax of, 349 looping with, 77-79 using static content with, 54-55 includes AnakiaTask tag attribute, description of, 225 J includes property of DVSLTask, J2EE environment, building with description of, 260 support for, 10-11 infoO method, result of calls to, 273 JAF (JavaBeans Activation Framework), initO method downloading, 137 in ASTText node, 309 Jakarta Commons, Web address for, 11, role in initialization phase, 273 24 using in Hello World application, 14 Jamon template engine, description of, using with ConsoleLogSystem class, 2 320-321 JAR files using with FileResourceLoader.initO and Avalon LogKit, 22-23 method,291 building for VelocityTools project, 12 using with VelocityEngine class, 38 location of, 10 initCapO method of BeanTool class, placing in lib directory, 218 using with DVSL, 263 types of, 11 initDocumentO method JarResourceLoader example of, 304 advisory about, 34 overview of, 305-307 configuring, 354 initialization phase disadvantages of, 90 dynamics of, 268 and refreshResourceO method, 300 overview of, 270-274, 294-296 testing, 29-30 testing performance of, 291-293 Java class for Hello World, examining, 14 initialization process for Java Collection interfaces, using with FileResourceLoader, overview of, JDOM,222 289-291 Java, using JDOM from, 223-224 initializeDirectivesO method, overview JavaBean properties, using, 45-46 of, 284-286. See also directives JavaBeans initializeIntrospectionO method, declaring for Subscriber class, 121 overviewof,287-289 leveraging with MVC pattern, 104 initializeLoggerO method, overview of, relationship to context, 84 275-279 using Veltag with, 240 initializeParserPoolO method, overview using with DVSL, 261 of,286-287 JavaCC, relationship to Velocity parser, initializePropertiesO method, overview 305 of,274-275 JavaMail,downloading, 137 initializeResourceManagerO method, JAXP (Java API for XML Processing), overview of, 280-284 using with Hello World, 220-221 input.encoding configuration JBYTE template engine, description of, 2 parameter, description of, 21, 351 ]DOM inputEncoding property for TexenTask, downloading, 216 description of, 244 resource for, 224 IntelliJ IDEA using, 219-224 description of, 7 JEdit, using Velocity in, 342-343 using Velocity in, 342 JFrame instance, creating for e-mail InternalContextAdapterImpl class newsletter application, 127 instance, creating, 306 JJTree, relationship to Velocity parser, 305

362 Index

JPanel class, extending in e-mail MailerPanel class, building for e-mail newsletter application, 129 newsletter application, 128 JSP (Java Server Pages), resource for, 236 MailerPanel constructor, calling for e• mail newsletter application, 129 mainO method, role in e-mail L newsletter application, 127 lastModifiedCheck AnakiaTask tag #makeDoc macro, using with Anakia, attribute, description of, 225 232-233 Legolas value, displaying in #makeHeader macro, using with Anakia, HelloWorldServlet,162 231-232 lib directory, placing JAR files in, 218 #makeMenu macro, using with Anakia, LinkTool, using with 232 VelocityViewServlet, 167, 169 #make Properties macro, using with Live Template for IntelliJ IDEA, Anakia,232 accessing, 342 mapping definitions, creating with 10adConfigurationO hook method, Hibernate, 179-180 using with VelocityServlet, 163 Maps, storing properties in, 46-47 10adDataO method, calling in e-mail #match directive, using with DVSL, 255, newsletter application, 129 261-263 10adDirectiveO method, code for, MathTool 285-286 performing floating-point arithmetic 10adPeopleO method, using with Texen, with, 75-77 251 using with MVC pattern, 99 10adResourceO method, overview of, mergeO method, code for, 307-308 297-299 merge phase of content creation logO method, role in initialization process, overview of, 307-311 phase, 273 mergeTemplateO method 10g4j, configuring Velocity for use of, significance of, 15 22-24 using with VelocityEngine class, 38 10gFile property of DVSLTask, MessageTool, description of, 206 description of, 260 MethodExceptionEventHandler, logging configuration parameters, list implementing, 315-316 of, 25 MethodExceptionEventHandler LogSystem interface, purpose of, 314 configuring, 279 methods, using, 45-53 creating and customizing, 320-323 Middlegen tool, features of, 8 features of, 22-23 MIME type, relationship to Subscriber locating, 278 class, 121-122 overview of, 323 MimeMessage instance, creating for e• 10gVeiocityMessageO method, using mail newsletter application, 140 with ConsoleLogSystem class, 320, ModelinMVC 322 versus context, 101-104 looping, performing with IteratorTool, guidelines for building of, 97, 101 77-79 populating, 97-98 Luxor tool, features of, 8 significance of, 94-95 Model objects, using, 104 ModelBeans M in abstraction layer, 107 macro calls, using with HTML templates implementing, 113 in Anakia, 231 modulo operator (%), purpose of, 53 macro libraries. See Velocimacros child, using with element macros. See also Velocimacros and Anakia, 217 adding to macro libraries, 70-71 MVC (Model-View-Controller) pattern overview of, 64-65 appropriate use of, 97-101 using, 65-66 overview of, 94-97, 104-105 Mailer class, creating for e-mail myAttrib attribute, accessing value of, newsletter application, 126-127 257-258 MySQL, downloading, 173

363 Index

N P nameO method of DVSLNode interface, pl-p3 parameters, role in description of, 257 ParameterParser tool, 169-170 Name property of Category class, ParameterParser tool, using with significance of, 124 VelocityViewServlet, 167, 169 NameCardTask, creating in Texen, 250, parent template, relationship to #parse 253 directive, 56 newsletter application. See e-mail #parse directive newsletter application embedding other templates with, newsletter applicationSection class, role 55-58 in domain object model of e-mail versus #include, 92-93 newsletter application, 125-126 versus #include directive, 58 NewsletterException class, code for, 139 syntax of, 350 NewsletterManager class, role in e-mail ParseErrorException error, throwing, 43, newsletter application, 137-138 89 NewsletterModelBean class, role in e• ParseException error, throwing, 52 mail newsletter application, 145 parse~using,305-311 NewsletterSection objects, creating Parser instance, creating, 287 array of, 136 parser. pool.size configuration NewsletterTemplate interface parameter, description of, 21, 351 code for, 142 parsing, overview of, 307 implementations for e-mail news• performance testing File versus URL letter application, 143 instance, 324-325 NewsletterTemplateFactory class, role Perffest, results of, 293 in e-mail newsletter application, PersistEnum interface, relationship to 146-147 Hibernate, 178 NOT operator (!), example of, 60 Person class $now context object for TexenTask, creating in Texen, 249 description of, 245 using with DVSL, 264-266 null return values, handling with plain-text email, example of, 154 VelocityServlet, 158 plain-text template, creating for e-mail NullSetEventHandler newsletter application, 147-148 implementing, 315-317 Powered by Velocity page, address for, 8 purpose of, 314 PreferredFormat property, role in NumberTool, formatting numbers with, Subscriber class, 121 72-73 presentation tier, using Velocity in, 93 numeric literals, example of, 48 Print-Friendly Product Detail template, creating for Widgetarium database, 194 o processing cycle, example of, 267-270 Objects and JavaBeans, relationship to Product class context, 84 code for, 62-63 ORM (Object Relational Mapping) tool, for Widgetarium database, 176-178 Hibernate as, 173 Product Details template, creating for out property of DVSLTask, description Widgetarium database, 193 of,260 Product List template $outputDirectory context object for code for, 63 TexenTask, description of, 245 creating for Widgetarium database, outputDirectory property for TexenTask, 191-192 description of, 244 output from, 68 output.encoding configuration Product mapping, creating for parameter, description of, 21, 351 Widgetarium database, 180 outputEncoding property for TexenTask, ProductCatalogDAO interface description of, 244 code for, 176 outputencoding property of DVSLTask, creating for Widgetarium database, description of, 260 182

364 Index

creating Hibernate implementation reflection process, carrying out, 310 of, 179 refreshResourceO method, result of call using StoreManager class with, to, 299-301 186-187 $relativePath variable ProductCatalogDAOHibernatelmpl class description of, 226 advisory about, 183 significance in Anakia, 233 creating instances of, 185-186 RemoveFromCartAction class, using ProductDetailAction class, using with with Velocity and Struts, 202-203 Velocity and Struts, 199-200 renderO method Producilist application calling, 308 code for, 98-99 implementing, 331-332 improvementot 100-101, 103-104 implementing in ImageDirective ProductlistAction class, using with class, 335-336 Velocity and Struts, 198-199 RenderTool, performing runtime VTL ProducilistModel class, code for, 102 evaluation with, 79-80 $project variable, description of, 226 $request variable, using with projectFile AnakiaTask tag attribute, VelocityViewServlet,165 description of, 225 requestCleanupO hook method, using project. file, defining with Anakia, with VelocityServlet, 163 229-230 requiresCheckingO method, effect of, $properties context object for 300 TexenTask, description of, 245 resource loaders Properties file, creating for Velocity choosing, 34, 90-91 runtime, 20 overview of, 25-26 Properties instance, creating for Velocity using, 26-27 runtime, 19 Resource Manager, configuring, 33-34 properties, storing in Maps, 46-47 resource. * configuration parameters, tag, using with DVSL, descriptions of, 33-34 261-262 ResourceCache implementation, public constants, storing values for loading, 283-284 Category class as, 179 ResourceLoader class. See also pull MVC, explanation of, 95 CachingClasspathResourceLoader pushMVC class examples of, 96 code for, 289-290 explanation of, 94 overview of, 324, 330 putAppValueO method, using with ResourceLoaderFactory.getLoaderO TransformTool interface, 256 method, effect of, 282 ResourceLoaders configuring, 353-354 Q creating and customizing, 323-330 Query interface, using with loading and initializing, 282 Widgetarium database, 184-185 ResourceManager implementation quiet references, significance of, 47 configuring, 352 $quote variable, creating, 50 loading and initializing, 280-284 ResourceManagerImpl.getResource0 method, code for, 294-295 R resource.manager.logwhenfound RandomMessageTool class, code for, configuration parameter, checking 171 status of, 282-283 ranges of numbers and arrays, using, 62 ResourceNotFoundException, throwing, refactoring, significance of, 92 294,302 ReferenceInsertEventHandler, Resource.processO method, result of implementing, 317 call to, 298, 301-304 ReferenceInsertionEventHandler, resources, locating and loading, 268 implementing, 315 Resource.touchO method, result of call ReferenceinsertionEventHandler to, 298 interface, purpose of, 314

365 Index

reusable content, separating into servlets separate resources, 92 building on top ofVelocityServlet, Roller Weblogger, description of, 6-7 158-162 $root variable overview of, 172 description of, 226 Session implementation, obtaining for using with Anakia and JDOM, 219-220 Widgetarium database, 184 document element, using with SessionFactory interrace, role in Anakia, 217 Widgetarium database, 182 runtime instances. See also Velocity #set directive runtime declaring array variables with, 61 creating for VelocityEngine class, 38 syntax of, 347 managing, 86-88 using with EventHandler class, 319 runtimeVTLevruuation,perrorming setO methods, using with Product class, with RenderTool, 79-80 62-63 runtime.* setActionCommandO method, cruling in configuration options, 351 e-mail newsletter application, 133 logging configuration parameters, 25 setContentTypeO hook method, using Runtimelnstance class, code for, with VelocityServlet, 163 271-272 setDefaultPropertiesO method, example RuntimeInstance.getTemplateO of,274-275 method, code for, 294 setURIO method, using with linkTool, RuntimeInstance.initO method, code 169 for, 273 shouldLogOnNuliO message, declaring, RuntimeInstance.initiruizeDirectivesO 316 method, code for, 284-285 ShowCartAction class RuntimeInstance.initiruizeIntrospection significance of, 202 o method, code for, 287-289 using with Velocity and Struts, 203-204 RuntimeInstance.initiruizeLoggerO SimpleLog4JLogSystem, method, code for, 275-276 recommendation of, 24 RuntimeInstance.initiruizeParserPoolO SimplePool class, purpose of, 287 method, code for, 286-287 single quotes (') RuntimeInstance.parseO method, effect embedding in strings, 49-50 of,303 using, 91 runtime.interpolate.literrus configur• site.vsl style sheet, defining with Anakia, ation parameter, description of, 21 229 runtime.introspector.uberspect config• SMTP _SERVER constant, role in e-mail uration parameter, description of, newsletter application, 138 21 RuntimeSingleton class, significance of, building Velocity from, 10 271 building VelocityTools from, 11-12 Spring controller, configuring, 211-212 Spring Framework S IoC capabilities of, 210 Scarab, description of, 6 using Velocity with, 206-214 schmoo, explanation of, 268 Spring servlet, creating configuration scopes, using in Veltag, 238-241. See also for, 210-211 strict scoping StoreActionController, code for, 207-209 SecureLinkTool, description of, 206 storeController bean, using with selectNodesO method Velocity and Spring, 212 of DVSLNode interrace, 257 storeControllerResolver bean, using significance of, 226 with Velocity and Spring, 212-213 selectSingleNodeO method ofDVSLNode storeManager bean, using with Velocity interrace, description of, 257 and Spring, 212 sendNewslettersO method, role in e• StoreManager implementation, setting mail newsletter application, with Spring Framework, 210 135-136, 139-140 StoreManager interrace, role in servletFoo beans, retrieving, 242-243 Widgetarium database, 186

366 Index

StoreManagerFactory class, role in Template.processO method, effect of, Widgetarium database, 187-188 302-303 I store I showCart request URI, templates significance of, 202 accessing data within, 44-45 strict scoping, using, 241-243. See also annotating, 43-44 scopes for concrete classes, 114 string interpolation, explanation of, 49 creating for VelocityEngine class, 36 String literals creating for Widgetarium database, example of, 48 191-197 marking for string interpolation, 91 creating output from, 17-18 preventing interpolation of, 49 creating variables in, 47-48 $strings context object for TexenTask, generating description of, 245 AbstractVelocityContentTemplate, StringTemplate template engine, 108-109 description of, 2 loading tools into, 167-168 Struts, using Velocity with, 197-206 merging with contexts, 268 struts-config.xml file, creating, 204-205 obtaining references to, 14 StrutsLinkTool, description of, 206 parsing, 268 StrutsTools, using, 206 placing in classes directory, 161 style AnakiaTask tag attribute, processing cycle of, 267-270 description of, 224 using macros with, 67-68 style property of DVSLTask, description using with e-mail newsletter of, 260 application, 147-151 SubscribedCatetories property, role in using with NumberTool, 73 Subscriber class, 122 Texen. See also Ant Subscriber class, role in domain object creating Hello World with, 245-247 model of e-mail newsletter features of, 243-244, 253 application, 120-122 loading data into context with, SubscriberManager class, role in 247-248 domain object model of e-mail purpose of, 5 newsletter application, 124-125 Texen context objects, list of, 245 subscribers TexenTask properties characteristics of, 117-118 list of, 244 role in e-mail newsletter application, subclassing, 248-253 119 TextPad, using Velocity in, 344 Swing, significance of, 127 throughput, increasing, 333-334 TilesTool, description of, 206 Tomcat, downloading, 161 T Toolbox tag, using with Anakia, 218 config file for, 167-168 template engines using with DVSL, 258-259 examples of, 2-3 using with VelocityViewServlet, Velocity as, 1 166-171 Template instance, retrieving with toolboxfile property of DVSLTask, getTemplateO method, 159-160 description of, 260 template interfaces, creating, 111-112 tools, explanation of, 72. See also template object model, providing for e• custom tools mail newsletter application, TransformTool interface, methods of, 142-147 256 TemplateException, throwing, 107-108 $treeWalk variable, description of, 226 TemplateFactory class, example of, 114 typed API, providing with abstraction Template.mergeO method, calling, layer, 106, 115 307-308 templatePath AnakiaTask tag attribute, description of, 225 U templatePath property for TexenTask, Uberspect interface, significance of, 310 description of, 244 UltraEdit, using Velocity in, 343-344

367 Index

updateCategoryContentsO method, using in TextPad, 344 calling in e-mail newsletter appli• using in UltraEdit, 343-344 cation, 133-134 using with Spring Framework, 206-214 URL instance versus performance test• using with Struts, 197-206 ing File, 324-325 Web site for, 9 urlMapping bean, using with Velocity Velocity build, testing, 11 and Spring, 212 Velocity helper class, using, 86 useClasspath property for TexenTask, Velocity JAR file. See JAR files description of, 244 Velocity logging, configuring, 21-25 user interface, developing for e-mail Velocity parser, using, 305-311 newsletter application, 126-137 Velocity runtime. See also runtime UserProfile class, code for, 110-111 instances UserProfileModelBean, purpose of, 114 general configuration parameters for, UserProfileTemplate interface, code for, 20-21 111-112 initializing, 14 $userType variable, adding to interacting with, 268-269 VelocityContext, 59 setting configuration parameters for, 19-20 Velocity templates V accessing data within, 44-45 validatingparser property of DVSLTask, annotating, 43-44 description of, 260 for concrete classes, 114 ValidatorTool, description of, 206 creating for VelocityEngine class, 36 valueO method of DVSLNode interface, creating for Widgetarium database, description of, 257 191-197 valueOfO method of DVSLNode creating output from, 17-18 interface, description of, 257 creating variables in, 47-48 variable concatenation, overview of, 48-50 generating variable names and dollar symbol ($), AbstractVelocityContentTemplate, outputting, 50-53 108-109 variable references, making quiet, 47 loading tools into, 167-168 variables merging with contexts, 268 creating in templates, 47-48 obtaining references to, 14 using, 44 parsing, 268 Velocimacros. See also macros placing in classes directory, 161 building, 66-69 processing cycle of, 267-270 configuring, 35, 69-71, 289,355 using macros with, 67-68 Velocity using with e-mail newsletter applica• benefits of, 93 tion,147-151 benefits of templating with, 4 using with NumberTool, 73 building from source code, 10 Velocity tools, overview of, 7-8 configuring, 350-355 velocityconfigclass property of configuring in Spring, 211 DVSLTask, description of, 260 configuring to use 10g4j, 22-24 VelocityContext class decoupling, 105 significance of, 14-16 on desktop, 7-8 using context as alternative to, 82-83 features of, 5-6 VelocityEngine class installing, 9-12 overview of, 291-293 justifications for use of, 3 using, 35-39, 86-88,269-270 overview of, 1-2,93 Velocity.getTemplateO, significance of, 26 power of, 15 Velocity.initO method, significance of, and Scarab, 6 270-271 understanding uses of, 4-5 VelocityManager class, code for, 87-88 using Commons Logging tool with, velocity. properties file, contents of, 274 24-25 velocityPropertiesFile AnakiaTask tag using in Emacs, 345 attribute, description of, 225 using in IntelliJ IDEA, 342 VelocityServlet using in IEdit, 342-343 creating Hello World with, 158-162 368 Index

extensions for, 163 using #include instead of #parse, overview of, 157-158 92-93 VelocityTools using single quotes ('), 91 building from source code, 11-12 VTL (Velocity Template Language) DateTool,73-75 configuring, 35-39 IteratorTool,77-79 overcoming shortcomings of, 71-80 MathTool, 75-77, 99 overview of, 41 NumberTool, 72-73 Significance of, 1-2 RenderTool, 79-80 significance of, 6 Struts tools in, 206 W Web address for, 12 WAR file, creating for VelocityServlet, VelocityViewResolver class, purpose of, 161 211 Web applications. See also Widgetarium VelocityViewServlet database configuring, 165 building with Spring Framework, creating Hello World with, 164 207-209 overview of, 164 creating database for, 173-175 and Struts, 197 overview of, 172-173 using custom tools with, 171-172 Web sites using Toolbox with, 166-171 Ant, 10 VeloEdit for Eclipse development tool, Apache Tomcat, 161 using, 340-342 CVS repository, 235, 342-343 VeloGUI development tool, using, 339-340 dom4j distribution, 254 Veltag Enhydra XMLC template engine, 3 building, 235-236 Gentleware Poseidon, 7 creating FooServlet class with, Hibernate, 173 238-241 IntelliJ IDEA, 7 creating Hello World with, 236-237 JAP (JavaBeans Activation features of, 235, 243 Framework),137 using scopes in, 238 Jakarta Commons, 24 veltag.jsp file, creating beans in, Jakarta Commons Collections and 239-240 Jakarta ORO, 11 veltagStrict.jsp file, code for, 241-242 Jamon template engine, 2 View in MVC, significance of, 94-95 JavaCC, 305 view path, specifying as in JavaMail, 137 Veltag, 240 JBYTE template engine, 2 View separation, significance of, 97-98, JDOM,216 115 JEdit, 342 View technology, avoiding reliance on, JJTree, 305 104-105 Luxor tool, 8 View transparency, loss of, 96 Middlegen tool, 8 viewResolver bean, using with Velocity MySQL,173 and Swing, 211 Powered byVelocity page, 8 views. See VelocityViewServlet Roller Weblogger, 7 .vm extension, Significance of, 165-166 Scarab,6 .vsl extension, significance of, 217 Spring Framework, 207 VTL constructs StringTemplate template engine, 2 annotating templates, 43-44 Struts, 197 methods, 45-53 TextPad, 344 overview of, 42 UltraEdit, 343 variables, 44 Velocity,9 VTL markup, adding to Hello World VelocityTools project, 12 application, 15 VeloEdit for Eclipse development VTL techniques tool,340 avoiding complex processing, 93 VeloGUI development tool, 340 separating reusable content into sep• WebMacro template engine, 2 arate resources, 92

369 Index

Weblink property of Category class, Product class for, 176-178 significance of, 124 Product mapping for, 180 weblogging, significance of, 6--7 implementing ProductCatalogDAO WebMacro template engine, description for, 182 of,2 templates for, 191-197 web.xml files writeAttributeO method, using with completing for Velocity and Spring, ImageDirective class, 336 213 writeTable macro, code for, 65 configuring Veltag servlets in, 240 writeTemplateOutputO method, creating forVelocityServlet, 160 example of, 38 $who variable, changing value of, 16 Widgetarium database. See also Web applications X business tier for, 186--188 XMLfiles cart classes for, 188-191 using with Anakia, 227-228 Category class for, 178-179 using with DVSL, 261 creating, 173-175 $xmlout variable, deSCription of, 226 data access layer for, 175-186 XPath queries, using with Anakia, 228 DataAccessException class for, 181 $xpath variable, description of, 226 Hibernate config file for, 180-181 XSL, resource for, 253

370