/// <summary> /// Invokes a currently registered Velocimacro with the parms provided /// and places the rendered stream into the writer. /// <br> /// 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 static bool InvokeVelocimacro(System.String vmName, System.String logTag, System.String[] params_Renamed, IContext context, TextWriter writer) { /* * check parms */ if (vmName == null || params_Renamed == 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("Velocity.invokeVelocimacro() : VM '" + vmName + "' not registered."); return(false); } /* * now just create the VM call, and use evaluate */ System.Text.StringBuilder construct = new System.Text.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 (System.Exception e) { RuntimeSingleton.error("Velocity.invokeVelocimacro() : error " + e); } return(false); }
/// <summary> /// merges a template and puts the rendered stream into the writer /// </summary> /// <param name="templateName">name of template to be used in merge /// </param> /// <param name="encoding">encoding used in template /// </param> /// <param name="context"> filled context to be used in merge /// </param> /// <param name="writer"> writer to write template into /// </param> /// <returns>true if successful, false otherwise. Errors /// logged to velocity log /// @since Velocity v1.1 /// </returns> public static bool MergeTemplate(System.String templateName, System.String encoding, IContext context, TextWriter writer) { Template template = RuntimeSingleton.getTemplate(templateName, encoding) ; if (template == null) { RuntimeSingleton.error("Velocity.parseTemplate() failed loading template '" + templateName + "'"); return(false); } else { template.Merge(context, writer); return(true); } }
/// <summary> /// Renders the input reader using the context into the output writer. /// To be used when a template is dynamically constructed, or want to /// use Velocity as a token replacer. /// </summary> /// <param name="context">context to use in rendering input string</param> /// <param name="out"> Writer in which to render the output</param> /// <param name="logTag"> string to be used as the template name for log messages in case of error</param> /// <param name="reader">Reader containing the VTL to be rendered</param> /// <returns>true if successful, false otherwise. If false, see Velocity runtime log</returns> public static bool Evaluate(IContext context, TextWriter writer, System.String logTag, TextReader reader) { SimpleNode nodeTree = null; try { nodeTree = RuntimeSingleton.parse(reader, logTag); } catch (ParseException pex) { throw new ParseErrorException(pex.Message); } /* * now we want to init and render */ if (nodeTree != null) { InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context); ica.PushCurrentTemplateName(logTag); try { try { nodeTree.init(ica, RuntimeSingleton.RuntimeServices); } catch (System.Exception e) { RuntimeSingleton.error("Velocity.evaluate() : init exception for tag = " + logTag + " : " + e); } /* * now render, and let any exceptions fly */ nodeTree.render(ica, writer); } finally { ica.PopCurrentTemplateName(); } return(true); } return(false); }
/// <summary> /// Log an error message. /// </summary> /// <param name="Object">message to log</param> public static void Error(System.Object message) { RuntimeSingleton.error(message); }