You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Marc DEXET <Ma...@dsi.cnrs.fr> on 2004/10/27 12:22:36 UTC

RE : [Jelly] How to create and use custom scope with standard tag libr ary ?

Marc DEXET wrote:

Quickly tested : what about this ?
Replace HashMap by Hashtable maybe.

Index: JellyContext.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/Jell
yContext.java,v
retrieving revision 1.61
diff -u -r1.61 JellyContext.java
--- JellyContext.java	9 Sep 2004 15:10:03 -0000	1.61
+++ JellyContext.java	27 Oct 2004 10:16:00 -0000
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Map;
@@ -192,8 +193,8 @@
     public JellyContext getScope(String name) {
         if ( "parent".equals( name ) ) {
             return getParent();
-        }
-        return null;
+        } 
+        return (JellyContext) variables.get(name);
     }
 
     /**
@@ -213,9 +214,9 @@
         if ( answer == null && parent != null ) {
             answer = parent.findVariable(name);
         }
-        // ### this is a hack - remove this when we have support for
pluggable Scopes
+        // ### this is a hack - remove this when we have support for
pluggable Scopes  	
         if ( answer == null ) {
-            answer = getSystemProperty(name);
+        		answer = getSystemProperty(name);	
         }
 
         if (log.isDebugEnabled()) {
@@ -240,9 +241,28 @@
         }
 
         // ### this is a hack - remove this when we have support for
pluggable Scopes
+        /**
+         * @author mde
+         * @version proposition
+         */
+        
         if ( value == null ) {
-            value = getSystemProperty(name);
-        }
+        	int scopeEnd = name.indexOf('.');
+        	
+        	if ( scopeEnd != -1 && scopeEnd > 4 ) {
+        		String scopeCandidate = name.substring(0,scopeEnd);
+        		
+        		if ( scopeCandidate.endsWith("Scope") &&
variables.containsKey(scopeCandidate) ) {
+        			String varName = name.substring(scopeEnd+1);
+        			value = (
(JellyContext)variables.get(scopeCandidate) ).getVariable(varName);
+        		}
+        		
+                if ( value == null ) {
+                    value = getSystemProperty(name);
+                }
+        		
+        	}
+        }        	
 
         return value;
     }
@@ -377,6 +397,20 @@
     public JellyContext newJellyContext() {
         return createChildContext();
     }
+    
+    /**
+     * A factory method to create a new child context of the
+     * current context with a new scope.
+     */
+    public JellyContext newJellyContext(String scopeName) {
+    	Map newVariables = new HashMap();
+        newVariables.put("parentScope", variables);
+        JellyContext answer = createChildContext();
+        answer.setVariables(newVariables);    	
+        answer.getVariables().put(scopeName, answer);
+        return answer;
+        
+    }    
 
     /** Registers the given tag library against the given namespace URI.
      * This should be called before the parser is used.