Inheritance: InternalContextAdapter
Ejemplo n.º 1
0
        /// <summary>  initializes the document.  init() is not longer
        /// dependant upon context, but we need to let the
        /// init() carry the template name down throught for VM
        /// namespace features
        /// </summary>
        public virtual void InitDocument()
        {
            /*
            *  send an empty InternalContextAdapter down into the AST to initialize it
            */

            InternalContextAdapterImpl ica = new InternalContextAdapterImpl(new VelocityContext());

            try {
            /*
                *  put the current template name on the stack
                */

            ica.PushCurrentTemplateName(name);

            /*
                *  init the AST
                */

            ((SimpleNode) data).init(ica, rsvc);
            } finally {
            /*
                *  in case something blows up...
                *  pull it off for completeness
                */

            ica.PopCurrentTemplateName();
            }
        }
Ejemplo n.º 2
0
        /// <summary>  initializes the document.  init() is not longer
        /// dependant upon context, but we need to let the
        /// init() carry the template name down throught for VM
        /// namespace features
        /// </summary>
        public virtual void  InitDocument()
        {
            /*
             *  send an empty InternalContextAdapter down into the AST to initialize it
             */

            InternalContextAdapterImpl ica = new InternalContextAdapterImpl(new VelocityContext());

            try {
                /*
                 *  put the current template name on the stack
                 */

                ica.PushCurrentTemplateName(name);

                /*
                 *  init the AST
                 */

                ((SimpleNode)data).init(ica, rsvc);
            } finally {
                /*
                 *  in case something blows up...
                 *  pull it off for completeness
                 */

                ica.PopCurrentTemplateName();
            }
        }
Ejemplo n.º 3
0
        /// <summary> The AST node structure is merged with the
        /// context to produce the final output.
        /// *
        /// Throws IOException if failure is due to a file related
        /// issue, and Exception otherwise
        /// *
        /// </summary>
        /// <param name="context">Conext with data elements accessed by template
        /// </param>
        /// <param name="writer">output writer for rendered 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  anything else.
        ///
        /// </param>
        public virtual void  Merge(IContext context, System.IO.TextWriter writer)
        {
            /*
             *  we shouldn't have to do this, as if there is an error condition,
             *  the application code should never get a reference to the
             *  Template
             */

            if (errorCondition != null)
            {
                throw errorCondition;
            }

            if (data != null)
            {
                /*
                 *  create an InternalContextAdapter to carry the user Context down
                 *  into the rendering engine.  Set the template name and render()
                 */

                InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context);

                try {
                    ica.PushCurrentTemplateName(name);
                    ica.CurrentResource = this;

                    ((SimpleNode)data).render(ica, writer);
                } finally {
                    /*
                     *  lets make sure that we always clean up the context
                     */
                    ica.PopCurrentTemplateName();
                    ica.CurrentResource = null;
                }
            }
            else
            {
                /*
                 * this shouldn't happen either, but just in case.
                 */

                System.String msg = "Template.merge() failure. The document is null, " + "most likely due to parsing error.";

                rsvc.error(msg);
                throw new System.Exception(msg);
            }
        }
Ejemplo 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);
        }
Ejemplo n.º 5
0
	internal virtual bool Render(DvslNode node, IContext context, TextWriter writer) {
	    /*
	    *  find if we have an AST where the xpath expression mathes
	    *  for this node
	    */

	    XmlNode dom4jnode = (XmlNode) node.NodeImpl;
	    XPathNavigator nav = dom4jnode.CreateNavigator();
	    SimpleNode sn = null;

	    for (int i = 0; i < xpathList.Count; i++) {
		Hashtable m = (Hashtable) xpathList[i];

		XPathExpression expr = nav.Compile((String)m["xpath"]);

		if (nav.Matches((String)m["xpath"])) {
		    sn = (SimpleNode) m["ast"];
		    break;
		}
	    }

	    // if we found something, render
	    if (sn != null) {
		InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context);
		ica.PushCurrentTemplateName(node.Name);

		try {
		    sn.render(ica, writer);
		} finally {
		    ica.PopCurrentTemplateName();
		}

		return true;
	    }

	    return false;
	}
Ejemplo n.º 6
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;
	}
Ejemplo n.º 7
0
        /// <summary> The AST node structure is merged with the
        /// context to produce the final output.
        /// *
        /// Throws IOException if failure is due to a file related
        /// issue, and Exception otherwise
        /// *
        /// </summary>
        /// <param name="context">Conext with data elements accessed by template
        /// </param>
        /// <param name="writer">output writer for rendered 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  anything else.
        ///
        /// </param>
        public virtual void Merge(IContext context, System.IO.TextWriter writer)
        {
            /*
            *  we shouldn't have to do this, as if there is an error condition,
            *  the application code should never get a reference to the
            *  Template
            */

            if (errorCondition != null) {
            throw errorCondition;
            }

            if (data != null) {
            /*
                *  create an InternalContextAdapter to carry the user Context down
                *  into the rendering engine.  Set the template name and render()
                */

            InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context);

            try {
            ica.PushCurrentTemplateName(name);
            ica.CurrentResource = this;

            ((SimpleNode) data).render(ica, writer);
            } finally {
            /*
            *  lets make sure that we always clean up the context
            */
            ica.PopCurrentTemplateName();
            ica.CurrentResource = null;
            }
            } else {
            /*
                * this shouldn't happen either, but just in case.
                */

            System.String msg = "Template.merge() failure. The document is null, " + "most likely due to parsing error.";

            rsvc.error(msg);
            throw new System.Exception(msg);
            }
        }