Esempio n. 1
0
        public static bool Evaluate(IContext context, TextWriter writer, string logTag, TextReader reader)
        {
            SimpleNode simpleNode = null;

            try
            {
                simpleNode = RuntimeSingleton.Parse(reader, logTag);
            }
            catch (ParseException ex)
            {
                throw new ParseErrorException(ex.Message, ex);
            }
            bool result;

            if (simpleNode != null)
            {
                InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(context);
                internalContextAdapterImpl.PushCurrentTemplateName(logTag);
                try
                {
                    try
                    {
                        simpleNode.Init(internalContextAdapterImpl, RuntimeSingleton.RuntimeServices);
                    }
                    catch (System.Exception ex2)
                    {
                        RuntimeSingleton.Error(string.Concat(new object[]
                        {
                            "Velocity.evaluate() : init exception for tag = ",
                            logTag,
                            " : ",
                            ex2
                        }));
                    }
                    simpleNode.Render(internalContextAdapterImpl, writer);
                }
                finally
                {
                    internalContextAdapterImpl.PopCurrentTemplateName();
                }
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
Esempio n. 2
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="writer"> 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, String logTag, TextReader reader)
        {
            SimpleNode nodeTree = null;

            try
            {
                nodeTree = RuntimeSingleton.Parse(reader, logTag);
            }
            catch (ParseException parseException)
            {
                throw new ParseErrorException(parseException.Message, parseException);
            }

            // now we want to init and render
            if (nodeTree != null)
            {
                InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(context);

                internalContextAdapterImpl.PushCurrentTemplateName(logTag);

                try
                {
                    try
                    {
                        nodeTree.Init(internalContextAdapterImpl, RuntimeSingleton.RuntimeServices);
                    }
                    catch (Exception exception)
                    {
                        RuntimeSingleton.Error(
                            string.Format("Velocity.evaluate() : init exception for tag = {0} : {1}", logTag, exception));
                    }

                    // now render, and let any exceptions fly
                    nodeTree.Render(internalContextAdapterImpl, writer);
                }
                finally
                {
                    internalContextAdapterImpl.PopCurrentTemplateName();
                }

                return(true);
            }

            return(false);
        }