Hi,
thanks a lot for the quick responses!
> I haven't actually seen anyone do this sort of thing. At least, i
> don't recall anyone contributing it. This would be a good thing to
> have up on our Wiki somewhere (perhaps under CommunityArticles and
> ContributedCode.
Yes, the CommunityArticles and ContributedCode sections
look promising to me as well. I'll try to get something done soon.
I'm not as sure where the VelocityInteractor would fit, but i would be
curious to see/know more about what it does. In recent months, i've
been mulling over the possibility of a new project (or new package in
the core project) to provide super-easy-to-use wrappers around the
Velocity runtime that are pre-configured (or much more easily and
specifically configured) and enhanced for particular Velocity tasks
(e.g. like a VelocityEmailer). It's still just a thought at this
point, but a glance at how you're using it makes it look like the
VelocityInteractor could be a such a thing or at least a seed for such
things.
I have attached the source code for the VelocityInteractor. It's
not rocket science, just a wrapper that I found useful for handling
many different VelocityContexts and Templates. Maybe you also find
it useful. Feel free to use or modify.
Thanks,
Matthias
P.S.: I noticed that the mailing list archive seems to discard
attachments, at least I can't see the attachment from my previous
posting in the web interface. Let me know if I should resend it
directly.
/*
* VelocityException.java
*
* Created on 6. August 2007, 09:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author i000698
*/
public class VelocityException extends Exception {
/**
* Constructs a new <code>VelocityException</code> exception with
<code>null</code> as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to [EMAIL PROTECTED] #initCause}.
*/
public VelocityException() {
super();
}
/**
* Constructs a new <code>VelocityException</code> exception with the
specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to [EMAIL PROTECTED] #initCause}.
*
*
* @param message the detail message. The detail message is saved for
* later retrieval by the [EMAIL PROTECTED] #getMessage()} method.
*/
public VelocityException(String message) {
super(message);
}
/**
* Constructs a new <code>VelocityException</code> exception with the
specified detail message and
* cause. <p>Note that the detail message associated with
* <code>cause</code> is <i>not</i> automatically incorporated in
* this <code>VelocityException</code> exception's detail message.
*
*
* @param message the detail message (which is saved for later retrieval
* by the [EMAIL PROTECTED] #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* [EMAIL PROTECTED] #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public VelocityException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new <code>VelocityException</code> exception with the
specified cause and a
* detail message of <tt>(cause==null ? null : cause.toString())</tt>
* (which typically contains the class and detail message of
* <tt>cause</tt>). This constructor is useful for
<code>VelocityException</code> exceptions
* that are little more than wrappers for other throwables.
*
*
* @param cause the cause (which is saved for later retrieval by the
* [EMAIL PROTECTED] #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public VelocityException(Throwable cause) {
super(cause);
}
}
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
/**
* A helper class to encapsulate the interaction with the Velocity template
engine
*/
public class VelocityInteractor {
private static final String ENCODING = "ISO-8859-1";
private Map<String,VelocityContext> contexts = new
HashMap<String,VelocityContext>();
private Map<String,Template> templates = new
HashMap<String,Template>();
private Map<String,Template> defaultTemplates = new
HashMap<String,Template>();
private VelocityContext defaultContext = new VelocityContext();
private Template defaultTemplate = null;
/**
* Create a new interactor instance
*
* Setup the search path for velocity: first, relative ot the local
* directory (".") and then with an absolute path (File.separator) and
* then with the configuration directory itself (for included Velocity macros)
*/
public VelocityInteractor(String templateDir) throws VelocityException {
if (templateDir == null)
throw new IllegalArgumentException("templateDir may not be null");
try {
Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
templateDir);
Velocity.init();
} catch (Exception ex) {
throw new VelocityException(ex);
}
}
/**
* Add a template for the anonymous context. Note that there is exactly one
anonymous template and
* context, so if this method is called multiple times, only the template
added last will be
* active in the anonymous context.
*/
public void add(String templateFileName) throws VelocityException {
if (templateFileName == null)
throw new IllegalArgumentException("templateFileName may not be null");
try {
if (!defaultTemplates.containsKey(templateFileName))
defaultTemplates.put(templateFileName,
Velocity.getTemplate(templateFileName, ENCODING));
} catch (ParseErrorException ex) {
throw new VelocityException(ex);
} catch (ResourceNotFoundException ex) {
throw new VelocityException(ex);
} catch (Exception ex) {
throw new VelocityException(ex);
}
defaultTemplate = defaultTemplates.get(templateFileName);
}
/**
* Add a template/context pair with the given name. There can be any number
of such named pairs,
* and this class will hold these pairs in an internal cache, accessible by
name.
*/
public void add(String name, String templateFileName) throws
VelocityException {
if (name == null)
throw new IllegalArgumentException("name may not be null");
if (templateFileName == null)
throw new IllegalArgumentException("templateFileName may not be null");
try {
contexts.put(name, new VelocityContext());
templates.put(name, Velocity.getTemplate(templateFileName, ENCODING));
} catch (ParseErrorException ex) {
throw new VelocityException(ex);
} catch (ResourceNotFoundException ex) {
throw new VelocityException(ex);
} catch (Exception ex) {
throw new VelocityException(ex);
}
}
/**
* Add something to the given named velocity context
*/
public void put(String name, String key, Object value) {
if (name == null)
throw new IllegalArgumentException("name may not be null");
if (key == null)
throw new IllegalArgumentException("key may not be null");
if (value == null)
throw new IllegalArgumentException("value may not be null (key = " + key
+ ")");
if (!templates.containsKey(name))
throw new IllegalArgumentException("No template defined for name " +
name);
contexts.get(name).put(key, value);
}
/**
* Add something to the anonymous velocity context
*/
public void put(String key, Object value) {
if (key == null)
throw new IllegalArgumentException("key may not be null");
if (value == null)
throw new IllegalArgumentException("value may not be null (key = " + key
+ ")");
if (defaultTemplate == null)
throw new IllegalArgumentException("No anonymous template/context
defined");
defaultContext.put(key, value);
}
/**
* Execute the Velocity merge operation for the anonymous context
*/
public void merge(Writer writer) throws VelocityException {
if (writer == null)
throw new IllegalArgumentException("writer may not be null");
if (defaultTemplate == null)
throw new IllegalArgumentException("No anonymous template/context
defined");
try {
defaultTemplate.merge(defaultContext, writer);
} catch (ParseErrorException ex) {
throw new VelocityException(ex);
} catch (ResourceNotFoundException ex) {
throw new VelocityException(ex);
} catch (MethodInvocationException ex) {
throw new VelocityException(ex);
} catch (Exception ex) {
throw new VelocityException(ex);
}
}
/**
* Execute the Velocity merge operation for the given named context
*/
public void merge(String name, Writer writer) throws VelocityException {
if (name == null)
throw new IllegalArgumentException("name may not be null");
if (writer == null)
throw new IllegalArgumentException("writer may not be null");
if (!templates.containsKey(name))
throw new IllegalArgumentException("No template defined for name " +
name);
try {
templates.get(name).merge(contexts.get(name), writer);
} catch (ParseErrorException ex) {
throw new VelocityException(ex);
} catch (ResourceNotFoundException ex) {
throw new VelocityException(ex);
} catch (MethodInvocationException ex) {
throw new VelocityException(ex);
} catch (Exception ex) {
throw new VelocityException(ex);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]