use of org.apache.velocity.Template in project cayenne by apache.
the class ClassGenerationAction method getTemplate.
protected Template getTemplate(TemplateType type) {
String templateName = customTemplateName(type);
if (templateName == null) {
templateName = defaultTemplateName(type);
}
// Velocity < 1.5 has some memory problems, so we will create a VelocityEngine every time,
// and store templates in an internal cache, to avoid uncontrolled memory leaks...
// Presumably 1.5 fixes it.
Template template = templateCache.get(templateName);
if (template == null) {
Properties props = new Properties();
props.put("resource.loader", "cayenne");
props.put("cayenne.resource.loader.class", ClassGeneratorResourceLoader.class.getName());
props.put("cayenne.resource.loader.cache", "false");
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(props);
template = velocityEngine.getTemplate(templateName);
templateCache.put(templateName, template);
}
return template;
}
use of org.apache.velocity.Template in project wcomponents by BorderTech.
the class VelocityRenderer method paintXml.
/**
* Paints the component in XML using the Velocity Template.
*
* @param component the component to paint.
* @param writer the writer to send the HTML output to.
*/
public void paintXml(final WComponent component, final Writer writer) {
if (LOG.isDebugEnabled()) {
LOG.debug("paintXml called for component class " + component.getClass());
}
String templateText = null;
if (component instanceof AbstractWComponent) {
AbstractWComponent abstractComp = ((AbstractWComponent) component);
templateText = abstractComp.getTemplateMarkUp();
}
try {
Map<String, WComponent> componentsByKey = new HashMap<>();
VelocityContext context = new VelocityContext();
fillContext(component, context, componentsByKey);
VelocityWriter velocityWriter = new VelocityWriter(writer, componentsByKey, UIContextHolder.getCurrent());
if (templateText != null) {
VelocityEngine engine = VelocityEngineFactory.getVelocityEngine();
engine.evaluate(context, velocityWriter, component.getClass().getSimpleName(), templateText);
} else {
Template template = getTemplate(component);
if (template == null) {
LOG.warn("VelocityRenderer invoked for a component with no template: " + component.getClass().getName());
} else {
template.merge(context, velocityWriter);
}
}
velocityWriter.close();
if (component instanceof VelocityProperties) {
((VelocityProperties) component).mapUsed();
}
} catch (ResourceNotFoundException rnfe) {
LOG.error("Could not find template '" + url + "' for component " + component.getClass().getName(), rnfe);
} catch (ParseErrorException pee) {
// syntax error : problem parsing the template
LOG.error("Parse problems", pee);
} catch (MethodInvocationException mie) {
// something invoked in the template
// threw an exception
Throwable wrapped = mie.getWrappedThrowable();
LOG.error("Problems with velocity", mie);
if (wrapped != null) {
LOG.error("Wrapped exception...", wrapped);
}
} catch (Exception e) {
LOG.error("Problems with velocity", e);
}
}
use of org.apache.velocity.Template in project cayenne by apache.
the class SQLTemplateResourceManager method getResource.
/**
* Returns a Velocity Resource which is a Template for the given SQL.
*/
public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException {
synchronized (templateCache) {
Template resource = templateCache.get(resourceName);
if (resource == null) {
resource = new Template();
resource.setRuntimeServices(rsvc);
resource.setResourceLoader(this);
resource.setName(resourceName);
resource.setEncoding(encoding);
resource.process();
templateCache.put(resourceName, resource);
}
return resource;
}
}
use of org.apache.velocity.Template in project mybatis-generator-gui-extension by spawpaw.
the class SCVXGeneratorPlugin method renderTemplateAsString.
public String renderTemplateAsString(String templateFile, VelocityContext ctx) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("template/" + templateFile);
StringWriter sw = new StringWriter();
t.merge(ctx, sw);
return sw.toString();
}
use of org.apache.velocity.Template in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.
@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new EndpointContext(endpoint, packageName);
VelocityContext context = configcontext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
Aggregations