You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2001/02/12 04:08:23 UTC

cvs commit: jakarta-velocity/examples/app_example1 Example.java README.txt example.sh example.vm velocity.properties velocity_example.log

geirm       01/02/11 19:08:23

  Added:       examples/app_example1 Example.java README.txt example.sh
                        example.vm velocity.properties velocity_example.log
  Log:
  This is the first application example - was located in the examples
  directory.
  
  This example shows how to use Velocity in a standalone application using
  the Velocity runtime methods.
  
  Revision  Changes    Path
  1.1                  jakarta-velocity/examples/app_example1/Example.java
  
  Index: Example.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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.runtime.Runtime;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.Template;
  
  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@periapt.com">Jason van Zyl</a>
   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
   * @version $Id: Example.java,v 1.1 2001/02/12 03:08:22 geirm Exp $
   */
  
  public class Example
  {
      public Example(String templateFile)
      {
          try
          {
              /*
               * setup
               */
  
              Runtime.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.
               */
  
              Template template = Runtime.getTemplate(templateFile);
               
              /*
               *  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));
              template.merge(context, writer);
  
              /*
               *  flush and cleanup
               */
  
              writer.flush();
              writer.close();
          }
          catch( Exception e )
          {
              System.out.println(e);
              e.printStackTrace();
          }
      }
  
      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]);
      }
  }
  
  
  
  1.1                  jakarta-velocity/examples/app_example1/README.txt
  
  Index: README.txt
  ===================================================================
  Welcome to Velocity!
  
  As always, the if you have any questions :
  
  1) Make sure you followed any directions :)  (did you build everything?)
  2) Review documentation included in this package, or online at
        http://jakarta.apache.org/velocity
  3) Ask on the velocity-user list.  This is a great source of support information.
     To join, read http://jakarta.apache.org/site/mail.html and then follow the 
     link at the bottom to join the lists.
  
  app_example1 
  ------------
  This simple example shows how to use the Velocity Template Engine
  in a standalone program.  It should be pre-compiled for you. Run it using the example
  template provided (example.vm):
  
    ./example.sh
  
  
  
  
  1.1                  jakarta-velocity/examples/app_example1/example.sh
  
  Index: example.sh
  ===================================================================
  echo "Running Example with input file 'example.vm'"
  
  for i in ../../bin/*.jar
  do
      _VELCP=$VELCP:"$i"
  done
  
  java -cp $_VELCP:. Example example.vm  
  
  
  
  
  1.1                  jakarta-velocity/examples/app_example1/example.vm
  
  Index: example.vm
  ===================================================================
  ## This is an example velocity template
  
  #set( $this = "Velocity")
  
  $this is great!
  
  #foreach( $name in $list )
      $name is great!
  #end
  
  #set( $condition = true)
  
  #if ($condition)
      The condition is true!
  #else
      The condition is false!
  #end    
  
  
  
  1.1                  jakarta-velocity/examples/app_example1/velocity.properties
  
  Index: velocity.properties
  ===================================================================
  #
  # This is a simple example of a velocity properties file.
  #
  # Any property that is not listed here will have it's default
  # value used.  The default values are located in :
  #  *  src/java/org/apache/velocity/runtime/default/velocity.defaults
  #  *  http://jakarta.apache.org/velocity/developer-guide.html
  #
  # as an example, we are changing the name of the velocity log 
  #
  runtime.log = velocity_example.log
  
  
  
  
  1.1                  jakarta-velocity/examples/app_example1/velocity_example.log
  
  Index: velocity_example.log
  ===================================================================
  Sun Feb 11 21:26:38 EST 2001   [info] Default Properties File: org/apache/velocity/runtime/defaults/velocity.properties
  Sun Feb 11 21:26:38 EST 2001   [info]    ** Property Override : runtime.log = velocity_example.log
  Sun Feb 11 21:26:38 EST 2001   [info] Log file being used is: /home/gmj/velocity/jakarta-velocity/examples/app_example1/velocity_example.log
  Sun Feb 11 21:26:38 EST 2001   [info] Resource Loader Instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader
  Sun Feb 11 21:26:38 EST 2001   [info] Resources Loaded From: /home/gmj/velocity/jakarta-velocity/examples/app_example1/.
  Sun Feb 11 21:26:38 EST 2001   [info] Resource Loader Initialized.
  Sun Feb 11 21:26:38 EST 2001   [info] Loaded Pluggable Directive: org.apache.velocity.runtime.directive.Literal
  Sun Feb 11 21:26:38 EST 2001   [info] Loaded Pluggable Directive: org.apache.velocity.runtime.directive.Macro
  Sun Feb 11 21:26:38 EST 2001   [info] Loaded Pluggable Directive: org.apache.velocity.runtime.directive.Parse
  Sun Feb 11 21:26:38 EST 2001   [info] Loaded Pluggable Directive: org.apache.velocity.runtime.directive.Include
  Sun Feb 11 21:26:38 EST 2001   [info] Loaded Pluggable Directive: org.apache.velocity.runtime.directive.Foreach
  Sun Feb 11 21:26:38 EST 2001   [info] Created: 20 parsers.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : initialization starting.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : adding VMs from global VM library template : VM_global_library.vm
  Sun Feb 11 21:26:38 EST 2001   [info] Attempting to find VM_global_library.vm with org.apache.velocity.runtime.resource.loader.FileResourceLoader
  Sun Feb 11 21:26:38 EST 2001  [error] FileResourceLoader Error: cannot find resource /home/gmj/velocity/jakarta-velocity/examples/app_example1/./VM_global_library.vm
  Sun Feb 11 21:26:38 EST 2001  [error] java.lang.Exception: Can't find VM_global_library.vm!
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : global VM library template macro registration complete.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : no local VM library template used.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : allowInline = true : VMs can be defined inline in templates
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : allowInlineLocal = false : VMs defined inline will be  global in scope if allowed.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : messages on  : VM system will output information messages
  Sun Feb 11 21:26:38 EST 2001   [info] Velocimacro : initialization complete.
  Sun Feb 11 21:26:38 EST 2001   [info] Velocity successfully started.
  Sun Feb 11 21:26:38 EST 2001   [info] Attempting to find example.vm with org.apache.velocity.runtime.resource.loader.FileResourceLoader