jvanzyl     00/10/09 08:08:22

  Modified:    src/java/org/apache/velocity/runtime Runtime.java
               src/java/org/apache/velocity/runtime/directive
                        Directive.java Dummy.java Foreach.java
  Log:
  - updates to reflect changes in nodes structure. fix a couple
    of concurrency problems.
  
  Revision  Changes    Path
  1.5       +8 -2      
jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
  
  Index: Runtime.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Runtime.java      2000/10/03 17:05:41     1.4
  +++ Runtime.java      2000/10/09 15:08:20     1.5
  @@ -111,9 +111,13 @@
   
       private static Parser parser;
   
  -    public static void init(String properties)
  +    private static boolean initialized;
  +
  +    public synchronized static void init(String properties)
           throws Exception
       {
  +        if (! initialized)
  +        {
           try
           {
               Configuration.setPropertiesFile(properties);
  @@ -155,12 +159,14 @@
               parser.setDirectives(directives);
   
               info("Velocity successfully started.");
  +            initialized = true;
           }
           catch (Exception e)
           {
               System.out.println(e);
               e.printStackTrace();
           }
  +        }
       }
   
       public static void setProperty(String key, String value)
  @@ -180,7 +186,7 @@
           templateLoader.init();
       }
   
  -    public static Template getTemplate(String template)
  +    public synchronized static Template getTemplate(String template)
       {
           try
           {
  
  
  
  1.3       +12 -4     
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java
  
  Index: Directive.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Directive.java    2000/10/03 17:08:00     1.2
  +++ Directive.java    2000/10/09 15:08:20     1.3
  @@ -7,14 +7,22 @@
   import org.apache.velocity.runtime.parser.Node;
   
   
  -public interface Directive
  +public abstract class Directive
   {
       public static final int BLOCK = 1;
       public static final int LINE = 2;
       
  -    public String getName();
  -    public int getType();
  +    public abstract String getName();
  +    public abstract int getType();
  +    
  +    public void init(Context context, Node node) throws Exception
  +    {
  +        int i, k = node.jjtGetNumChildren();
   
  -    public void render(Context context, Writer writer, Node node)
  +        for (i = 0; i < k; i++)
  +            node.jjtGetChild(i).init(context, null);
  +    }
  +    
  +    public abstract void render(Context context, Writer writer, Node node)
           throws IOException;
   }        
  
  
  
  1.3       +6 -1      
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Dummy.java
  
  Index: Dummy.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Dummy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Dummy.java        2000/10/03 17:08:01     1.2
  +++ Dummy.java        2000/10/09 15:08:21     1.3
  @@ -7,13 +7,18 @@
   
   import org.apache.velocity.runtime.parser.Node;
   
  -public class Dummy implements Directive
  +public class Dummy extends Directive
   {
       public String getName() { return "dummy"; }
       public int getType() { return LINE; }
   
  +    public void init(Context context, Node node) throws Exception
  +    {
  +    }
  +    
       public void render(Context context, Writer writer, Node node)
           throws IOException
       {
       }
  +
   }
  
  
  
  1.4       +78 -68    
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java
  
  Index: Foreach.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Foreach.java      2000/10/03 17:08:01     1.3
  +++ Foreach.java      2000/10/09 15:08:21     1.4
  @@ -14,87 +14,97 @@
   import org.apache.velocity.runtime.parser.Node;
   import org.apache.velocity.runtime.parser.Token;
   
  -public class Foreach implements Directive
  +public class Foreach extends Directive
   {
       public String getName() { return "foreach"; }        
       public int getType() { return BLOCK; }
   
  -    public void render(Context context, Writer writer, Node node)
  -        throws IOException
  -    {
  -    
  -        // tokens 2,4
  -        Object data = null;
  -        Object listObject = null;
  -        
  -        // Now we have to iterate over all the child nodes
  -        // for each in the list, we have to change the
  -        // context each time the element changes.
  -        String elementKey = node.jjtGetChild(0).getFirstToken()
  -                                .image.substring(1);
  -        
  -        // if there is an object in the context with
  -        // the same name as the $element save it so
  -        // we can restore it after the #foreach.
  -        
  -        // The Collection interface provides iterator()
  -        // I don't think elements() is provided by an interface
  -        // it just looks like it belongs to Vector. Crappy
  -        // 1.1 vs 1.2 junk.
  +    private final static int ARRAY = 1;
  +    private final static int ITERATOR = 2;
   
  -        Object tmp = null;
  -        if (context.containsKey(elementKey))
  -            tmp = context.get(elementKey);
  -
  +    private String elementKey;
  +    private Object listObject;
  +    private Object tmp;
  +    
  +    public void init(Context context, Node node) throws Exception
  +    {
  +        Object sampleElement = null;
  +        
  +        elementKey = node.jjtGetChild(0).getFirstToken()
  +                        .image.substring(1);
  +        
  +        // This is a refence node and it needs to
  +        // be inititialized.
  +        
  +        node.jjtGetChild(2).init(context, null);
           listObject = node.jjtGetChild(2).value(context);
           
  -        /*!
  -         * @desc Need to create a ReferenceException here, for
  -         * example when the listObject is null. This
  -         * obviously should be caught.
  -         * @priority 1
  -         */
  -
  +        // Figure out what type of object the list
  +        // element is so that we don't have to do it
  +        // everytime the node is traversed.
  +        
  +        if (listObject == null)
  +        {
  +            System.out.println("dude you're fucked!");
  +            System.out.println(node.jjtGetChild(2).getFirstToken().image);
  +        }         
  +        
           if (listObject instanceof Object[])
           {
  -            int length = ((Object[]) listObject).length;
  -            
  -            for (int i = 0; i < length; i++)
  -            {
  -                context.put(elementKey,((Object[])listObject)[i]);
  -                node.jjtGetChild(3).render(context, writer);
  -            }
  -            context.remove(elementKey);
  -        }
  +            node.setInfo(ARRAY);
  +            sampleElement = ((Object[]) listObject)[0];
  +        }            
           else if (ClassUtils.implementsMethod(listObject, "iterator"))
           {
  -            // Maybe this could be optimized with get(index) ?
  -            // Check the interface. size() and get(index) might
  -            // be faster then using an Iterator.
  -            Iterator i = ((Collection) listObject).iterator();
  -            
  -            while (i.hasNext())
  -            {
  -                context.put(elementKey,i.next());
  -                node.jjtGetChild(3).render(context, writer);
  -            }
  -            context.remove(elementKey);
  -        }
  -        else if (ClassUtils.implementsMethod(listObject, "elements"))
  +            node.setInfo(ITERATOR);
  +            sampleElement = ((Collection) listObject).iterator().next();
  +        }            
  +    
  +        // This is a little trick so that we can initialize
  +        // all the blocks in the foreach  properly given
  +        // that there are references that refer to the
  +        // elementKey name.
  +        
  +        if (sampleElement != null)
           {
  -            Enumeration e = ((Vector) listObject).elements();
  -            
  -            while (e.hasMoreElements())
  -            {
  -                context.put(elementKey,e.nextElement());
  -                node.jjtGetChild(3).render(context, writer);
  -            }
  +            context.put(elementKey, sampleElement);
  +            super.init(context, node);
               context.remove(elementKey);
  -        }
  +        }            
  +    }
   
  -        // Restore the element that was overridden by
  -        // the iterator.
  -        if (tmp != null)
  -            context.put(elementKey, tmp);
  +    public void render(Context context, Writer writer, Node node)
  +        throws IOException
  +    {
  +        listObject = node.jjtGetChild(2).value(context);
  +        
  +        switch(node.getInfo())
  +        {
  +            case ARRAY:
  +                int length = ((Object[]) listObject).length;
  +            
  +                for (int i = 0; i < length; i++)
  +                {
  +                    context.put(elementKey,((Object[])listObject)[i]);
  +                    node.jjtGetChild(3).render(context, writer);
  +                }
  +                context.remove(elementKey);
  +                
  +                break;
  +            
  +            case ITERATOR:
  +                // Maybe this could be optimized with get(index) ?
  +                // Check the interface. size() and get(index) might
  +                // be faster then using an Iterator.
  +                Iterator i = ((Collection) listObject).iterator();
  +            
  +                while (i.hasNext())
  +                {
  +                    context.put(elementKey,i.next());
  +                    node.jjtGetChild(3).render(context, writer);
  +                }
  +                context.remove(elementKey);
  +                break;
  +        }            
       }
   }
  
  
  

Reply via email to