geirm       00/12/09 20:52:52

  Modified:    src/java/org/apache/velocity/runtime VelocimacroFactory.java
  Log:
  Added support for template-scope inline VMs.  Move VM management to a VMManager 
class, and modified the addVM() gating rules.
  
  Revision  Changes    Path
  1.3       +127 -77   
jakarta-velocity/src/java/org/apache/velocity/runtime/VelocimacroFactory.java
  
  Index: VelocimacroFactory.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/VelocimacroFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- VelocimacroFactory.java   2000/12/06 05:58:57     1.2
  +++ VelocimacroFactory.java   2000/12/10 04:52:51     1.3
  @@ -58,25 +58,26 @@
    *   manages the set of VMs in a running Velocity engine.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: VelocimacroFactory.java,v 1.2 2000/12/06 05:58:57 geirm Exp $ 
  + * @version $Id: VelocimacroFactory.java,v 1.3 2000/12/10 04:52:51 geirm Exp $ 
    *
    */
   
   package org.apache.velocity.runtime;
   
  -import java.util.Hashtable;
   import java.util.TreeMap;
   
   import org.apache.velocity.runtime.directive.Directive;
   import org.apache.velocity.runtime.directive.VelocimacroProxy;
   import org.apache.velocity.Template;
  +import org.apache.velocity.runtime.VelocimacroManager;
   
   public class VelocimacroFactory
   {
  -    private Hashtable hMacros_ = new Hashtable();
  +    private VelocimacroManager vmManager_ = new VelocimacroManager();
  +
       private boolean bReplaceAllowed_ = false;
       private boolean bAddNewAllowed_ = true;
  -    private Object  obLock = new Object();
  +    private boolean bTemplateLocal_ = false;
   
       /** name of global Velocimacro library template */
       private static String GLOBAL_LIBRARY = "velocimacro.library.global";
  @@ -85,11 +86,14 @@
       private static String LOCAL_LIBRARY  = "velocimacro.library.local";
   
       /** boolean (true/false) default true : allow inline (in-template) macro 
definitions */
  -    private static String PERM_ALLOW_INLINE  = 
"velocimacro.permissions.allowInline";
  +    private static String VM_PERM_ALLOW_INLINE  = 
"velocimacro.permissions.allowInline";
   
       /** boolean (true/false) default false : allow inline (in-template) macro 
definitions to replace existing */
  -    private static String PERM_ALLOW_INLINE_OVERRIDE  = 
"velocimacro.permissions.allowInlineToOverride";
  -
  +    public final static String VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL  = 
"velocimacro.permissions.allowInlineToReplaceGlobal";
  +    
  +    /** switch for forcing inline macros to be local */
  +    public final static String VM_PERM_INLINE_LOCAL = 
"velocimacro.permissions.allowInlineLocalScope";
  +    
       /**
        *    setup
        */
  @@ -99,13 +103,19 @@
            *  maybe I'm just paranoid...
            */
   
  -        synchronized( obLock )
  +        synchronized( this )
           {
               /*
                *   allow replacements while we add the libraries, if exist
                */
               
               setReplacementPermission( true );
  +
  +            /*
  +             *  add all library macros to the global namespace
  +             */
  +
  +           vmManager_. setNamespaceUsage( false );
           
               /*
                *  now, if there is a global or local libraries specified, use them.
  @@ -142,7 +152,8 @@
               }
               else
                   Runtime.info("Velocimacro : no local VM library template used.");
  -     
  +   
  +
               /*
                *   now, the permissions
                */
  @@ -156,9 +167,8 @@
               
               setAddMacroPermission( true );
               
  -            strLib = Runtime.getString( PERM_ALLOW_INLINE, "");
               
  -            if ( strLib.equals("false"))
  +            if ( !Runtime.getBoolean( VM_PERM_ALLOW_INLINE, true) )
               {
                   setAddMacroPermission( false );
                   Runtime.info("Velocimacro : allowInline = false : VMs can not be 
defined inline in templates");
  @@ -167,54 +177,45 @@
                   Runtime.info("Velocimacro : allowInline = true : VMs can be defined 
inline in templates");
   
               /*
  -             *  allowInlineToOverride : allows an inline, if allowed at all
  -             *  to replace an existing VM
  +             *  allowInlineToReplaceGlobal : allows an inline VM , if allowed at 
all,
  +             *  to replace an existing global VM
                *
                *  default = false
                */
               
               setReplacementPermission( false );
  -            
  -            strLib = Runtime.getString( PERM_ALLOW_INLINE_OVERRIDE, "");
               
  -            if (strLib.equals("true"))
  +            if ( Runtime.getBoolean( VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, false) )
               {
                   setReplacementPermission( true );
                   Runtime.info("Velocimacro : allowInlineToOverride = true : VMs 
defined inline may replace previous VM definitions");
               }
               else
                  Runtime.info("Velocimacro : allowInlineToOverride = false : VMs 
defined inline may NOT replace previous VM definitions");
  +
  +            /*
  +             *  now turn on namespace handling as far as permissions allow in the 
manager, and also set it
  +             *  here for gating purposes
  +             */
  +           
  +            vmManager_.setNamespaceUsage( true );
  +
  +            if (Runtime.getBoolean(  VM_PERM_INLINE_LOCAL, false ))
  +            {
  +                setTemplateLocal( true );
  +                Runtime.info("Velocimacro : allowInlineLocal = true : VMs defined 
inline will be local to their defining template only.");
  +            }
  +            else
  +                Runtime.info("Velocimacro : allowInlineLocal = false : VMs defined 
inline will be  global in scope if allowed.");
  + 
           }
   
           Runtime.info("Velocimacro initialized.");
           return;
       }
  -    
  -    /**
  -     *   sets the permission to add new macros
  -     */
  -    private boolean setAddMacroPermission( boolean bAddNewAllowed )
  -    {
  -        boolean b = bAddNewAllowed_;
  -        
  -        bAddNewAllowed_ = bAddNewAllowed;
  -        return b;
  -    }
  -
  -    /**
  -     *    sets the permission for allowing addMacro() calls to 
  -     *    replace existing VM's
  -     */
  -    private boolean setReplacementPermission( boolean bReplacementAllowed )
  -    {
  -        boolean b = bReplaceAllowed_;
  -        
  -        bReplaceAllowed_ = bReplacementAllowed;
  -        return b;
  -    }
   
       /**
  -     *   adds a macro to the factory.  Lots of room for improvement here...
  +     *   adds a macro to the factory. 
        */
       public boolean addVelocimacro( String strName, String strMacro, String 
strArgArray[], String strMacroArray[], 
                                      TreeMap tmArgIndexMap, String strSourceTemplate )
  @@ -229,29 +230,40 @@
               return false;
           
           /*
  -         *   if exists, need to see if allowed to replace
  +         *  maybe the rules should be in manager?  I dunno. It's to manage the 
namespace issues
  +         *
  +         *  first, are we allowed to add VMs at all?  This trumps all.
            */
  -        
  -        if ( isVelocimacro( strName ) && !bReplaceAllowed_ )
  -            return false;
  -        
  +
           if ( !bAddNewAllowed_ )
               return false;
  +
           /*
  -         *  ok. Just make one
  +         *  are they local in scope?  Then it is ok to add.
            */
  -        
  -        Hashtable h = new Hashtable();
  -        h.put("macroname", strName );
  -        h.put("argarray",  strArgArray );
  -        h.put("macroarray",  strMacroArray );
  -        h.put("indexmap", tmArgIndexMap );
  -        h.put("macrobody", strMacro);
  -        h.put("sourcetemplate", strSourceTemplate );
  -
  -        synchronized( obLock) {
  -            if (!isVelocimacro( strName ))
  -                hMacros_.put( strName, h );
  +
  +        if (!bTemplateLocal_  )
  +        {
  +            /* 
  +             * otherwise, if we have it already in global namespace, and they can't 
replace
  +             * since local templates are not allowed, the global namespace is 
implied.
  +             *  remember, we don't know anything about namespace managment here, so 
lets
  +             *  note do anything fancy like trying to give it the global namespace 
here
  +             *
  +             *  so if we have it, and we aren't allowed to replace, bail
  +             */
  +            
  +            if ( isVelocimacro( strName, strSourceTemplate ) && !bReplaceAllowed_ )
  +                return false;
  +        }
  +
  +        /*
  +         *  seems like all is good.  Lets do it.
  +         */
  +
  +        synchronized( this ) 
  +        {
  +            vmManager_.addVM( strName, strMacro, strArgArray, strMacroArray, 
tmArgIndexMap, strSourceTemplate );
           }
   
           return true;
  @@ -260,10 +272,15 @@
       /**
        *   tells the world if a given directive string is a Velocimacro
        */
  -    public boolean isVelocimacro( String vm )
  +    public boolean isVelocimacro( String vm , String strSourceTemplate )
       {
  -        synchronized( obLock ) {
  -            if (hMacros_.get( vm ) != null)
  +        synchronized( this ) 
  +        {
  +            /*
  +             *  first we check the locals to see if we have a local definition for 
this template
  +             */
  +            
  +            if (vmManager_.get( vm, strSourceTemplate ) != null)
                   return true;
           }
   
  @@ -275,24 +292,14 @@
        *  behave correctly wrt getting the framework to 
        *  dig out the correct # of args
        */
  -    public Directive getVelocimacro( String strVMName )
  +    public Directive getVelocimacro( String strVMName, String strSourceTemplate )
       {
  -        synchronized( obLock ) 
  +        synchronized( this ) 
           {
  -            if ( isVelocimacro( strVMName ) ) 
  -                {    
  -                    Hashtable h = (Hashtable) hMacros_.get( strVMName );
  -                    
  -                    VelocimacroProxy vp = new VelocimacroProxy();
  -                     
  -                    vp.setName( (String) h.get("macroname"));
  -                    vp.setArgArray(  (String []) h.get("argarray") ); 
  -                    vp.setMacroArray( (String [] ) h.get("macroarray"));
  -                    vp.setArgIndexMap( (TreeMap) h.get("indexmap"));
  -                    vp.setMacrobody( (String) h.get("macrobody"));
  -
  -                    return vp;
  -                }
  +            if ( isVelocimacro( strVMName, strSourceTemplate ) ) 
  +            {    
  +                return  vmManager_.get( strVMName, strSourceTemplate );
  +            }
           }
   
           /*
  @@ -301,6 +308,49 @@
           
           return null;
       }
  +
  +    /**
  +     *  tells the vmManager to dump the specified namespace
  +     */
  +    public boolean dumpVMNamespace( String strNamespace )
  +    {
  +        return vmManager_.dumpNamespace( strNamespace );
  +    }
  +
  +    /**
  +     *  sets permission to have VMs local in scope to their declaring template
  +     *  note that this is really taken care of in the VMManager class, but
  +     *  we need it here for gating purposes in addVM
  +     *  eventually, I will slide this all into the manager, maybe.
  +     */   
  +    private void setTemplateLocal( boolean b )
  +    {
  +        bTemplateLocal_ = b;
  +    }
  +
  +    /**
  +     *   sets the permission to add new macros
  +     */
  +    private boolean setAddMacroPermission( boolean bAddNewAllowed )
  +    {
  +        boolean b = bAddNewAllowed_;
  +        
  +        bAddNewAllowed_ = bAddNewAllowed;
  +        return b;
  +    }
  +
  +    /**
  +     *    sets the permission for allowing addMacro() calls to 
  +     *    replace existing VM's
  +     */
  +    private boolean setReplacementPermission( boolean bReplacementAllowed )
  +    {
  +        boolean b = bReplaceAllowed_;
  +        
  +        bReplaceAllowed_ = bReplacementAllowed;
  +        return b;
  +    }
  +
   }
   
   
  
  
  

Reply via email to