Exemplo n.º 1
0
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="vmName">name of Velocimacro to call
        /// </param>
        /// <param name="logTag">string to be used for template name in case of error
        /// </param>
        /// <param name="params[]">args used to invoke Velocimacro. In context key format :
        /// eg  "foo","bar" (rather than "$foo","$bar")
        /// </param>
        /// <param name="context">Context object containing data/objects used for rendering.
        /// </param>
        /// <param name="writer"> Writer for output stream
        /// </param>
        /// <returns>true if Velocimacro exists and successfully invoked, false otherwise.
        /// </returns>
        public bool InvokeVelocimacro(String vmName, String logTag, String[] params_Renamed, IContext context, TextWriter writer)
        {
            /*
             *  check parms
             */
            if (vmName == null || params_Renamed == null || context == null || writer == null || logTag == null)
            {
                ri.error("VelocityEngine.invokeVelocimacro() : invalid parameter");
                return(false);
            }

            /*
             * does the VM exist?
             */
            if (!ri.isVelocimacro(vmName, logTag))
            {
                ri.error("VelocityEngine.invokeVelocimacro() : VM '" + vmName + "' not registered.");
                return(false);
            }

            /*
             *  now just create the VM call, and use evaluate
             */
            StringBuilder construct = new StringBuilder("#");

            construct.Append(vmName);
            construct.Append("(");

            for (int i = 0; i < params_Renamed.Length; i++)
            {
                construct.Append(" $");
                construct.Append(params_Renamed[i]);
            }

            construct.Append(" )");

            try
            {
                bool retval = Evaluate(context, writer, logTag, construct.ToString());

                return(retval);
            }
            catch (Exception e)
            {
                ri.error("VelocityEngine.invokeVelocimacro() : error " + e);
                throw e;
            }
        }