Ejemplo n.º 1
0
        /// <summary>
        /// Invokes a currently registered Velocimacro with the parameters 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="parameters">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 static bool InvokeVelocimacro(String vmName, String logTag, String[] parameters, IContext context,
                                             TextWriter writer)
        {
            // check parameters
            if (vmName == null || parameters == null || context == null || writer == null || logTag == null)
            {
                RuntimeSingleton.Error("Velocity.invokeVelocimacro() : invalid parameter");
                return(false);
            }

            // does the VM exist?
            if (!RuntimeSingleton.IsVelocimacro(vmName, logTag))
            {
                RuntimeSingleton.Error(string.Format("Velocity.invokeVelocimacro() : VM '{0}' not registered.", vmName));
                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 < parameters.Length; i++)
            {
                construct.Append(" $");
                construct.Append(parameters[i]);
            }

            construct.Append(" )");

            try
            {
                bool retval = Evaluate(context, writer, logTag, construct.ToString());
                return(retval);
            }
            catch (Exception e)
            {
                RuntimeSingleton.Error(string.Format("Velocity.invokeVelocimacro() : error {0}", e));
            }

            return(false);
        }
Ejemplo n.º 2
0
        public static bool InvokeVelocimacro(string vmName, string logTag, string[] parameters, IContext context, TextWriter writer)
        {
            bool result;

            if (vmName == null || parameters == null || context == null || writer == null || logTag == null)
            {
                RuntimeSingleton.Error("Velocity.invokeVelocimacro() : invalid parameter");
                result = false;
            }
            else if (!RuntimeSingleton.IsVelocimacro(vmName, logTag))
            {
                RuntimeSingleton.Error("Velocity.invokeVelocimacro() : VM '" + vmName + "' not registered.");
                result = false;
            }
            else
            {
                StringBuilder stringBuilder = new StringBuilder("#");
                stringBuilder.Append(vmName);
                stringBuilder.Append("(");
                for (int i = 0; i < parameters.Length; i++)
                {
                    stringBuilder.Append(" $");
                    stringBuilder.Append(parameters[i]);
                }
                stringBuilder.Append(" )");
                try
                {
                    bool flag = Velocity.Evaluate(context, writer, logTag, stringBuilder.ToString());
                    result = flag;
                    return(result);
                }
                catch (System.Exception arg)
                {
                    RuntimeSingleton.Error("Velocity.invokeVelocimacro() : error " + arg);
                }
                result = false;
            }
            return(result);
        }