jvanzyl     00/12/18 21:36:32

  Modified:    src/java/org/apache/velocity Template.java
  Log:
  - Template now extends Resource, so most of the method have been
    removed because they are present in resource. I just left this
    class here because if it was moved it will break every vel app.
  
    I'd like to rename it TemplateResource and move it into the
    resource package, but that can wait.
  
  Revision  Changes    Path
  1.19      +27 -157   jakarta-velocity/src/java/org/apache/velocity/Template.java
  
  Index: Template.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/Template.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Template.java     2000/12/10 04:56:04     1.18
  +++ Template.java     2000/12/19 05:36:32     1.19
  @@ -59,7 +59,7 @@
   import java.io.Writer;
   
   import org.apache.velocity.runtime.Runtime;
  -import org.apache.velocity.runtime.loader.TemplateLoader;
  +import org.apache.velocity.runtime.resource.Resource;
   import org.apache.velocity.runtime.parser.ParseException;
   import org.apache.velocity.runtime.parser.node.SimpleNode;
   
  @@ -84,24 +84,11 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: Template.java,v 1.18 2000/12/10 04:56:04 geirm Exp $
  + * @version $Id: Template.java,v 1.19 2000/12/19 05:36:32 jvanzyl Exp $
    */
  -public class Template
  +public class Template extends Resource
   {
       /**
  -     * The root of the AST node structure that results
  -     * from parsing a Velocity template. This node
  -     * structure can walk itself, or it can be traversed
  -     * with a visitor. There are two methods for self
  -     * walking: init(), and render(). These are an
  -     * attempt to keep multithreading housekeeping
  -     * issues to a minimum, so that we don't have to
  -     * keep a pool of visitors to init()/render()
  -     * the node structure.
  -     */
  -    private SimpleNode document;
  -    
  -    /**
        * To keep track of whether this template has been
        * initialized. We use the document.init(context)
        * to perform this. The AST walks itself optimizing
  @@ -113,67 +100,31 @@
        */
       private boolean initialized = false;
   
  -    /**
  -     * The template loader that initially loaded the input
  -     * stream for this template, and knows how to check the
  -     * source of the input stream for modification.
  -     */
  -    private TemplateLoader templateLoader;
  -
  -    /**
  -     * The number of milliseconds in a minute, used to calculate the
  -     * check interval.
  -     */
  -    protected static final long MILLIS_PER_MINUTE = 60 * 1000;
  -
  -    /**
  -     * How often the file modification time is checked (in milliseconds).
  -     */
  -    private long modificationCheckInterval = 0;
  -
  -    /**
  -     * The file modification time (in milliseconds) for the cached template.
  -     */
  -    private long lastModified = 0;
  -
  -    /**
  -     * The next time the file modification time will be checked (in 
  -     * milliseconds).
  -     */
  -    private long lastCheck = 0;
  -
  -    /**
  -     * The next time the file modification time will be checked (in 
  -     * milliseconds).
  -     */
  -    private long nextCheck = 0;
  -
  -    /** Template name */
  -    private String name;
  -
       /** Default constructor */
       public Template()
       {
       }
   
  -    /**
  -     * Set the modification check interval.
  -     * @param interval The interval (in minutes).
  -     */
  -    public void setModificationCheckInterval(long modificationCheckInterval)
  +    public boolean process()
       {
  -        this.modificationCheckInterval = modificationCheckInterval;
  +        try
  +        {
  +            InputStream is = resourceLoader.getResourceStream(name);
  +        
  +            if (is != null)
  +            {
  +                data = Runtime.parse(is, name);
  +                initDocument();
  +                return true;
  +            }
  +            else
  +                return false;
  +        }
  +        catch (Exception e)
  +        {
  +            return false;
  +        }            
       }
  -    
  -    public void setDocument(SimpleNode document)
  -    {
  -        this.document = document;
  -    }        
  -
  -    public SimpleNode getDocument()
  -    {
  -        return document;
  -    }        
   
       /**
        *  initializes the document.  init() is not longer 
  @@ -181,94 +132,12 @@
        *  init() carry the template name down throught for VM
        *  namespace features
        */
  -    public void initDocument()
  -        throws Exception
  +    public void initDocument() throws Exception
       {
           Context c = new Context();
           c.setCurrentTemplateName( name );
  -        document.init( c, null);
  -    }
  -
  -    /**
  -     * Is it time to check to see if the template
  -     * source has been updated?
  -     */
  -     public boolean requiresChecking()
  -     {
  -        if ( lastCheck >= nextCheck)
  -        {
  -            return true;
  -        }            
  -        else
  -        {
  -            lastCheck = System.currentTimeMillis();
  -            return false;
  -        }
  -    }
  -
  -    /**
  -     * Touch this template and thereby resetting
  -     * the lastCheck, and nextCheck fields.
  -     */
  -    public void touch()
  -    {
  -        lastCheck = System.currentTimeMillis();
  -        nextCheck = lastCheck + modificationCheckInterval;
  -    }
  -    
  -    /**
  -     * Set the name of this template, for example
  -     * test.vm.
  -     */
  -    public void setName(String name)
  -    {
  -        this.name = name;
  -    }        
  -
  -    /**
  -     * Get the name of this template.
  -     */
  -    public String getName()
  -    {
  -        return name;
  -    }        
  -
  -    /**
  -     * Return the lastModifed time of this
  -     * template.
  -     */
  -    public long getLastModified()
  -    {
  -        return lastModified;
  -    }        
  -    
  -    /**
  -     * Set the last modified time for this
  -     * template.
  -     */
  -    public void setLastModified(long lastModified)
  -    {
  -        this.lastModified = lastModified;
  -    }        
  -
  -    /**
  -     * Return the template loader that pulled
  -     * in the template stream
  -     */
  -    public TemplateLoader getTemplateLoader()
  -    {
  -        return templateLoader;
  +        ((SimpleNode)data).init( c, null);
       }
  -    
  -    /**
  -     * Set the template loader for this template. Set
  -     * when the Runtime determines where this template
  -     * came from the list of possible sources.
  -     */
  -    public void setTemplateLoader(TemplateLoader templateLoader)
  -    {
  -        this.templateLoader = templateLoader;
  -    }        
   
       /**
        * The AST node structure is merged with the
  @@ -283,12 +152,13 @@
       public void merge(Context context, Writer writer)
           throws IOException, Exception
       {
  -        if( document != null)
  +        if( data != null)
           {
               context.setCurrentTemplateName( name );
  -            document.render(context, writer);
  +            ((SimpleNode)data).render(context, writer);
           }
           else
  -            Runtime.error("Template.merge() failure.  The document is null, most 
likely due to parsing error.");
  +            Runtime.error("Template.merge() failure. The document is null, " + 
  +                          "most likely due to parsing error.");
       }
   }
  
  
  

Reply via email to