Esempio n. 1
0
        /// <summary>  does the parameters rendering of the included file
        ///
        /// </summary>
        /// <param name="node">AST argument of type StringLiteral or Reference
        /// </param>
        /// <param name="context">valid context so we can render References
        /// </param>
        /// <param name="writer">output Writer
        /// </param>
        /// <returns> boolean success or failure.  failures are logged
        /// </returns>
        /// <exception cref="IOException">
        /// </exception>
        /// <exception cref="MethodInvocationException">
        /// </exception>
        /// <exception cref="ResourceNotFoundException">
        /// </exception>
        private bool RenderOutput(INode node, IInternalContextAdapter context, System.IO.TextWriter writer)
        {
            if (node == null)
            {
                rsvc.Log.Error("#include() null argument");
                return(false);
            }

            /*
             *  does it have a value?  If you have a null reference, then no.
             */
            object value = node.Value(context);

            if (value == null)
            {
                rsvc.Log.Error("#include() null argument");
                return(false);
            }

            /*
             *  Get the path
             */
            string sourcearg = value.ToString();

            /*
             *  check to see if the argument will be changed by the event handler
             */

            string arg = EventHandlerUtil.IncludeEvent(rsvc, context, sourcearg, context.CurrentTemplateName, Name);

            /*
             *   a null return value from the event cartridge indicates we should not
             *   input a resource.
             */
            bool blockinput = false;

            if (arg == null)
            {
                blockinput = true;
            }

            Resource resource = null;

            try
            {
                if (!blockinput)
                {
                    resource = rsvc.GetContent(arg, GetInputEncoding(context));
                }
            }
            catch (ResourceNotFoundException rnfe)
            {
                /*
                 * the arg wasn't found.  Note it and throw
                 */
                rsvc.Log.Error("#include(): cannot find resource '" + arg + "', called at " + Log.Log.FormatFileString(this));
                throw rnfe;
            }

            /**
             * pass through application level runtime exceptions
             */
            catch (System.SystemException e)
            {
                rsvc.Log.Error("#include(): arg = '" + arg + "', called at " + Log.Log.FormatFileString(this));
                throw e;
            }
            catch (System.Exception e)
            {
                string msg = "#include(): arg = '" + arg + "', called at " + Log.Log.FormatFileString(this);
                rsvc.Log.Error(msg, e);
                throw new VelocityException(msg, e);
            }


            /*
             *    note - a blocked input is still a successful operation as this is
             *    expected behavior.
             */

            if (blockinput)
            {
                return(true);
            }
            else if (resource == null)
            {
                return(false);
            }

            writer.Write((string)resource.Data);
            return(true);
        }
Esempio n. 2
0
        /// <summary>  iterates through the argument list and renders every
        /// argument that is appropriate.  Any non appropriate
        /// arguments are logged, but render() continues.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="writer">
        /// </param>
        /// <param name="node">
        /// </param>
        /// <returns> True if the directive rendered successfully.
        /// </returns>
        /// <throws>  IOException </throws>
        /// <throws>  ResourceNotFoundException </throws>
        /// <throws>  ParseErrorException </throws>
        /// <throws>  MethodInvocationException </throws>
        public override bool Render(IInternalContextAdapter context, System.IO.TextWriter writer, INode node)
        {
            /*
             * if rendering is no longer allowed (after a stop), we can safely
             * skip execution of all the parse directives.
             */
            if (!context.AllowRendering)
            {
                return(true);
            }

            /*
             *  did we Get an argument?
             */
            if (node.GetChild(0) == null)
            {
                rsvc.Log.Error("#parse() null argument");
                return(false);
            }

            /*
             *  does it have a value?  If you have a null reference, then no.
             */
            object value = node.GetChild(0).Value(context);

            if (value == null)
            {
                rsvc.Log.Error("#parse() null argument");
                return(false);
            }

            /*
             *  Get the path
             */

            string sourcearg = value.ToString();

            /*
             *  check to see if the argument will be changed by the event cartridge
             */


            string arg = EventHandlerUtil.IncludeEvent(rsvc, context, sourcearg, context.CurrentTemplateName, Name);

            /*
             *   a null return value from the event cartridge indicates we should not
             *   input a resource.
             */
            bool blockinput = false;

            if (arg == null)
            {
                blockinput = true;
            }


            if (maxDepth > 0)
            {
                /*
                 * see if we have exceeded the configured depth.
                 */
                object[] templateStack = context.TemplateNameStack;
                if (templateStack.Length >= maxDepth)
                {
                    System.Text.StringBuilder path = new System.Text.StringBuilder();
                    for (int i = 0; i < templateStack.Length; ++i)
                    {
                        path.Append(" > " + templateStack[i]);
                    }
                    rsvc.Log.Error("Max recursion depth reached (" + templateStack.Length + ')' + " File stack:" + path);
                    return(false);
                }
            }

            /*
             *  now use the Runtime resource loader to Get the template
             */

            Template t = null;

            try
            {
                if (!blockinput)
                {
                    t = rsvc.GetTemplate(arg, GetInputEncoding(context));
                }
            }
            catch (ResourceNotFoundException rnfe)
            {
                /*
                 * the arg wasn't found.  Note it and throw
                 */
                rsvc.Log.Error("#parse(): cannot find template '" + arg + "', called at " + Log.Log.FormatFileString(this));
                throw rnfe;
            }
            catch (ParseErrorException pee)
            {
                /*
                 * the arg was found, but didn't parse - syntax Error
                 *  note it and throw
                 */
                rsvc.Log.Error("#parse(): syntax error in #parse()-ed template '" + arg + "', called at " + Log.Log.FormatFileString(this));

                throw pee;
            }

            /**
             * pass through application level runtime exceptions
             */
            catch (System.SystemException e)
            {
                rsvc.Log.Error("Exception rendering #parse(" + arg + ") at " + Log.Log.FormatFileString(this));
                throw e;
            }
            catch (System.Exception e)
            {
                string msg = "Exception rendering #parse(" + arg + ") at " + Log.Log.FormatFileString(this);
                rsvc.Log.Error(msg, e);
                throw new VelocityException(msg, e);
            }

            /**
             * Add the template name to the macro libraries list
             */
            System.Collections.IList macroLibraries = context.MacroLibraries;

            /**
             * if macroLibraries are not set create a new one
             */
            if (macroLibraries == null)
            {
                macroLibraries = new System.Collections.ArrayList();
            }

            context.MacroLibraries = macroLibraries;

            macroLibraries.Add(arg);

            /*
             *  and render it
             */
            try
            {
                if (!blockinput)
                {
                    context.PushCurrentTemplateName(arg);
                    ((SimpleNode)t.Data).Render(context, writer);
                }
            }

            /**
             * pass through application level runtime exceptions
             */
            catch (System.SystemException e)
            {
                /**
                 * LogMessage #parse errors so the user can track which file called which.
                 */
                rsvc.Log.Error("Exception rendering #parse(" + arg + ") at " + Log.Log.FormatFileString(this));
                throw e;
            }
            catch (System.Exception e)
            {
                string msg = "Exception rendering #parse(" + arg + ") at " + Log.Log.FormatFileString(this);
                rsvc.Log.Error(msg, e);
                throw new VelocityException(msg, e);
            }
            finally
            {
                if (!blockinput)
                {
                    context.PopCurrentTemplateName();
                }
            }

            /*
             *    note - a blocked input is still a successful operation as this is
             *    expected behavior.
             */

            return(true);
        }