velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r547365 - in /velocity/engine/trunk/src/java/org/apache/velocity: app/Velocity.java app/VelocityEngine.java runtime/RuntimeInstance.java runtime/RuntimeServices.java
Date Thu, 14 Jun 2007 19:03:50 GMT
Author: nbubna
Date: Thu Jun 14 12:03:49 2007
New Revision: 547365

URL: http://svn.apache.org/viewvc?view=rev&rev=547365
Log:
invokeVelocimacro() code was largely duplicated in both Velocity and VelocityEngine
 - move invokeVelocimacro() code to RuntimeInstance where it can be shared
 - make miscellaneous minor improvements to the base implementation

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java
    velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java?view=diff&rev=547365&r1=547364&r2=547365
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java Thu Jun 14 12:03:49
2007
@@ -268,92 +268,34 @@
     }
 
     /**
-     *  Invokes a currently registered Velocimacro with the parms provided
-     *  and places the rendered stream into the writer.
+     * Invokes a currently registered Velocimacro with the params provided
+     * and places the rendered stream into the writer.
+     * <br>
+     * Note : currently only accepts args to the VM if they are in the context.
      *
-     *  Note : currently only accepts args to the VM if they are in the context.
-     *
-     *  @param vmName name of Velocimacro to call
-     *  @param logTag string to be used for template name in case of error
-     *  @param params args used to invoke Velocimacro. In context key format :
-     *                  eg  "foo","bar" (rather than "$foo","$bar")
-     *  @param context Context object containing data/objects used for rendering.
-     *  @param writer  Writer for output stream
-     *  @return true if Velocimacro exists and successfully invoked, false otherwise.
+     * @param vmName name of Velocimacro to call
+     * @param logTag string to be used for template name in case of error. if null,
+     *               the vmName will be used
+     * @param params keys for args used to invoke Velocimacro, in java format
+     *               rather than VTL (eg  "foo" or "bar" rather than "$foo" or "$bar")
+     * @param context Context object containing data/objects used for rendering.
+     * @param writer  Writer for output stream
+     * @return true if Velocimacro exists and successfully invoked, false otherwise.
      */
     public static  boolean invokeVelocimacro( String vmName, String logTag,
                                               String params[], Context context,
                                               Writer writer )
     {
-        /*
-         *  check parms
-         */
-
-        if ( vmName == null ||  params == null ||  context == null
-             || writer == null || logTag == null)
-        {
-            getLog().error("Velocity.invokeVelocimacro() : invalid parameter");
-            return false;
-        }
-
-        /*
-         * does the VM exist?
-         */
-
-        if (!RuntimeSingleton.isVelocimacro( vmName, logTag ))
-        {
-            getLog().error("Velocity.invokeVelocimacro() : VM '"+ vmName
-                           + "' not registered.");
-            return false;
-        }
-
-        /*
-         *  now just create the VM call, and use evaluate
-         */
-
-        StringBuffer construct = new StringBuffer("#");
-
-        construct.append( vmName );
-        construct.append( "(" );
-
-        for( int i = 0; i < params.length; i++)
-        {
-            construct.append( " $" );
-            construct.append( params[i] );
-        }
-
-        construct.append(" )");
-
         try
         {
-            return evaluate(  context,  writer,
-                                         logTag, construct.toString() );
+            return RuntimeSingleton.getRuntimeServices()
+                .invokeVelocimacro(vmName, logTag, params, context, writer);
         }
-
-        catch(ParseErrorException pee)
-        {
-            throw pee;
-        }
-        catch(MethodInvocationException mie)
-        {
-            throw mie;
-        }
-        catch(ResourceNotFoundException rnfe)
-        {
-            throw rnfe;
-        }
-        catch(IOException ioe)
+        catch (IOException ioe)
         {
             getLog().error("Velocity.invokeVelocimacro() failed", ioe);
+            return false;
         }
-        /**
-         * pass through application level runtime exceptions
-         */
-        catch(RuntimeException re)
-        {
-            throw re;
-        }
-        return false;
     }
 
     /**

Modified: velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java?view=diff&rev=547365&r1=547364&r2=547365
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java Thu Jun 14
12:03:49 2007
@@ -45,18 +45,12 @@
  * is using the Velocity class which employs the singleton
  * model.
  * </p>
- *
- * <p>
- * Please ensure that you call one of the init() variants.
- * This is critical for proper behavior.
- * </p>
- *
- * <p> Coming soon : Velocity will call
+ * <p>Velocity will call
  * the parameter-less init() at the first use of this class
  * if the init() wasn't explicitly called.  While this will
- * ensure that Velocity functions, it almost certainly won't
- * function in the way you intend, so please make sure to
- * call init().
+ * ensure that Velocity functions, it probably won't
+ * function in the way you intend, so it is strongly recommended that
+ * you call an init() method yourself if you use the default constructor.
  * </p>
  *
  * @version $Id$
@@ -300,83 +294,27 @@
 
 
     /**
-     *  Invokes a currently registered Velocimacro with the parms provided
-     *  and places the rendered stream into the writer.
-     *
-     *  Note : currently only accepts args to the VM if they are in the context.
-     *
-     *  @param vmName name of Velocimacro to call
-     *  @param logTag string to be used for template name in case of error
-     *  @param params args used to invoke Velocimacro. In context key format :
-     *                  eg  "foo","bar" (rather than "$foo","$bar")
-     *  @param context Context object containing data/objects used for rendering.
-     *  @param writer  Writer for output stream
-     *  @return true if Velocimacro exists and successfully invoked, false otherwise.
-     * @throws Exception
+     * Invokes a currently registered Velocimacro with the params provided
+     * and places the rendered stream into the writer.
+     * <br>
+     * Note : currently only accepts args to the VM if they are in the context.
+     *
+     * @param vmName name of Velocimacro to call
+     * @param logTag string to be used for template name in case of error. if null,
+     *               the vmName will be used
+     * @param params keys for args used to invoke Velocimacro, in java format
+     *               rather than VTL (eg  "foo" or "bar" rather than "$foo" or "$bar")
+     * @param context Context object containing data/objects used for rendering.
+     * @param writer  Writer for output stream
+     * @return true if Velocimacro exists and successfully invoked, false otherwise.
+     * @throws IOException While rendering to the writer, an I/O problem occured.
      */
     public boolean invokeVelocimacro( String vmName, String logTag,
                                               String params[], Context context,
                                               Writer writer )
         throws Exception
     {
-        /*
-         *  check parms
-         */
-
-        if ( vmName == null ||  params == null ||  context == null
-             || writer == null || logTag == null)
-        {
-            getLog().error("VelocityEngine.invokeVelocimacro() : invalid parameter");
-            return false;
-        }
-
-        /*
-         * does the VM exist?
-         */
-
-        if (!ri.isVelocimacro( vmName, logTag ))
-        {
-            getLog().error("VelocityEngine.invokeVelocimacro() : VM '" + vmName
-                           + "' not registered.");
-            return false;
-        }
-
-        /*
-         *  now just create the VM call, and use evaluate
-         */
-
-        StringBuffer construct = new StringBuffer("#");
-
-        construct.append( vmName );
-        construct.append( "(" );
-
-        for( int i = 0; i < params.length; i++)
-        {
-            construct.append( " $" );
-            construct.append( params[i] );
-        }
-
-        construct.append(" )");
-
-        try
-        {
-            boolean retval = evaluate(  context,  writer,
-                                         logTag, construct.toString() );
-
-            return retval;
-        }
-        /**
-         * pass through application level runtime exceptions
-         */
-        catch( RuntimeException e )
-        {
-            throw e;
-        }
-        catch(Exception e)
-        {
-            getLog().error("VelocityEngine.invokeVelocimacro() : error ", e);
-            throw e;
-        }
+        return ri.invokeVelocimacro(vmName, logTag, params, context, writer);
     }
 
     /**

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?view=diff&rev=547365&r1=547364&r2=547365
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java Thu Jun
14 12:03:49 2007
@@ -1233,6 +1233,66 @@
     }
 
     /**
+     * Invokes a currently registered Velocimacro with the params provided
+     * and places the rendered stream into the writer.
+     * <br>
+     * Note : currently only accepts args to the VM if they are in the context.
+     *
+     * @param vmName name of Velocimacro to call
+     * @param logTag string to be used for template name in case of error. if null,
+     *               the vmName will be used
+     * @param params keys for args used to invoke Velocimacro, in java format
+     *               rather than VTL (eg  "foo" or "bar" rather than "$foo" or "$bar")
+     * @param context Context object containing data/objects used for rendering.
+     * @param writer  Writer for output stream
+     * @return true if Velocimacro exists and successfully invoked, false otherwise.
+     * @throws IOException While rendering to the writer, an I/O problem occured.
+     */
+    public boolean invokeVelocimacro(final String vmName, String logTag,
+                                     String[] params, final Context context,
+                                     final Writer writer)
+        throws IOException
+    {
+        /* check necessary parameters */
+        if (vmName == null || context == null || writer == null)
+        {
+            getLog().error("RuntimeInstance.invokeVelocimacro() : invalid call : vmName,
context, and writer must not be null");
+            return false;
+        }
+
+        /* handle easily corrected parameters */
+        if (logTag == null)
+        {
+            logTag = vmName;
+        }
+        if (params == null)
+        {
+            params = new String[0];
+        }
+
+        /* does the VM exist? */
+        if (!isVelocimacro(vmName, logTag))
+        {
+            getLog().error("RuntimeInstance.invokeVelocimacro() : VM '" + vmName
+                           + "' is not registered.");
+            return false;
+        }
+
+        /* now just create the VM call, and use evaluate */
+        StringBuffer template = new StringBuffer("#");
+        template.append(vmName);
+        template.append("(");
+        for( int i = 0; i < params.length; i++)
+        {
+            template.append(" $");
+            template.append(params[i]);
+        }
+        template.append(" )");
+
+        return evaluate(context, writer, logTag, template.toString());
+    }
+
+    /**
      * Returns a <code>Template</code> from the resource manager.
      * This method assumes that the character encoding of the
      * template is set by the <code>input.encoding</code>

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java?view=diff&rev=547365&r1=547364&r2=547365
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java Thu Jun
14 12:03:49 2007
@@ -227,6 +227,26 @@
                             String logTag, Reader reader) throws IOException;
 
     /**
+     * Invokes a currently registered Velocimacro with the params provided
+     * and places the rendered stream into the writer.
+     * <br>
+     * Note : currently only accepts args to the VM if they are in the context.
+     *
+     * @param vmName name of Velocimacro to call
+     * @param logTag string to be used for template name in case of error. if null,
+     *               the vmName will be used
+     * @param params keys for args used to invoke Velocimacro, in java format
+     *               rather than VTL (eg  "foo" or "bar" rather than "$foo" or "$bar")
+     * @param context Context object containing data/objects used for rendering.
+     * @param writer  Writer for output stream
+     * @return true if Velocimacro exists and successfully invoked, false otherwise.
+     * @throws IOException While rendering to the writer, an I/O problem occured.
+     */
+    public boolean invokeVelocimacro(final String vmName, String logTag,
+                                     String[] params, final Context context,
+                                     final Writer writer) throws IOException;
+
+    /**
      * Returns a <code>Template</code> from the resource manager.
      * This method assumes that the character encoding of the
      * template is set by the <code>input.encoding</code>



Mime
View raw message