You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by da...@apache.org on 2001/03/04 22:32:28 UTC

cvs commit: jakarta-velocity/whiteboard/daveb/dajarred Example.java

daveb       01/03/04 13:32:28

  Modified:    whiteboard/daveb/dajarred Example.java
  Log:
  Update Example
  
  Revision  Changes    Path
  1.2       +60 -16    jakarta-velocity/whiteboard/daveb/dajarred/Example.java
  
  Index: Example.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/whiteboard/daveb/dajarred/Example.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Example.java	2001/03/02 23:39:50	1.1
  +++ Example.java	2001/03/04 21:32:27	1.2
  @@ -69,12 +69,19 @@
    *
    * @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/03/02 23:39:50 daveb Exp $
  + * @version $Id: Example.java,v 1.2 2001/03/04 21:32:27 daveb Exp $
    */
   
   public class Example
   {
  -    public Example( String templateFile)
  +    VelocityContext context = null;
  +    
  +    private final static String JAR_RESOURCE_LOADER_PATH1 = "jar:file:test.jar!/";
  +
  +    private final static String JAR_RESOURCE_LOADER_PATH2 = "jar:file:template.jar!/";
  +
  +    
  +    public Example()
       {
           try
           {
  @@ -83,27 +90,37 @@
                */
   
               Runtime.init("velocity.properties");
  +            //Runtime.setDefaultProperties();
  +            
  +            //Runtime.setSourceProperty(Runtime.JAR_RESOURCE_LOADER_PATH, JAR_RESOURCE_LOADER_PATH1);
               
  +            //Runtime.setSourceProperty(Runtime.JAR_RESOURCE_LOADER_PATH, JAR_RESOURCE_LOADER_PATH2);
  +            
  +            //Runtime.init();
  +
               /*
                *  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 = 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...)
  -             */
  -
  +        }
  +        catch( Exception e )
  +        {
  +            Runtime.error("ERROR starting runtime: " + e );
  +        }
  +    }
  +    
  +    public void getTemplate( String templateFile)
  +    throws Exception{
  +        try
  +        {
  +            
               Template template =  null;
  -
  +            
               try 
               {
                   template = Runtime.getTemplate(templateFile);
  @@ -157,10 +174,37 @@
   
       public static void main(String[] args)
       {
  -        Example t = new Example("/example/test1.vm");
  -                
  +        Example ex = new Example();
  +        
  +        try
  +        {
  +            say( "Started..." );
  +            
  +            say("get template from template.jar");
  +            ex.getTemplate( "/template/test1.vm" );
  +            
  +            say("get template from test.jar");
  +            ex.getTemplate( "/example/test1.vm" );
  +            
  +            say("try template from template.jar again");
  +            ex.getTemplate( "/template/test2.vm" );
  +            
  +            say("Try something that doesn't exist");
  +            ex.getTemplate( "/example/yomama.vm" );
  +        }
  +        catch(Exception e )
  +        {
  +            say("ERROR");
  +        }
  +    }
  +
  +    private static void say( String m )
  +    {
  +        System.out.println(m);
       }
   }
  +
  +