velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chenjian <chenj...@uvic.ca>
Subject RE: #parse - major issues
Date Tue, 06 Jul 2004 15:52:31 GMT
Hi,

Well, I also had this problem before so I made a work around. Instead of using 
#parse, I used $vmParser.evalFile(...), where $vmParser an object that could 
parse a velocity template. A copy of my file is below. As an example, in my 
template I called: $vmParser.evalFile("common/header.vm", $ctx)

When the #parse problem gets fixed, I could very easily do a global replace of 
my own $vmParser with the #parse.

Cheers,

Jian Chen


=======================================================================
package parser;

import java.io.OutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.runtime.RuntimeSingleton;
import action.ActionUtil;
import org.apache.commons.lang.StringUtils;
import action.ActionUtil;

public class VMParser
{
	public static String evalFile(String templateName, Context ctx)
		throws Exception
	{
		String path = (String) 
Velocity.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
		BufferedReader reader = new BufferedReader(new FileReader(path + 
templateName));
		String sep = System.getProperty("line.separator");
		StringBuffer sb = new StringBuffer();
		while (true)
		{
			String line = reader.readLine();
			if (line == null)
			{
				break;
			}
			sb.append(line + sep);
		}

		String str = sb.toString();

		/*
		TODO: the following 2 lines are written to handle the bug in velocity
			where Macro Doesn't Work 1st Time
			the following is from Velocity user mailing list:
			* From: Terry Steichen
			* Subject: Macro Doesn't Work 1st Time
			* Date: Sun, 11 Jan 2004 10:47:35 -0800

			For as long as I can recall, I've had a problem with Velocity templates 
that contain
			macros not working the first time - after the first time they work fine.
		*/
		reader = new BufferedReader(new StringReader(str));
		RuntimeSingleton.parse(reader, "Vlog");

		StringWriter sw = new StringWriter();
		Velocity.evaluate(ctx, sw, "Vlog", str);
		return sw.toString();
	}

	public static String evalString(String str, Context ctx)
		throws Exception
	{
		/*
		TODO: the following 2 lines are written to handle the bug in velocity
			where Macro Doesn't Work 1st Time
			the following is from Velocity user mailing list:
			* From: Terry Steichen
			* Subject: Macro Doesn't Work 1st Time
			* Date: Sun, 11 Jan 2004 10:47:35 -0800

			For as long as I can recall, I've had a problem with Velocity templates 
that contain
			macros not working the first time - after the first time they work fine.
		*/
		BufferedReader reader = new BufferedReader(new StringReader(str));
		RuntimeSingleton.parse(reader, "Vlog");

		StringWriter sw = new StringWriter();
		Velocity.evaluate(ctx, sw, "Vlog", str);
		return sw.toString();
	}

	public static void render(String templateName, Context ctx, OutputStream out)
		throws Exception
	{
		String result = evalFile(templateName, ctx);
		byte[] utf8 = result.getBytes("UTF8");
		out.write(utf8);
		out.flush();
	}
}

======================================================================



>===== Original Message From "White, Tim" <Tim.White@qwest.com> =====
>Well, following up on my previous issues with #parse not picking up
>updates, here is more - and it's bad.
>
>Our content management system deletes files and then replaces them on
>our production system.
>
>While doing so, there is a moment (literally) where the "master" .vm
>page is new and live, and the "included" page isn't there.  The site is
>high-traffic enough in production that someone regularly requests the
>page in that moment.
>
>So, Velocity parses the page, unable to find it's include, and wha-bam,
>the page falls apart.  10 seconds later, when the included file is
>there, and Velocity should be checking for updated files, the "master"
>page hasn't changed, and so nothing happens, and the "master" page stays
>broken - until it get's touched again.
>
>Since our CMS can only update all the files at once, this happens every
>time we push a page.
>
>Regardless - the whole core problem is that Velocity isn't picking up
>updates to #parsed files if they change and the master page doesn't
>change.  I could live with the page being broken for 10 seconds while
>the CMS uploads the included page, but the update never takes.
>
>I've included my entire velocity.properties below.  Again, Velo 1.4,
>Velo Tools 1.1, Struts 1.1.
>
>Thanks,
>
>Tim White
>
># This version of velocity.properties
># is always used by the servlet,
># and used by com.qwest.pcatr.util.xml.XMLCreate if running in a
># Servlet container.
>
>#-----------------------------------------------------------------------
>-----
># R U N T I M E  L O G
>#-----------------------------------------------------------------------
>-----
>runtime.log.logsystem.class =
>org.apache.velocity.runtime.log.SimpleLog4JLogSystem
>runtime.log.logsystem.log4j.category = org.apache.velocity
>
>#-----------------------------------------------------------------------
>-----
># This controls if Runtime.error(), info() and warn() messages include
>the
># whole stack trace. The last property controls whether invalid
>references
># are logged.
>#-----------------------------------------------------------------------
>-----
>
>runtime.log.error.stacktrace = true
>runtime.log.warn.stacktrace = true
>runtime.log.info.stacktrace = true
>runtime.log.invalid.reference = true
>
>
>#-----------------------------------------------------------------------
>-----
># T E M P L A T E  E N C O D I N G
>#-----------------------------------------------------------------------
>-----
>
>input.encoding=ISO-8859-1
>output.encoding=ISO-8859-1
>
>
>#-----------------------------------------------------------------------
>-----
># F O R E A C H  P R O P E R T I E S
>#-----------------------------------------------------------------------
>-----
># These properties control how the counter is accessed in the #foreach
># directive. By default the reference $velocityCount will be available
># in the body of the #foreach directive. The default starting value
># for this reference is 1.
>#-----------------------------------------------------------------------
>-----
>
>directive.foreach.counter.name = velocityCount
>directive.foreach.counter.initial.value = 1
>
>
>#-----------------------------------------------------------------------
>-----
># I N C L U D E  P R O P E R T I E S
>#-----------------------------------------------------------------------
>-----
># These are the properties that governed the way #include'd content
># is governed.
>#-----------------------------------------------------------------------
>-----
>
>directive.include.output.errormsg.start = <!-- include error :
>directive.include.output.errormsg.end   =  see error log -->
>
>
>#-----------------------------------------------------------------------
>-----
># P A R S E  P R O P E R T I E S
>#-----------------------------------------------------------------------
>-----
>
>directive.parse.max.depth = 10
>
>
>#-----------------------------------------------------------------------
>-----
># VELOCIMACRO PROPERTIES
>#-----------------------------------------------------------------------
>-----
># global : name of default global library.  It is expected to be in the
>regular
># template path.  You may remove it (either the file or this property)
>if
># you wish with no harm.
>#-----------------------------------------------------------------------
>-----
>velocimacro.library = WEB-INF/VM_global_library.vm
>
>velocimacro.permissions.allow.inline = true
>velocimacro.permissions.allow.inline.to.replace.global = true
>velocimacro.permissions.allow.inline.local.scope = false
>
>velocimacro.context.localscope = false
>
>
>#-----------------------------------------------------------------------
>-----
># INTERPOLATION
>#-----------------------------------------------------------------------
>-----
># turn off and on interpolation of references and directives in string
># literals.  ON by default :)
>#-----------------------------------------------------------------------
>-----
>runtime.interpolate.string.literals = false
>
>
>#-----------------------------------------------------------------------
>-----
># RESOURCE MANAGEMENT
>#-----------------------------------------------------------------------
>-----
># Allows alternative ResourceManager and ResourceCache implementations
># to be plugged in.
>#-----------------------------------------------------------------------
>-----
>resource.manager.class =
>org.apache.velocity.runtime.resource.ResourceManagerImpl
>resource.manager.cache.class =
>org.apache.velocity.runtime.resource.ResourceCacheImpl
>
>#The line below tells the loader to look in the file loader, then the
>webapp loader
>resource.loader = file, webapp
>
>#The webapp resource loader looks for templates in the .war file
>webapp.resource.loader.cache = true
>#modificationCheckInterval does not work for this resource loader
>
>file.resource.loader.cache = true
>file.resource.loader.path = /opt/cms/PCAT/, /opt/cms/, c:, p:/PCAT, p:,
>/, .
>#modificationCheckInterval is in seconds
>file.resource.loader.modificationCheckInterval = 10
>
>#-----------------------------------------------------------------------
>-----
># R U N T I M E  C O N F I G
>#-----------------------------------------------------------------------
>-----
>
>#Number of parsers to start up for the initial pool
>parser.pool.size = 100
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message