geirm       01/04/10 05:20:16

  Added:       examples/xmlapp_example README.txt XMLTest.java test.xml
                        velocity.log xml.vm xmlapp_example.bat
                        xmlapp_example.sh
  Log:
  Simple example to show :
  
  1) How to access XML data directly in a Velocity template
  2) An example of Velocimacro recursion
  
  Revision  Changes    Path
  1.1                  jakarta-velocity/examples/xmlapp_example/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.
  
  
  xmlapp_example 
  --------------
  Another simple example showing how to use Velocity in a standalone
  program.  This example shows how to use Velocity to access XML data 
  directly from an XML input file within the template.  It also
  demonstrates velocimacro recursion.
  
    ./xmlapp_example.sh 
  or 
     xmlapp_example.bat
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/XMLTest.java
  
  Index: XMLTest.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 [EMAIL PROTECTED]
   *
   * 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 java.io.BufferedWriter;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.Template;
  import org.apache.velocity.app.Velocity;
  
  import org.jdom.Document;
  import org.jdom.input.SAXBuilder;
  
  
  /**
   * Example to show basic XML handling in a template.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
   * @version $Id: XMLTest.java,v 1.1 2001/04/10 12:20:15 geirm Exp $
   */
  public class XMLTest
  {
      public XMLTest( String templateFile)
      {            
          try
          {
              
              /*
               *  and now call init
               */
  
              Velocity.init();
  
                         
              /*
               * build a Document from our xml
               */
  
              SAXBuilder builder;
              Document root = null;
  
              try 
              {
                  builder = new SAXBuilder(  "org.apache.xerces.parsers.SAXParser" );
                  root = builder.build("test.xml");
              }
              catch( Exception ee)
              { 
                  System.out.println("Exception building Document : " + ee);
                  return;
              }
  
              /*
               * now, make a Context object and populate it.
               */
  
              VelocityContext context = new VelocityContext();
              context.put("root", root);
  
              /*
               *  make a writer, and merge the template 'against' the context
               */
   
              Template template = Velocity.getTemplate(templateFile);
  
              Writer writer = new BufferedWriter(new OutputStreamWriter(System.out));
              template.merge( context , writer);
  
              writer.flush();
              writer.close();
          }
          catch( Exception e )
          {
             System.out.println("Exception : " + e);
          }
      }
  
      public static void main(String[] args)
      {
          XMLTest t;
  
          if( args.length < 1 )
          {
              System.out.println("Usage : java XMLTest <templatename>");
              return;
          }
  
          t = new XMLTest(args[0]);
      }
  }
  
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/test.xml
  
  Index: test.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <document>
    <properties>
      <author>
        <email>[EMAIL PROTECTED]</email>
        <name>
           <last>
                <full>Team</full>
                <firstinitial>T</firstinitial>
           </last>
           <first>
                Velocity
           </first>
        </name>
      </author>
      <title>
          The Jakarta Project
      </title>
    </properties>
  
    <body>
        I am the body
    </body>
  </document>
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/velocity.log
  
  Index: velocity.log
  ===================================================================
  Tue Apr 10 08:49:05 EDT 2001 AvalonLogSystem initialized using logfile 
/home/gmj/velocity/jakarta-velocity/examples/xmlapp_example/velocity.log
  Tue Apr 10 08:49:05 EDT 2001   [info]   [info] Default Properties File: 
org/apache/velocity/runtime/defaults/velocity.properties
  Tue Apr 10 08:49:05 EDT 2001   [info] Resource Loader Instantiated: 
org.apache.velocity.runtime.resource.loader.FileResourceLoader
  Tue Apr 10 08:49:05 EDT 2001   [info] FileResourceLoader : initialization starting.
  Tue Apr 10 08:49:05 EDT 2001   [info] FileResourceLoader : adding path '.'
  Tue Apr 10 08:49:05 EDT 2001   [info] FileResourceLoader : initialization complete.
  Tue Apr 10 08:49:05 EDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Literal
  Tue Apr 10 08:49:05 EDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Macro
  Tue Apr 10 08:49:05 EDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Parse
  Tue Apr 10 08:49:05 EDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Include
  Tue Apr 10 08:49:05 EDT 2001   [info] Loaded Pluggable Directive: 
org.apache.velocity.runtime.directive.Foreach
  Tue Apr 10 08:49:06 EDT 2001   [info] Created: 20 parsers.
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : initialization starting.
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : adding VMs from VM library 
template : VM_global_library.vm
  Tue Apr 10 08:49:06 EDT 2001  [error] ResourceManager : unable to find resource 
'VM_global_library.vm' in any resource loader.
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : error using  VM library template 
VM_global_library.vm : org.apache.velocity.exception.ResourceNotFoundException: Unable 
to find resource 'VM_global_library.vm'
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : allowInline = true : VMs can be 
defined inline in templates
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : allowInlineToOverride = false : 
VMs defined inline may NOT replace previous VM definitions
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : allowInlineLocal = false : VMs 
defined inline will be  global in scope if allowed.
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : messages on  : VM system will 
output logging messages
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocimacro : initialization complete.
  Tue Apr 10 08:49:06 EDT 2001   [info] Velocity successfully started.
  Tue Apr 10 08:49:08 EDT 2001   [info] Velocimacro : added new VM : #recursive( e 
indent ) : source = xml.vm
  Tue Apr 10 08:49:08 EDT 2001   [info] ResourceManager : found xml.vm with loader 
org.apache.velocity.runtime.resource.loader.FileResourceLoader
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/xml.vm
  
  Index: xml.vm
  ===================================================================
  #macro ( recursive $e $indent )     
  #if( $e.getChildren().size() > 0 )
  $indent <$e.getName()> 
  #foreach ($child in $e.getChildren() ) 
  #recursive( $child "$indent  " )
  #end
  $indent </$e.getName()>
  #else
  $indent <$e.getName()>
  $indent    $e.getTextTrim()
  $indent </$e.getName()>
  #end
  #end
  
  #set($i = " ")
  
  First, we print out the document tree with a 
  recursive Velocimacro :
  
  #recursive( $root.getRootElement() $i )
  
  
  Next, we access pieces of data directly :
  
  email : 
$root.getRootElement().getChild("properties").getChild("author").getChild("email").getText()
  last name :  
$root.getRootElement().getChild("properties").getChild("author").getChild("name").getChild("last").getChild("full").getText()
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/xmlapp_example.bat
  
  Index: xmlapp_example.bat
  ===================================================================
  @echo off
  
  set VELCP=.
  for %%i in (..\..\bin\*.jar) do set VELCP=%VELCP%;%%i
  
  for %%i in (..\..\build\lib\*.jar) do set VELCP=%VELCP%;%%i
  
  echo Using classpath:  %VELCP%
  
  java -cp %VELCP% XMLTest xml.vm
  
  
  
  
  1.1                  jakarta-velocity/examples/xmlapp_example/xmlapp_example.sh
  
  Index: xmlapp_example.sh
  ===================================================================
  #!/bin/sh
  
  for i in ../../build/lib/*.jar
  do
      _VELCP=$_VELCP:"$i"
  done
  
  for i in ../../bin/*.jar
  do
      _VELCP=$_VELCP:"$i"
  done
  
  
  java -cp $_VELCP XMLTest xml.vm
  
  
  
  

Reply via email to