Author: danielf
Date: Mon Dec 20 14:32:17 2004
New Revision: 122893

URL: http://svn.apache.org/viewcvs?view=rev&rev=122893
Log:
Factored out iterator expressions from the invoker to a helper method. Changed 
variable context for the import tag. Before the global context rather than the 
current was used for jxpath but not for jexl. Now the current is used for both. 
After this the variable property in ExecutionContext was not needed any more.
Modified:
   
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java
   
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java
   
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java
   
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java

Modified: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java
Url: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java?view=diff&rev=122893&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r1=122892&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r2=122893
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java
      (original)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java
      Mon Dec 20 14:32:17 2004
@@ -277,7 +277,7 @@
             throws SAXException {
         cocoon.put("consumer", consumer);
         Invoker.execute(this.xmlConsumer, new ExecutionContext(jexlContext,
-                jxpathContext, this.variables, this.definitions), null,
+                jxpathContext, this.definitions), null,
                 startEvent, null, scriptManager);
     }
 
@@ -322,4 +322,4 @@
         }
         return null;
     }
-}
\ No newline at end of file
+}

Modified: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java
Url: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java?view=diff&rev=122893&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r1=122892&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r2=122893
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java
     (original)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java
     Mon Dec 20 14:32:17 2004
@@ -19,19 +19,16 @@
 
 import org.apache.cocoon.template.jxtg.expression.MyJexlContext;
 import org.apache.commons.jxpath.JXPathContext;
-import org.apache.commons.jxpath.Variables;
 
 public class ExecutionContext {
     private MyJexlContext jexlContext;
     private JXPathContext jxpathContext;
-    private Variables variables;
     private Map definitions;
 
     public ExecutionContext(MyJexlContext jexlContext,
-            JXPathContext jxpathContext, Variables variables, Map definitions) 
{
+            JXPathContext jxpathContext, Map definitions) {
         this.jexlContext = jexlContext;
         this.jxpathContext = jxpathContext;
-        this.variables = variables;
         this.definitions = definitions;
     }
 
@@ -43,17 +40,12 @@
         return this.jxpathContext;
     }
 
-    public Variables getVariables() {
-        return this.variables;
-    }
-
     public Map getDefinitions() {
         return this.definitions;
     }
 
     public ExecutionContext getChildContext(MyJexlContext jexlContext,
             JXPathContext jxpathContext) {
-        return new ExecutionContext(jexlContext, jxpathContext, this.variables,
-                this.definitions);
+        return new ExecutionContext(jexlContext, jxpathContext, 
this.definitions);
     }
-}
\ No newline at end of file
+}

Modified: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java
Url: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java?view=diff&rev=122893&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java&r1=122892&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java&r2=122893
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java
  (original)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ValueHelper.java
  Mon Dec 20 14:32:17 2004
@@ -21,19 +21,52 @@
 import java.util.List;
 import java.util.Locale;
 
+import org.apache.cocoon.template.jxtg.environment.JSIntrospector;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
 import org.apache.commons.jexl.Expression;
 import org.apache.commons.jexl.JexlContext;
+import org.apache.commons.jexl.util.Introspector;
+import org.apache.commons.jexl.util.introspection.Info;
 import org.apache.commons.jxpath.CompiledExpression;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.Pointer;
 import org.apache.commons.lang.StringUtils;
+import org.mozilla.javascript.NativeArray;
 import org.w3c.dom.Node;
+import org.xml.sax.Locator;
 
 /**
  */
 public class ValueHelper {
 
+    private static final Iterator EMPTY_ITER = new Iterator() {
+        public boolean hasNext() {
+            return false;
+        }
+
+        public Object next() {
+            return null;
+        }
+
+        public void remove() {
+            // EMPTY
+        }
+    };
+
+    private static final Iterator NULL_ITER = new Iterator() {
+        public boolean hasNext() {
+            return true;
+        }
+
+        public Object next() {
+            return null;
+        }
+
+        public void remove() {
+            // EMPTY
+        }
+    };
+
     public static Locale parseLocale(String locale, String variant) {
         Locale ret = null;
         String language = locale;
@@ -59,7 +92,7 @@
 
     // Hack: try to prevent JXPath from converting result to a String
     public static Object getNode(JXTExpression expr, JexlContext jexlContext, 
JXPathContext jxpathContext, Boolean lenient)
-               throws Exception {
+                throws Exception {
         try {
             Object compiled = expr.getCompiledExpression();
             if (compiled instanceof CompiledExpression) {
@@ -71,24 +104,24 @@
                     if (iter.hasNext()) {
                         Pointer first = (Pointer)iter.next();
                         if (iter.hasNext()) {
-                           List result = new LinkedList();
-                           result.add(first.getNode());
-                           boolean dom = (first.getNode() instanceof Node);
-                           while (iter.hasNext()) {
-                               Object obj = ((Pointer)iter.next()).getNode();
-                               dom = dom && (obj instanceof Node);
-                               result.add(obj);
-                           }
-                           Object[] arr;
-                           if (dom) {
-                               arr = new Node[result.size()];
-                           } else {
-                               arr = new Object[result.size()];
-                           }
-                           result.toArray(arr);
-                           return arr;
-                       }
-                       return first.getNode();                    
+                            List result = new LinkedList();
+                            result.add(first.getNode());
+                            boolean dom = (first.getNode() instanceof Node);
+                            while (iter.hasNext()) {
+                                Object obj = ((Pointer)iter.next()).getNode();
+                                dom = dom && (obj instanceof Node);
+                                result.add(obj);
+                            }
+                            Object[] arr;
+                            if (dom) {
+                                arr = new Node[result.size()];
+                            } else {
+                                arr = new Object[result.size()];
+                            }
+                            result.toArray(arr);
+                            return arr;
+                        }
+                        return first.getNode();                    
                     }
                     return null;
                 } finally {
@@ -110,6 +143,59 @@
 
     public static Object getNode(JXTExpression expr, JexlContext jexlContext, 
JXPathContext jxpathContext) throws Exception {
         return getNode(expr, jexlContext, jxpathContext, null);
+    }
+
+    public static Iterator getIterator(final JXTExpression items, JexlContext 
jexlContext, JXPathContext jxpathContext, Locator loc) throws Exception {
+        Iterator iter = null;
+        if (items != null) {
+            if (items.getCompiledExpression() instanceof CompiledExpression) {
+                CompiledExpression compiledExpression = 
+                    (CompiledExpression) items.getCompiledExpression();
+                Object val =
+                    compiledExpression.getPointer(jxpathContext, 
items.getRaw()).getNode();
+                // FIXME: workaround for JXPath bug
+                iter =
+                    val instanceof NativeArray ?
+                    new JSIntrospector.NativeArrayIterator((NativeArray) val)
+                        : compiledExpression.iteratePointers(jxpathContext);
+            } else if (items.getCompiledExpression() instanceof Expression) {
+                Expression e = (Expression) items.getCompiledExpression();
+                Object result = e.evaluate(jexlContext);
+                if (result != null) {
+                    iter = Introspector.getUberspect().getIterator(
+                                                                   result,
+                                                                   new Info(
+                                                                            
loc.getSystemId(),
+                                                                            
loc.getLineNumber(),
+                                                                            
loc.getColumnNumber()));
+                }
+                if (iter == null) {
+                    iter = EMPTY_ITER;
+                }
+            } else {
+                // literal value
+                iter = new Iterator() {
+                        Object val = items;
+                        
+                        public boolean hasNext() {
+                            return val != null;
+                        }
+                        
+                        public Object next() {
+                            Object res = val;
+                            val = null;
+                            return res;
+                        }
+                        
+                        public void remove() {
+                            // EMPTY
+                        }
+                    };
+            }
+        } else {
+            iter = NULL_ITER;
+        }
+        return iter;
     }
 
     public static Boolean getBooleanValue(JXTExpression expr, JexlContext 
jexlContext,

Modified: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java
Url: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java?view=diff&rev=122893&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r1=122892&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r2=122893
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java
   (original)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java
   Mon Dec 20 14:32:17 2004
@@ -24,7 +24,6 @@
 import org.apache.cocoon.template.jxtg.JXTemplateGenerator;
 import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
 import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
-import org.apache.cocoon.template.jxtg.environment.JSIntrospector;
 import org.apache.cocoon.template.jxtg.environment.LocatorFacade;
 import org.apache.cocoon.template.jxtg.environment.MyVariables;
 import org.apache.cocoon.template.jxtg.environment.ValueHelper;
@@ -38,17 +37,12 @@
 import org.apache.cocoon.xml.XMLUtils;
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.cocoon.xml.dom.DOMStreamer;
-import org.apache.commons.jexl.Expression;
-import org.apache.commons.jexl.util.Introspector;
-import org.apache.commons.jexl.util.introspection.Info;
-import org.apache.commons.jxpath.CompiledExpression;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.JXPathContextFactory;
 import org.apache.commons.jxpath.Pointer;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.excalibur.xml.sax.XMLizable;
-import org.mozilla.javascript.NativeArray;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.Attributes;
@@ -62,34 +56,6 @@
             .newInstance();
     private static final Attributes EMPTY_ATTRS = new AttributesImpl();
 
-    private static final Iterator EMPTY_ITER = new Iterator() {
-        public boolean hasNext() {
-            return false;
-        }
-
-        public Object next() {
-            return null;
-        }
-
-        public void remove() {
-            // EMPTY
-        }
-    };
-
-    private static final Iterator NULL_ITER = new Iterator() {
-        public boolean hasNext() {
-            return true;
-        }
-
-        public Object next() {
-            return null;
-        }
-
-        public void remove() {
-            // EMPTY
-        }
-    };
-
     public static void execute(final XMLConsumer consumer,
             ExecutionContext executionContext, StartElement macroCall,
             Event startEvent, Event endEvent, ScriptManager scriptManager)
@@ -196,63 +162,14 @@
                 }
             } else if (ev instanceof StartForEach) {
                 StartForEach startForEach = (StartForEach) ev;
-                final Object items = startForEach.getItems();
+                final JXTExpression items = startForEach.getItems();
                 Iterator iter = null;
                 int begin, end, step;
                 String var, varStatus;
                 try {
-                    if (items != null) {
-                        JXTExpression expr = (JXTExpression) items;
-                        if (expr.getCompiledExpression() instanceof 
CompiledExpression) {
-                            CompiledExpression compiledExpression = 
(CompiledExpression) expr
-                                    .getCompiledExpression();
-                            Object val = compiledExpression.getPointer(
-                                    jxpathContext, expr.getRaw()).getNode();
-                            // FIXME: workaround for JXPath bug
-                            iter = val instanceof NativeArray ? new 
JSIntrospector.NativeArrayIterator(
-                                    (NativeArray) val)
-                                    : compiledExpression
-                                            .iteratePointers(jxpathContext);
-                        } else if (expr.getCompiledExpression() instanceof 
Expression) {
-                            Expression e = (Expression) expr
-                                    .getCompiledExpression();
-                            Object result = e.evaluate(jexlContext);
-                            if (result != null) {
-                                iter = Introspector.getUberspect().getIterator(
-                                        result,
-                                        new Info(
-                                                ev.getLocation().getSystemId(),
-                                                ev.getLocation()
-                                                        .getLineNumber(), ev
-                                                        .getLocation()
-                                                        .getColumnNumber()));
-                            }
-                            if (iter == null) {
-                                iter = EMPTY_ITER;
-                            }
-                        } else {
-                            // literal value
-                            iter = new Iterator() {
-                                Object val = items;
-
-                                public boolean hasNext() {
-                                    return val != null;
-                                }
-
-                                public Object next() {
-                                    Object res = val;
-                                    val = null;
-                                    return res;
-                                }
-
-                                public void remove() {
-                                    // EMPTY
-                                }
-                            };
-                        }
-                    } else {
-                        iter = NULL_ITER;
-                    }
+                    iter = ValueHelper.getIterator(items,
+                                                   jexlContext, jxpathContext,
+                                                   ev.getLocation());
                     begin = startForEach.getBegin() == null ? 0 : ValueHelper
                             .getIntValue(startForEach.getBegin(), jexlContext,
                                     jxpathContext);
@@ -756,7 +673,7 @@
                                 .getSelect(), jexlContext, jxpathContext);
                         selectJXPath = jxpathContextFactory.newContext(null,
                                 obj);
-                        selectJXPath.setVariables(executionContext
+                        selectJXPath.setVariables(jxpathContext
                                 .getVariables());
                         selectJexl = new MyJexlContext(jexlContext);
                         JXTemplateGenerator.fillContext(obj, selectJexl);
@@ -868,4 +785,4 @@
         return node.getChildNodes();
     }
 
-}
\ No newline at end of file
+}

Reply via email to