示例#1
0
        public void Merge(IContext context, TextWriter writer)
        {
            if (this.errorCondition != null)
            {
                throw this.errorCondition;
            }
            if (this.data != null)
            {
                InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(context);
                try
                {
                    internalContextAdapterImpl.PushCurrentTemplateName(this.name);
                    internalContextAdapterImpl.CurrentResource = this;
                    ((SimpleNode)this.data).Render(internalContextAdapterImpl, writer);
                }
                finally
                {
                    internalContextAdapterImpl.PopCurrentTemplateName();
                    internalContextAdapterImpl.CurrentResource = null;
                }
                return;
            }
            string message = "Template.merge() failure. The document is null, most likely due to parsing error.";

            this.rsvc.Error(message);
            throw new System.Exception(message);
        }
示例#2
0
文件: Template.cs 项目: 15831944/tool
        /// <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>
        /// <throws>  TemplateInitException When a problem occurs during the document initialization. </throws>
        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);
                ica.CurrentResource = this;

                /*
                 *  Init the AST
                 */

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

                ica.PopCurrentTemplateName();
                ica.CurrentResource = null;
            }
        }
示例#3
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, 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 (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);
        }
示例#4
0
        public void InitDocument()
        {
            InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(new VelocityContext());

            try
            {
                internalContextAdapterImpl.PushCurrentTemplateName(this.name);
                ((SimpleNode)this.data).Init(internalContextAdapterImpl, this.rsvc);
            }
            finally
            {
                internalContextAdapterImpl.PopCurrentTemplateName();
            }
        }
示例#5
0
        public bool Evaluate(IContext context, TextWriter writer, string logTag, TextReader reader)
        {
            SimpleNode simpleNode = null;

            try
            {
                simpleNode = this.ri.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, this.ri);
                    }
                    catch (System.Exception ex2)
                    {
                        this.ri.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);
        }
        /// <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 void Merge(IContext context, 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.
                 */
                String msg = "Template.merge() failure. The document is null, " + "most likely due to parsing error.";

                rsvc.error(msg);
                throw new System.Exception(msg);
            }
        }
示例#7
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);
        }
示例#8
0
        internal 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);
        }
示例#9
0
        /// <summary>
        /// initializes the document.  init() is not longer
        /// dependant upon context, but we need to let the
        /// init() carry the template name down through for VM
        /// namespace features
        /// </summary>
        public void InitDocument()
        {
            // send an empty InternalContextAdapter down into the AST to initialize it
            InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(new VelocityContext());

            try
            {
                // put the current template name on the stack
                internalContextAdapterImpl.PushCurrentTemplateName(name);

                // init the AST
                ((SimpleNode)data).Init(internalContextAdapterImpl, runtimeServices);
            }
            finally
            {
                // in case something blows up...
                // pull it off for completeness
                internalContextAdapterImpl.PopCurrentTemplateName();
            }
        }
        /// <summary> 
        /// initializes the document.  init() is not longer
        /// dependant upon context, but we need to let the
        /// init() carry the template name down through for VM
        /// namespace features
        /// </summary>
        public void InitDocument()
        {
            // send an empty InternalContextAdapter down into the AST to initialize it
            InternalContextAdapterImpl internalContextAdapterImpl = new InternalContextAdapterImpl(new VelocityContext());

            try
            {
                // put the current template name on the stack
                internalContextAdapterImpl.PushCurrentTemplateName(name);

                // init the AST
                ((SimpleNode) data).Init(internalContextAdapterImpl, runtimeServices);
            }
            finally
            {
                // in case something blows up...
                // pull it off for completeness
                internalContextAdapterImpl.PopCurrentTemplateName();
            }
        }
示例#11
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">Context with data elements accessed by template</param>
        /// <param name="writer">writer for rendered template</param>
        /// <exception cref="ResourceNotFoundException">
        /// if template not found from any available source.
        /// </exception>
        /// <exception cref="ParseErrorException">
        /// if template cannot be parsed due to syntax (or other) error.
        /// </exception>
        /// <exception cref="System.Exception">
        /// anything else.
        /// </exception>
        public void Merge(IContext context, 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 internalContextAdapterImpl = new InternalContextAdapterImpl(context);

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

                    ((SimpleNode)data).Render(internalContextAdapterImpl, writer);
                }
                finally
                {
                    // lets make sure that we always clean up the context
                    internalContextAdapterImpl.PopCurrentTemplateName();
                    internalContextAdapterImpl.CurrentResource = null;
                }
            }
            else
            {
                // this shouldn't happen either, but just in case.
                String msg = "Template.merge() failure. The document is null, most likely due to parsing error.";

                runtimeServices.Error(msg);
                throw new System.Exception(msg);
            }
        }
示例#12
0
文件: Template.cs 项目: 15831944/tool
        /// <summary> The AST node structure is merged with the
        /// context to produce the final output.
        ///
        /// </summary>
        /// <param name="context">Conext with data elements accessed by template
        /// </param>
        /// <param name="writer">output writer for rendered template
        /// </param>
        /// <param name="macroLibraries">a list of template files containing macros to be used when merging
        /// </param>
        /// <throws>  ResourceNotFoundException if template not found </throws>
        /// <summary>          from any available source.
        /// </summary>
        /// <throws>  ParseErrorException if template cannot be parsed due </throws>
        /// <summary>          to syntax (or other) Error.
        /// </summary>
        /// <throws>  MethodInvocationException When a method on a referenced object in the context could not invoked. </throws>
        /// <throws>  IOException  Might be thrown while rendering. </throws>
        /// <since> 1.6
        /// </since>
        public virtual void Merge(IContext context, TextWriter writer, IList macroLibraries)
        {
            /*
             *  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);

                /**
                 * Set the macro libraries
                 */
                ica.MacroLibraries = macroLibraries;

                if (macroLibraries != null)
                {
                    for (int i = 0; i < macroLibraries.Count; i++)
                    {
                        /**
                         * Build the macro library
                         */
                        try
                        {
                            rsvc.GetTemplate((string)macroLibraries[i]);
                        }
                        catch (ResourceNotFoundException re)
                        {
                            /*
                             * the macro lib wasn't found.  Note it and throw
                             */
                            rsvc.Log.Error("template.merge(): " + "cannot find template " + ((string)macroLibraries[i]));
                            throw re;
                        }
                        catch (ParseErrorException pe)
                        {
                            /*
                             * the macro lib was found, but didn't parse - syntax Error
                             *  note it and throw
                             */
                            rsvc.Log.Error("template.merge(): " + "syntax error in template " + ((string)macroLibraries[i]) + ".");
                            throw pe;
                        }
                        catch (System.Exception e)
                        {
                            throw new RuntimeException("Template.merge(): parse failed in template  " + ((string)macroLibraries[i]) + ".", e);
                        }
                    }
                }

                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.
                 */

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

                throw new SystemException(msg);
            }
        }
示例#13
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 bool Evaluate(IContext context, TextWriter writer, String logTag, TextReader reader)
		{
			SimpleNode nodeTree = null;

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

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

				ica.PushCurrentTemplateName(logTag);

				try
				{
					try
					{
						nodeTree.Init(ica, ri);
					}
					catch(Exception e)
					{
						ri.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;
		}
示例#14
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 bool Evaluate(IContext context, TextWriter writer, String logTag, TextReader reader)
		{
			SimpleNode nodeTree = null;

			try
			{
				nodeTree = runtimeInstance.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, runtimeInstance);
					}
					catch(Exception e)
					{
						runtimeInstance.Error(string.Format("Velocity.evaluate() : init exception for tag = {0} : {1}", logTag, e));
					}

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

				return true;
			}

			return false;
		}
示例#15
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">writer for rendered template</param>
		/// <exception cref="ResourceNotFoundException">
		/// if template not found from any available source.
		/// </exception>
		/// <exception cref="ParseErrorException">
		/// if template cannot be parsed due to syntax (or other) error.
		/// </exception>
		/// <exception cref="System.Exception">
		/// anything else.
		/// </exception>
		public void Merge(IContext context, 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.
				String msg = "Template.merge() failure. The document is null, most likely due to parsing error.";

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