You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/07/29 02:17:21 UTC

svn commit: r680578 - /velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java

Author: nbubna
Date: Mon Jul 28 17:17:19 2008
New Revision: 680578

URL: http://svn.apache.org/viewvc?rev=680578&view=rev
Log:
refactor for clarity, no meaningful changes

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=680578&r1=680577&r2=680578&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java Mon Jul 28 17:17:19 2008
@@ -179,70 +179,48 @@
             throws IOException, ResourceNotFoundException,
             ParseErrorException, MethodInvocationException
     {
-        VelocimacroProxy vmProxy = null;
-        
-        /**
-         * first look in the source template
-         */
-        Object o = rsvc.getVelocimacro(macroName, sourceTemplate);
-        if (o != null && o instanceof VelocimacroProxy)
+        VelocimacroProxy vmProxy = getProxy(context);
+        if (vmProxy == null)
         {
-            vmProxy = (VelocimacroProxy)o;
+            /**
+             * If we cannot find an implementation write the literal text
+             */
+            writer.write(literal);
+            return true;
         }
 
         /**
-         * if not found, look in the macro libraries.
+         * init and render the proxy
+         * is the init call always necessary?
+         * if so, why are we using this.context instead of context?
          */
-        if (vmProxy == null)
-        {
-            List macroLibraries = context.getMacroLibraries();
-            if (macroLibraries != null)
-            {
-                for (int i = macroLibraries.size() - 1; i >= 0; i--)
-                {
-                    o = rsvc.getVelocimacro(macroName,
-                            (String)macroLibraries.get(i));
-
-                    /**
-                     * Get the first matching macro
-                     */
-                    if (o != null && o instanceof VelocimacroProxy)
-                    {
-                        vmProxy = (VelocimacroProxy) o;
-                        break;
-                    }
-                }
-            }
-        }
-
-        if (vmProxy != null)
+        synchronized (vmProxy)
         {
-            /**
-             * initialize the macro if necessary
-             */
             try
             {
-                synchronized (vmProxy)
-                {
-                    vmProxy.init(rsvc, this.context, this.node);
-                    /**
-                     * render it
-                     */
-                    return vmProxy.render(context, writer, node);
-                }
+                vmProxy.init(rsvc, this.context, this.node);
             }
             catch (TemplateInitException die)
             {
-                Info info = new Info( sourceTemplate, node.getLine(), node.getColumn() );
-
+                Info info = new Info(sourceTemplate, node.getLine(), node.getColumn());
                 throw new ParseErrorException(die.getMessage(), info);
             }
+            return vmProxy.render(context, writer, node);
         }
+    }
 
-        /**
-         * If we cannot find an implementation write the literal text
-         */
-        writer.write(literal);
-        return true;
+    private VelocimacroProxy getProxy(InternalContextAdapter context)
+    {
+        Object vm = rsvc.getVelocimacro(macroName, sourceTemplate);
+        if (vm == null && context.getMacroLibraries() != null)
+        {
+            List libs = context.getMacroLibraries();
+            for (int i = libs.size()-1; vm == null && i >= 0; i--)
+            {
+                vm = rsvc.getVelocimacro(macroName, (String)libs.get(i));
+            }
+        }
+        return (VelocimacroProxy)vm;
     }
+
 }
\ No newline at end of file