This is the Runtime system for Velocity. It is the single access point for all functionality in Velocity. It adheres to the mediator pattern and is the only structure that developers need to be familiar with in order to get Velocity to perform. The Runtime will also cooperate with external systems like Turbine. Runtime properties can set and then the Runtime is initialized. Turbine for example knows where the templates are to be loaded from, and where the velocity log file should be placed. So in the case of Velocity cooperating with Turbine the code might look something like the following:
 RuntimeSingleton.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath); RuntimeSingleton.setProperty(RuntimeConstants.RUNTIME_LOG, pathToVelocityLog); RuntimeSingleton.init(); 
 ----------------------------------------------------------------------- N O T E S  O N  R U N T I M E  I N I T I A L I Z A T I O N ----------------------------------------------------------------------- RuntimeSingleton.init() If Runtime.init() is called by itself the Runtime will initialize with a set of default values. ----------------------------------------------------------------------- RuntimeSingleton.init(String/Properties) In this case the default velocity properties are layed down first to provide a solid base, then any properties provided in the given properties object will override the corresponding default property. ----------------------------------------------------------------------- 
Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Renders the input stream 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="instream">input stream containing the VTL to be rendered
        /// </param>
        /// <returns>true if successful, false otherwise.  If false, see
        /// Velocity runtime log
        /// </returns>
        /// <deprecated>Use
        /// {@link #evaluate( Context context, Writer writer,
        /// String logTag, Reader reader ) }
        /// </deprecated>
        public static bool Evaluate(IContext context, TextWriter writer, System.String logTag, Stream instream)
        {
            /*
             *  first, parse - convert ParseException if thrown
             */

            TextReader reader = null;

            System.String encoding = null;

            try {
                encoding = RuntimeSingleton.getString(NVelocity.Runtime.RuntimeConstants_Fields.INPUT_ENCODING, NVelocity.Runtime.RuntimeConstants_Fields.ENCODING_DEFAULT);
                reader   = new StreamReader(new StreamReader(instream, System.Text.Encoding.GetEncoding(encoding)).BaseStream);
            } catch (IOException uce) {
                System.String msg = "Unsupported input encoding : " + encoding + " for template " + logTag;
                throw new ParseErrorException(msg);
            }

            return(Evaluate(context, writer, logTag, reader));
        }
Esempio n. 4
0
        /// <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);
        }
Esempio n. 5
0
 /// <summary>
 /// initialize the Velocity runtime engine, using default properties
 /// plus the properties in the passed in java.util.Properties object
 /// </summary>
 /// <param name="p">
 /// Proprties object containing initialization properties
 /// </param>
 public static void Init(ExtendedProperties p)
 {
     RuntimeSingleton.init(p);
 }
Esempio n. 6
0
 /// <summary>
 /// initialize the Velocity runtime engine, using default properties
 /// plus the properties in the properties file passed in as the arg
 /// </summary>
 /// <param name="propsFilename">
 /// file containing properties to use to initialize
 /// the Velocity runtime
 /// </param>
 public static void Init(System.String propsFilename)
 {
     RuntimeSingleton.init(propsFilename);
 }
Esempio n. 7
0
 /// <summary>
 /// Log a debug message.
 /// </summary>
 /// <param name="Object">message to log</param>
 public static void Debug(System.Object message)
 {
     RuntimeSingleton.debug(message);
 }
Esempio n. 8
0
 /// <summary>
 /// Log an error message.
 /// </summary>
 /// <param name="Object">message to log</param>
 public static void Error(System.Object message)
 {
     RuntimeSingleton.error(message);
 }
Esempio n. 9
0
 /// <summary>
 /// Log an info message.
 /// </summary>
 /// <param name="Object">message to log</param>
 public static void Info(System.Object message)
 {
     RuntimeSingleton.info(message);
 }
Esempio n. 10
0
 /// <summary>
 /// Log a warning message.
 /// </summary>
 /// <param name="Object">message to log
 /// </param>
 public static void Warn(System.Object message)
 {
     RuntimeSingleton.warn(message);
 }
Esempio n. 11
0
 /// <summary>
 /// initialize the NVelocity runtime engine, using the default
 /// properties of the NVelocity distribution
 /// </summary>
 public static void Init()
 {
     RuntimeSingleton.init();
 }
Esempio n. 12
0
 /// <summary>
 /// Returns a <code>Template</code> from the Velocity
 /// resource management system.
 /// </summary>
 /// <param name="name">The file name of the desired template.
 /// </param>
 /// <param name="encoding">The character encoding to use for the template.
 /// </param>
 /// <returns>    The template.
 /// @throws ResourceNotFoundException if template not found
 /// from any available source.
 /// @throws ParseErrorException if template cannot be parsed due
 /// to syntax (or other) error.
 /// @throws Exception if an error occurs in template initialization
 /// @since Velocity v1.1
 /// </returns>
 public static Template GetTemplate(System.String name, System.String encoding)
 {
     return(RuntimeSingleton.getTemplate(name, encoding));
 }
Esempio n. 13
0
 /// <summary>
 /// Returns a <code>Template</code> from the Velocity
 /// resource management system.
 /// </summary>
 /// <param name="name">The file name of the desired template.
 /// </param>
 /// <returns>    The template.
 /// @throws ResourceNotFoundException if template not found
 /// from any available source.
 /// @throws ParseErrorException if template cannot be parsed due
 /// to syntax (or other) error.
 /// @throws Exception if an error occurs in template initialization
 /// </returns>
 public static Template GetTemplate(System.String name)
 {
     return(RuntimeSingleton.getTemplate(name));
 }
Esempio n. 14
0
 /// <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="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.
 /// </returns>
 /// <deprecated>Use
 /// {@link #mergeTemplate( String templateName, String encoding,
 /// Context context, Writer writer )}
 /// </deprecated>
 public static bool MergeTemplate(System.String templateName, IContext context, TextWriter writer)
 {
     return(MergeTemplate(templateName, RuntimeSingleton.getString(NVelocity.Runtime.RuntimeConstants_Fields.INPUT_ENCODING, NVelocity.Runtime.RuntimeConstants_Fields.ENCODING_DEFAULT), context, writer));
 }
Esempio n. 15
0
 /// <summary>
 /// Add a Velocity Runtime property.
 /// </summary>
 /// <param name="String">key</param>
 /// <param name="Object">value</param>
 public static void  AddProperty(System.String key, System.Object value_Renamed)
 {
     RuntimeSingleton.addProperty(key, value_Renamed);
 }
Esempio n. 16
0
 /// <summary>
 /// Determines if a template is accessable via the currently
 /// configured resource loaders.
 /// <br><br>
 /// Note that the current implementation will <b>not</b>
 /// change the state of the system in any real way - so this
 /// cannot be used to pre-load the resource cache, as the
 /// previous implementation did as a side-effect.
 /// <br><br>
 /// The previous implementation exhibited extreme lazyness and
 /// sloth, and the author has been flogged.
 /// </summary>
 /// <param name="templateName"> name of the temlpate to search for
 /// </param>
 /// <returns>true if found, false otherwise
 /// </returns>
 public static bool TemplateExists(System.String templateName)
 {
     return(RuntimeSingleton.getLoaderNameForResource(templateName) != null);
 }
Esempio n. 17
0
 /// <summary>
 /// Clear a NVelocity Runtime property.
 /// </summary>
 /// <param name="key">of property to clear</param>
 public static void ClearProperty(System.String key)
 {
     RuntimeSingleton.clearProperty(key);
 }
Esempio n. 18
0
 /// <summary>
 /// Get a Velocity Runtime property.
 /// </summary>
 /// <param name="key">property to retrieve</param>
 /// <returns>property value or null if the property not currently set</returns>
 public static System.Object GetProperty(System.String key)
 {
     return(RuntimeSingleton.getProperty(key));
 }