You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Michael Heuer <he...@acm.org> on 2005/06/28 22:00:07 UTC
creating a Template from a String
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: 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