Update of /cvsroot/xdoclet/xdoclet/modules/apache/src/xdoclet/modules/apache/velocity
In directory sc8-pr-cvs1:/tmp/cvs-serv22162/apache/velocity
Added Files:
VelocityEngineTagHandler.java VelocitySubTemplateEngine.java
Log Message:
Add Velocity tag
--- NEW FILE: VelocityEngineTagHandler.java ---
/*
* Copyright (c) 2001, 2002 The XDoclet team
* All rights reserved.
*/
package xdoclet.modules.apache.velocity;
import java.util.Properties;
import org.apache.commons.logging.Log;
import xdoclet.XDocletException;
import xdoclet.modules.apache.SubTemplateEngine;
import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
import xdoclet.template.TemplateEngine;
import xdoclet.template.TemplateException;
import xdoclet.util.LogUtil;
/**
* This is a tag handler able to execute Velocity template engine on a block!
*
* @author zluspai
* @created July 16, 2003
* @xdoclet.taghandler namespace="Velocity"
*/
public class VelocityEngineTagHandler extends AbstractProgramElementTagsHandler
{
/*
* @todo - put this section in the above docs, but needs care so the reformatter
doesn't mangle it
* <XDtTemplateEngines:generator>
* currentClass is: ${currentClass.Name}
* Methods are: #set ( $numMethods = 0 )
* #foreach ($method in ${currentClass.methods})
* $method.Name #set ( $numMethods = $numMethods+1 )
* #end </XDtTemplateEngines:generator>
* And you can get a variable from the last run Velocity context by using this
XDoclet tag:
* <XDtTemplateEngines:getVelocityVariable name="numMethods" />
* Also if the result of the template contains a special <XDt></XDt> section that
will be merged by the XDoclet
* engine. For example:
* <XDtTemplateEngines:generator>
* Print out the methods using the XDoclet Templates:
* <XDt> <XDtMethod:forAllMethods> <XDtMethod:methodName/>
</XDtMethod:forAllMethods> </XDt>
* </XDtTemplateEngines:generator>
*/
// XDOCLET subtemplate section start tag inside the other template
private final String XDTSectionStart = "<XDt>";
// subtemplate end tag
private final String XDTSectionEnd = "</XDt>";
//// Velocity engine
private VelocitySubTemplateEngine velocityEngine;
private VelocitySubTemplateEngine getVelocityEngine() {
if (velocityEngine == null) {
velocityEngine = new VelocitySubTemplateEngine();
}
return velocityEngine;
}
/**
* Get a value of a velocity variable from the context <pre>
* <XDtTemplateEngines:getVelocityVariable name="numMethods" default="0" />
* </pre>
*
* @param attributes
* @return
* @exception XDocletException
* @doc.tag type="content"
*/
public String getVariable(Properties attributes) throws XDocletException
{
return getSubTemplateVariable(getVelocityEngine(), attributes);
}
/**
* Evaluates the body block with the Velocity template engine If the silent="yes"
attribute is set then the
* Generator will not produce any output, but the template will run. If the
disable="yes" attribute is set then the
* Velocity template will not run at all.
*
* @param template The body of the block tag
* @param attributes The attributes of the template tag
* @exception TemplateException
* @doc.tag type="block"
*/
public void generator(String template, Properties attributes) throws
TemplateException
{
generate(getVelocityEngine(), template, attributes);
}
/**
* Clear all velocity variables
*
* @doc.tag type="content"
* @throws XDocletException
*/
public void clearVariables() throws XDocletException
{
getVelocityEngine().clearVariables();
}
/**
* Get a subtemplate variable
*
* @param subengine The subtemplate engine
* @param attributes The attributes from XDOCLET tag
* @return The value
* @throws XDocletException
*/
private String getSubTemplateVariable(SubTemplateEngine subengine, Properties
attributes) throws XDocletException
{
String vname = attributes.getProperty("name");
if (vname == null) {
throw new XDocletException("Missing name property the name of the Velocity
variable!");
}
Object value = subengine.getVariable(vname);
if (value == null) {
String defaultValue = attributes.getProperty("default", "");
return defaultValue;
}
return value.toString();
}
/// Common methods for all engines
/**
* @param subengine The SubTemplateEngine used to generate
* @param template The body of the block tag
* @param attributes The attributes of the template tag
* @throws TemplateException
*/
private void generate(SubTemplateEngine subengine, String template, Properties
attributes) throws TemplateException
{
if ("yes".equalsIgnoreCase(attributes.getProperty("disable"))) {
return;
}
fillVariables(subengine);
String result = subengine.generate(template, attributes);
Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "generate");
log.debug("Subengine generated results:" + result);
StringBuffer output = new StringBuffer(result);
// send back the XDOCLET engine the velocity output, but only when
silent="true" is not set
if (!"yes".equalsIgnoreCase(attributes.getProperty("silent"))) {
TemplateEngine engine = getEngine();
escapeResults(engine, output);
engine.print(output.toString());
}
}
/**
* Fill the variables passed to the engines
*
* @param templateEngine
* @exception XDocletException
*/
private void fillVariables(SubTemplateEngine templateEngine) throws
XDocletException
{
Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "fillVariables");
log.debug("fillVariables() called");
try {
templateEngine.setVariable("tagHandler", this);
templateEngine.setVariable("currentPackage", getCurrentPackage());
templateEngine.setVariable("currentClass", getCurrentClass());
templateEngine.setVariable("currentMethod", getCurrentMethod());
templateEngine.setVariable("currentConstructor", getCurrentConstructor());
templateEngine.setVariable("currentField", getCurrentField());
templateEngine.setVariable("currentClassTag", getCurrentClassTag());
templateEngine.setVariable("currentFieldTag", getCurrentFieldTag());
templateEngine.setVariable("currentMethodTag", getCurrentMethodTag());
}
catch (Exception ex) {
log.error("Exception when setting variables", ex);
throw new XDocletException(ex, "Exception when setting variables");
}
}
/**
* Escape and evaluate the <XDt></XDt> sections with XDOCLET template engine. This
allows embedding XDOCLET sections
* into Velocity sections
*
* @param engine The XDOCLET template engine
* @param results The results
* @exception TemplateException
*/
private void escapeResults(TemplateEngine engine, StringBuffer results) throws
TemplateException
{
Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "escapeResults");
// will return when no more subtemplate found
while (true) {
// find the <XDt> sections
int startidx = results.indexOf(XDTSectionStart);
int endidx = results.indexOf(XDTSectionEnd);
boolean templatefound = (startidx >= 0 && endidx >= 0);
if (!templatefound)
return;
// extract the content of the <XDt> section
String subXDtTemplate = results.substring(startidx +
XDTSectionStart.length(), endidx);
log.debug("subTemplate found:<" + subXDtTemplate + ">");
// remove the <XDt> section
results.delete(startidx, endidx + XDTSectionEnd.length());
log.debug("Results after XDt section removed:<" + results.toString() +
">");
// generate the subtemplate by calling the XDOCLET engine
String subTemplateOutput = engine.outputOf(subXDtTemplate);
log.debug("Generated subTempalte output:" + subTemplateOutput);
// write back the result
results.insert(startidx, subTemplateOutput);
}
}
}
--- NEW FILE: VelocitySubTemplateEngine.java ---
/*
* Copyright (c) 2001, 2002 The XDoclet team
* All rights reserved.
*/
package xdoclet.modules.apache.velocity;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import xdoclet.XDocletException;
import xdoclet.modules.apache.SubTemplateEngine;
/**
* Velocity subtemplate engine
*
* @author zluspai
* @created July 16, 2003
*/
class VelocitySubTemplateEngine implements SubTemplateEngine
{
// context where the Velocity variables stored
private VelocityContext context = new VelocityContext();
/*
* (non-Javadoc)
* @see xdoclet.templateutil.SubTemplateEngine#getVariable(java.lang.String)
*/
public Object getVariable(String name)
{
return context.get(name);
}
/*
* (non-Javadoc)
* @see xdoclet.templateutil.SubTemplateEngine#setVariable(java.lang.String,
java.lang.Object)
*/
public void setVariable(String name, Object value)
{
context.put(name, value);
}
/*
* (non-Javadoc)
* @see xdoclet.templateutil.SubTemplateEngine#clearVariables()
*/
public void clearVariables()
{
context = new VelocityContext();
}
/*
* (non-Javadoc)
* @see xdoclet.templateutil.SubTemplateEngine#generate(java.lang.String,
java.util.Properties)
*/
public String generate(String template, Properties attributes) throws
XDocletException
{
// write out the current template as a temp file
// String templateFile = "TEMP_" + System.currentTimeMillis() + ".vm";
// String templateFile =
"c:\\temp\\"+System.currentTimeMillis()+".vm";
// File f = new File(templateFile);
// f.deleteOnExit();
try {
// BufferedOutputStream os = new BufferedOutputStream(new
FileOutputStream(f));
//
// os.write(template.getBytes());
// os.flush();
// os.close();
/*
* setup
*/
Velocity.init();
//init("velocity.properties");
/*
* Make a context object and populate with the data. This
* is where the Velocity engine gets the data to resolve the
* references (ex. $list) in the template
*/
// VelocityContext context = new VelocityContext();
/*
* get the Template object. This is the parsed version of your
* template input file. Note that getTemplate() can throw
* ResourceNotFoundException : if it doesn't find the template
* ParseErrorException : if there is something wrong with the VTL
* Exception : if something else goes wrong (this is generally
* indicative of as serious problem...)
*/
/*
* Template velocityTemplate = null;
* try {
* velocityTemplate = Velocity.
* /.getTemplate(templateFile);
* }
* catch (ResourceNotFoundException rnfe) {
* throw new XDocletException(rnfe, "Cannot load temporary template file:"
+ templateFile);
* }
* catch (ParseErrorException pee) {
* throw new XDocletException(pee, "Syntax error in template!");
* }
*/
/*
* Now have the template engine process your template using the
* data placed into the context. Think of it as a 'merge'
* of the template and the data to produce the output stream.
*/
StringWriter writer = new StringWriter();
// if (velocityTemplate != null) {
Velocity.evaluate(context, writer, "generate", template);
// velocityTemplate.merge(context, writer);
// }
/*
* flush and cleanup
*/
writer.flush();
writer.close();
return writer.getBuffer().toString();
}
catch (Exception e) {
throw new XDocletException(e, "Exception when executing Velocity
template!");
}
// finally {
// f.delete();
// }
}
}
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel