You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Leo Asanov <le...@yahoo.com> on 2005/06/28 08:06:56 UTC
Initializing Velocity ouside of a servlet
Hi!
What is the right way to initialize Velocity outside
of a servlet? My application works fine when I'm using
VelocityViewServlet, but when I'm trying to initialize
Velocity from any standard Java class (with
Velocity.init(absolutePathToPropertiesFile))
Velocity.mergeTemplate always throws
"ResourceNotFound" exception.
I guess this happens because Velocity cannot get
parent directory of a web application without a
reference to servlet context. But I'm not sure what I
can do with this. I can't specify absolute path in
velocity.properties and I would prefer to avoid
setting path properties directly in the code (I don't
know how to it anyway - tried
Velocity.setProperty("file.resource.loader.path",absolutePathToTemplateDirectory)
and it didn't work).
Maybe it's possible to pass a "default directory" path
somehow?
Cheers,
Leo Asanov
____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating a Template from a String
Posted by Will Glass-Husain <wg...@forio.com>.
It's even easier than that. Use the method VelocityEngine.evaluate() and
pass in a StringWriter.
http://jakarta.apache.org/velocity/api/index.html
Note that this doesn't take advantage of Velocity's extensive caching
capababilities - it's best suited for low-volume evaluation.
WILL
----- Original Message -----
From: "didge" <di...@foundrylogic.com>
To: <ve...@jakarta.apache.org>
Sent: Tuesday, June 28, 2005 1:56 PM
Subject: Re: creating a Template from a String
Michael,
Here's how I do it:
import org.apache.velocity.*;
import org.apache.velocity.runtime.parser.*;
import org.apache.velocity.runtime.*;
import org.apache.velocity.exception.*;
import java.io.*;
public class StringTemplate extends Template {
protected String _source;
public StringTemplate(String id, String source) throws Exception {
name = id;
Exception errorCondition = null;
setRuntimeServices(RuntimeSingleton.getRuntimeServices());
try
{
BufferedReader br = new BufferedReader(new
StringReader(source));
data = RuntimeSingleton.parse( br, name);
initDocument();
}
catch( UnsupportedEncodingException uce )
{
String msg = "Template.process : Unsupported input encoding : "
+ encoding
+ " for template " + name;
errorCondition = new ParseErrorException( msg );
throw errorCondition;
}
catch ( ParseException pex )
{
/*
* remember the error and convert
*/
errorCondition = new ParseErrorException( pex.getMessage() );
throw errorCondition;
}
catch( Exception e )
{
/*
* who knows? Something from initDocument()
*/
errorCondition = e;
throw e;
}
}
public boolean process() throws ResourceNotFoundException,
ParseErrorException, Exception {
return true;
}
}
---- Michael Heuer <he...@acm.org> wrote:
>
> Hello,
>
> Is it possible to create a Template from a String?
I couldn't find an
> obvious way to do this from the javadoc and wanted
to ask before diving
> into the source code.
>
> Thanks.
>
> michael
>
>
>
>
>
---------------------------------------------------------------------
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
Re: creating a Template from a String
Posted by Shinobu Kawai <sh...@gmail.com>.
> Isn't the simplest way to do this to create your own resource loader?
Like this one? ;)
http://issues.apache.org/bugzilla/show_bug.cgi?id=20677
Best regards,
-- Shinobu
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: creating a Template from a String
Posted by William Pietri <wi...@scissor.com>.
> On Wed, 2005-06-29 at 17:07 +0100, Steve O'Hara wrote:
>
> > I wish it was that easy. Micheal asked how to create a Template
> > from a String, but VelocityEngine.evaluate() does not return a
> > Template. Maybe that's what he really wanted, but that's not what
> > he asked for.
> Isn't the simplest way to do this to create your own resource loader?
>
That's the approach I took for some unit testing, and it worked fine for
me.
William
--
William Pietri <wi...@scissor.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: creating a Template from a String
Posted by Steve O'Hara <so...@pivotal-solutions.co.uk>.
Isn't the simplest way to do this to create your own resource loader?
package com.pivotal;
import java.io.*;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
public class StringResourceLoader extends ResourceLoader {
public StringResourceLoader() {}
public void init(ExtendedProperties configuration) {}
public boolean isSourceModified(Resource resource) {
return true;
}
public long getLastModified(Resource resource) {
return 0L;
}
public synchronized InputStream getResourceStream(String templateName) throws
ResourceNotFoundException {
return new StringBufferInputStream(templateName);
}
}
-----Original Message-----
From:
velocity-user-return-16292-sohara=pivotal-solutions.co.uk@jakarta.apache
.org
[mailto:velocity-user-return-16292-sohara=pivotal-solutions.co.uk@jakart
a.apache.org]On Behalf Of didge
Sent: 28 June 2005 22:20
To: Velocity Users List
Subject: Re: creating a Template from a String
I wish it was that easy. Micheal asked how to create a Template from a String, but VelocityEngine.evaluate() does not return a Template. Maybe that's what he really wanted, but that's not what he asked for.
didge
---- Velocity Users List <ve...@jakarta.apache.org> wrote:
>
> It's even easier than that. Use the method VelocityEngine.evaluate() and
> pass in a StringWriter.
>
> http://jakarta.apache.org/velocity/api/index.html
>
> Note that this doesn't take advantage of Velocity's extensive caching
> capababilities - it's best suited for low-volume evaluation.
>
> WILL
>
> ----- Original Message -----
> From: "didge" <di...@foundrylogic.com>
> To: <ve...@jakarta.apache.org>
> Sent: Tuesday, June 28, 2005 1:56 PM
> Subject: Re: creating a Template from a String
>
>
>
> Michael,
>
> Here's how I do it:
>
> import org.apache.velocity.*;
> import org.apache.velocity.runtime.parser.*;
> import org.apache.velocity.runtime.*;
> import org.apache.velocity.exception.*;
>
> import java.io.*;
>
> public class StringTemplate extends Template {
> protected String _source;
>
> public StringTemplate(String id, String source) throws Exception {
> name = id;
> Exception errorCondition = null;
> setRuntimeServices(RuntimeSingleton.getRuntimeServices());
>
> try
> {
> BufferedReader br = new BufferedReader(new
> StringReader(source));
> data = RuntimeSingleton.parse( br, name);
> initDocument();
> }
> catch( UnsupportedEncodingException uce )
> {
> String msg = "Template.process : Unsupported input encoding : "
> + encoding
> + " for template " + name;
>
> errorCondition = new ParseErrorException( msg );
> throw errorCondition;
> }
> catch ( ParseException pex )
> {
> /*
> * remember the error and convert
> */
>
> errorCondition = new ParseErrorException( pex.getMessage() );
> throw errorCondition;
> }
> catch( Exception e )
> {
> /*
> * who knows? Something from initDocument()
> */
>
> errorCondition = e;
> throw e;
> }
> }
>
> public boolean process() throws ResourceNotFoundException,
> ParseErrorException, Exception {
> return true;
> }
> }
>
>
>
> ---- Michael Heuer <he...@acm.org> wrote:
> >
> > Hello,
> >
> > Is it possible to create a Template from a String?
> I couldn't find an
> > obvious way to do this from the javadoc and wanted
> to ask before diving
> > into the source code.
> >
> > Thanks.
> >
> > michael
> >
> >
>
>
>
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> 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
>
>
>
---------------------------------------------------------------------
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
Re: creating a Template from a String
Posted by didge <di...@foundrylogic.com>.
I wish it was that easy. Micheal asked how to create a Template from a String, but VelocityEngine.evaluate() does not return a Template. Maybe that's what he really wanted, but that's not what he asked for.
didge
---- Velocity Users List <ve...@jakarta.apache.org> wrote:
>
> It's even easier than that. Use the method VelocityEngine.evaluate() and
> pass in a StringWriter.
>
> http://jakarta.apache.org/velocity/api/index.html
>
> Note that this doesn't take advantage of Velocity's extensive caching
> capababilities - it's best suited for low-volume evaluation.
>
> WILL
>
> ----- Original Message -----
> From: "didge" <di...@foundrylogic.com>
> To: <ve...@jakarta.apache.org>
> Sent: Tuesday, June 28, 2005 1:56 PM
> Subject: Re: creating a Template from a String
>
>
>
> Michael,
>
> Here's how I do it:
>
> import org.apache.velocity.*;
> import org.apache.velocity.runtime.parser.*;
> import org.apache.velocity.runtime.*;
> import org.apache.velocity.exception.*;
>
> import java.io.*;
>
> public class StringTemplate extends Template {
> protected String _source;
>
> public StringTemplate(String id, String source) throws Exception {
> name = id;
> Exception errorCondition = null;
> setRuntimeServices(RuntimeSingleton.getRuntimeServices());
>
> try
> {
> BufferedReader br = new BufferedReader(new
> StringReader(source));
> data = RuntimeSingleton.parse( br, name);
> initDocument();
> }
> catch( UnsupportedEncodingException uce )
> {
> String msg = "Template.process : Unsupported input encoding : "
> + encoding
> + " for template " + name;
>
> errorCondition = new ParseErrorException( msg );
> throw errorCondition;
> }
> catch ( ParseException pex )
> {
> /*
> * remember the error and convert
> */
>
> errorCondition = new ParseErrorException( pex.getMessage() );
> throw errorCondition;
> }
> catch( Exception e )
> {
> /*
> * who knows? Something from initDocument()
> */
>
> errorCondition = e;
> throw e;
> }
> }
>
> public boolean process() throws ResourceNotFoundException,
> ParseErrorException, Exception {
> return true;
> }
> }
>
>
>
> ---- Michael Heuer <he...@acm.org> wrote:
> >
> > Hello,
> >
> > Is it possible to create a Template from a String?
> I couldn't find an
> > obvious way to do this from the javadoc and wanted
> to ask before diving
> > into the source code.
> >
> > Thanks.
> >
> > michael
> >
> >
>
>
>
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> 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
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating a Template from a String
Posted by didge <di...@foundrylogic.com>.
Michael,
Here's how I do it:
import org.apache.velocity.*;
import org.apache.velocity.runtime.parser.*;
import org.apache.velocity.runtime.*;
import org.apache.velocity.exception.*;
import java.io.*;
public class StringTemplate extends Template {
protected String _source;
public StringTemplate(String id, String source) throws Exception {
name = id;
Exception errorCondition = null;
setRuntimeServices(RuntimeSingleton.getRuntimeServices());
try
{
BufferedReader br = new BufferedReader(new StringReader(source));
data = RuntimeSingleton.parse( br, name);
initDocument();
}
catch( UnsupportedEncodingException uce )
{
String msg = "Template.process : Unsupported input encoding : " + encoding
+ " for template " + name;
errorCondition = new ParseErrorException( msg );
throw errorCondition;
}
catch ( ParseException pex )
{
/*
* remember the error and convert
*/
errorCondition = new ParseErrorException( pex.getMessage() );
throw errorCondition;
}
catch( Exception e )
{
/*
* who knows? Something from initDocument()
*/
errorCondition = e;
throw e;
}
}
public boolean process() throws ResourceNotFoundException, ParseErrorException, Exception {
return true;
}
}
---- Michael Heuer <he...@acm.org> wrote:
>
> Hello,
>
> Is it possible to create a Template from a String?
I couldn't find an
> obvious way to do this from the javadoc and wanted
to ask before diving
> into the source code.
>
> Thanks.
>
> michael
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
creating a Template from a String
Posted by Michael Heuer <he...@acm.org>.
Hello,
Is it possible to create a Template from a String? I couldn't find an
obvious way to do this from the javadoc and wanted to ask before diving
into the source code.
Thanks.
michael
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Initializing Velocity ouside of a servlet
Posted by mailmur <ma...@yahoo.com>.
Here is a small standalone test program, It uses my
customized UnicodeFileResourceLoader. But it should
give you a good example how to use it outside of any
app server.
http://koti.mbnet.fi/akini/java/unicodereader/
I have actually used Velocity as a "sql query
generator". I have sql template files as .vm files.
Then I get input parameters from the user, run
velocity template and get a full sql query string.
Works well and can even modify templates at runtime
and changes are reflected to the program immediately.
FileResourcelaoder handles reload stuff automatically.
--- Markos Charatzas <xa...@forthnet.gr> wrote:
> For starters, try and initialize velocity with a
> Properties object loaded
> using a Resource. Make sure that velocity.properties
> is in the classpath.
> On Tuesday 28 June 2005 09:06, Leo Asanov wrote:
> > What is the right way to initialize Velocity
> outside
> > of a servlet? My application works fine when I'm
____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Initializing Velocity ouside of a servlet
Posted by Markos Charatzas <xa...@forthnet.gr>.
Hello Leo,
For starters, try and initialize velocity with a Properties object loaded
using a Resource. Make sure that velocity.properties is in the classpath.
e.g.
------------------------
InputStream vProps =
this.getClass().getResourceAsStream("/velocity.properties");
try {
Properties properties = new Properties();
properties.load(vProps);
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(properties);
VelocityContext context = new VelocityContext();
}
catch(Exception e) {
logger.error("velocity engine failed to initialise", e);
}
------------------------
Also, in velocity.properties you have to specify the template loader to use.
(have a look here for template loaders,
http://jakarta.apache.org/velocity/developer-guide.html#Configuring%20Resource%20Loaders)
Try ClasspathLoader
----------------------
resource.loader = production
production.loader.description = "Production Resource Loader"
production.resource.loader.class =
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
----------------------
When using velocityEngine.getTemplate(String) just make sure that the path is
in the following format
--------------------
templates/myTemplate.vm
--------------------
and that "templates/" dir is in the classpath since you are using a
ClasspathResourceLoader
Regards,
Markos
On Tuesday 28 June 2005 09:06, Leo Asanov wrote:
> Hi!
>
> What is the right way to initialize Velocity outside
> of a servlet? My application works fine when I'm using
> VelocityViewServlet, but when I'm trying to initialize
> Velocity from any standard Java class (with
> Velocity.init(absolutePathToPropertiesFile))
> Velocity.mergeTemplate always throws
> "ResourceNotFound" exception.
>
> I guess this happens because Velocity cannot get
> parent directory of a web application without a
> reference to servlet context. But I'm not sure what I
> can do with this. I can't specify absolute path in
> velocity.properties and I would prefer to avoid
> setting path properties directly in the code (I don't
> know how to it anyway - tried
> Velocity.setProperty("file.resource.loader.path",absolutePathToTemplateDire
>ctory) and it didn't work).
> Maybe it's possible to pass a "default directory" path
> somehow?
>
> Cheers,
> Leo Asanov
>
>
>
> ____________________________________________________
> Yahoo! Sports
> Rekindle the Rivalries. Sign up for Fantasy Football
> http://football.fantasysports.yahoo.com
>
> ---------------------------------------------------------------------
> 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
Re: Initializing Velocity ouside of a servlet
Posted by Nathan Bubna <nb...@gmail.com>.
what version of VelocityTools are you using? and are you trying to
initialize the Velocity singleton within the same application as the
VelocityViewServlet?
have you tried initializing a VelocityEngine instead of the singleton?
On 6/27/05, Leo Asanov <le...@yahoo.com> wrote:
> Hi!
>
> What is the right way to initialize Velocity outside
> of a servlet? My application works fine when I'm using
> VelocityViewServlet, but when I'm trying to initialize
> Velocity from any standard Java class (with
> Velocity.init(absolutePathToPropertiesFile))
> Velocity.mergeTemplate always throws
> "ResourceNotFound" exception.
>
> I guess this happens because Velocity cannot get
> parent directory of a web application without a
> reference to servlet context. But I'm not sure what I
> can do with this. I can't specify absolute path in
> velocity.properties and I would prefer to avoid
> setting path properties directly in the code (I don't
> know how to it anyway - tried
> Velocity.setProperty("file.resource.loader.path",absolutePathToTemplateDirectory)
> and it didn't work).
> Maybe it's possible to pass a "default directory" path
> somehow?
>
> Cheers,
> Leo Asanov
>
>
>
> ____________________________________________________
> Yahoo! Sports
> Rekindle the Rivalries. Sign up for Fantasy Football
> http://football.fantasysports.yahoo.com
>
> ---------------------------------------------------------------------
> 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