| /* |
| * The Apache Software License, Version 1.1 |
| * |
| * Copyright (c) 2000-2001 The Apache Software Foundation. All rights |
| * reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in |
| * the documentation and/or other materials provided with the |
| * distribution. |
| * |
| * 3. The end-user documentation included with the redistribution, if |
| * any, must include the following acknowlegement: |
| * "This product includes software developed by the |
| * Apache Software Foundation (http://www.apache.org/)." |
| * Alternately, this acknowlegement may appear in the software itself, |
| * if and wherever such third-party acknowlegements normally appear. |
| * |
| * 4. The names "The Jakarta Project", "Velocity", and "Apache Software |
| * Foundation" must not be used to endorse or promote products derived |
| * from this software without prior written permission. For written |
| * permission, please contact apache@apache.org. |
| * |
| * 5. Products derived from this software may not be called "Apache" |
| * nor may "Apache" appear in their names without prior written |
| * permission of the Apache Group. |
| * |
| * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| * SUCH DAMAGE. |
| * ==================================================================== |
| * |
| * This software consists of voluntary contributions made by many |
| * individuals on behalf of the Apache Software Foundation. For more |
| * information on the Apache Software Foundation, please see |
| * <http://www.apache.org/>. |
| */ |
| |
| |
| import org.apache.velocity.app.Velocity; |
| import org.apache.velocity.VelocityContext; |
| import org.apache.velocity.Template; |
| |
| import org.apache.velocity.exception.ParseErrorException; |
| import org.apache.velocity.exception.ResourceNotFoundException; |
| |
| import java.io.*; |
| import java.util.ArrayList; |
| |
| /** |
| * This class is a simple demonstration of how the Velocity Template Engine |
| * can be used in a standalone application. |
| * |
| * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
| * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| * @version $Id: Example.java,v 1.4 2001/10/22 03:53:22 jon Exp $ |
| */ |
| |
| public class Example |
| { |
| public Example(String templateFile) |
| { |
| try |
| { |
| /* |
| * setup |
| */ |
| |
| Velocity.init("velocity.properties"); |
| |
| /* |
| * Make a context object and populate with the data. This |
| * is where the Velocity engine gets the data to resolve the |
| * references (ex. $list) in the template |
| */ |
| |
| VelocityContext context = new VelocityContext(); |
| context.put("list", getNames()); |
| |
| /* |
| * get the Template object. This is the parsed version of your |
| * template input file. Note that getTemplate() can throw |
| * ResourceNotFoundException : if it doesn't find the template |
| * ParseErrorException : if there is something wrong with the VTL |
| * Exception : if something else goes wrong (this is generally |
| * indicative of as serious problem...) |
| */ |
| |
| Template template = null; |
| |
| try |
| { |
| template = Velocity.getTemplate(templateFile); |
| } |
| catch( ResourceNotFoundException rnfe ) |
| { |
| System.out.println("Example : error : cannot find template " + templateFile ); |
| } |
| catch( ParseErrorException pee ) |
| { |
| System.out.println("Example : Syntax error in template " + templateFile + ":" + pee ); |
| } |
| |
| /* |
| * Now have the template engine process your template using the |
| * data placed into the context. Think of it as a 'merge' |
| * of the template and the data to produce the output stream. |
| */ |
| |
| BufferedWriter writer = writer = new BufferedWriter( |
| new OutputStreamWriter(System.out)); |
| |
| if ( template != null) |
| template.merge(context, writer); |
| |
| /* |
| * flush and cleanup |
| */ |
| |
| writer.flush(); |
| writer.close(); |
| } |
| catch( Exception e ) |
| { |
| System.out.println(e); |
| } |
| } |
| |
| public ArrayList getNames() |
| { |
| ArrayList list = new ArrayList(); |
| |
| list.add("ArrayList element 1"); |
| list.add("ArrayList element 2"); |
| list.add("ArrayList element 3"); |
| list.add("ArrayList element 4"); |
| |
| return list; |
| } |
| |
| public static void main(String[] args) |
| { |
| Example t = new Example(args[0]); |
| } |
| } |