use of org.apache.velocity.app.VelocityEngine 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.app.VelocityEngine in project wcomponents by BorderTech.
the class VelocityEngineFactory method getVelocityEngine.
/**
* <p>
* Returns the VelocityEngine associated with this factory. If this is the first time we are using the engine,
* create it and initialise it.</p>
*
* <p>
* Note that velocity engines are hugely resource intensive, so we don't want too many of them. For the time being
* we have a single instance stored as a static variable. This would only be a problem if the VelocityLayout class
* ever wanted to use different engine configurations (unlikely).</p>
*
* @return the VelocityEngine associated with this factory.
*/
public static synchronized VelocityEngine getVelocityEngine() {
if (engine == null) {
String fileTemplates = ConfigurationProperties.getVelocityFileTemplates();
boolean cacheTemplates = ConfigurationProperties.getVelocityCacheTemplates();
VelocityEngine newEngine = new VelocityEngine();
Properties props = new Properties();
// "source mode" or not
if (fileTemplates != null && !"".equals(fileTemplates)) {
// Source mode
LOG.info("Velocity engine running in source mode from " + fileTemplates);
props.setProperty("resource.loader", "file,class");
props.setProperty("file.resource.loader.path", fileTemplates);
props.setProperty("file.resource.loader.cache", "false");
props.setProperty("file.resource.loader.modificationCheckInterval", "2");
props.setProperty("class.resource.loader.cache", "false");
props.setProperty("class.resource.loader.modificationCheckInterval", "2");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
} else {
String cache = String.valueOf(cacheTemplates);
props.setProperty("class.resource.loader.cache", cache);
props.setProperty("resource.loader", "class");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
// Setup commons logging for velocity
props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "com.github.bordertech.wcomponents.velocity.VelocityLogger");
// Set up access to the common velocity macros.
props.setProperty(RuntimeConstants.VM_LIBRARY, ConfigurationProperties.getVelocityMacroLibrary());
try {
if (LOG.isInfoEnabled()) {
// Dump properties
StringWriter writer = new StringWriter();
props.list(new PrintWriter(writer));
LOG.info("Configuring velocity with the following properties...\n" + writer);
}
newEngine.init(props);
} catch (Exception ex) {
throw new SystemException("Failed to configure VelocityEngine", ex);
}
engine = newEngine;
}
return engine;
}
use of org.apache.velocity.app.VelocityEngine 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.app.VelocityEngine in project traccar by tananaev.
the class Context method initEventsModule.
private static void initEventsModule() {
geofenceManager = new GeofenceManager(dataManager);
calendarManager = new CalendarManager(dataManager);
notificationManager = new NotificationManager(dataManager);
Properties velocityProperties = new Properties();
velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/");
velocityProperties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
String address;
try {
address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
address = "localhost";
}
String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", "");
webUrl = Context.getConfig().getString("web.url", webUrl);
velocityProperties.setProperty("web.url", webUrl);
velocityEngine = new VelocityEngine();
velocityEngine.init(velocityProperties);
motionEventHandler = new MotionEventHandler(tripsConfig);
overspeedEventHandler = new OverspeedEventHandler(Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, Context.getConfig().getBoolean("event.overspeed.notRepeat"));
}
use of org.apache.velocity.app.VelocityEngine 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