use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class VelocityRendererImpl 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) {
VelocityEngine newEngine = new VelocityEngine();
// Class Loader
newEngine.addProperty("resource.loader", "class");
newEngine.addProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// Caching
if (isCaching()) {
newEngine.addProperty("class.resource.loader.cache", "true");
newEngine.addProperty(RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS, "com.github.bordertech.wcomponents.template.VelocityCacheImpl");
} else {
newEngine.addProperty("class.resource.loader.cache", "false");
}
// Logging
newEngine.addProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "com.github.bordertech.wcomponents.velocity.VelocityLogger");
try {
newEngine.init();
} catch (Exception ex) {
throw new SystemException("Failed to configure VelocityEngine", ex);
}
engine = newEngine;
}
return engine;
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class HandlebarsRendererImpl method renderTemplate.
/**
* {@inheritDoc}
*/
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering handlebars template " + templateName);
try {
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Get Engine
Handlebars handlebars = getHandlebarsEngine(options);
// Load template (Handlebars loader makes the template name "absolute")
Template template = handlebars.compile(templateName);
// Setup handlebars context
Context handlebarsContext = createContext(context);
// Render
writeTemplate(template, handlebarsContext, componentsByKey, writer);
} catch (FileNotFoundException e) {
throw new SystemException("Could not find handlebars template [" + templateName + "]. " + e.getMessage(), e);
} catch (Exception e) {
throw new SystemException("Problems with handlebars template [" + templateName + "]. " + e.getMessage(), e);
}
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class HandlebarsRendererImpl method renderInline.
/**
* {@inheritDoc}
*/
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering handlebars inline template.");
try {
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Get Engine
Handlebars handlebars = getHandlebarsEngine(options);
// Compile inline
Template template = handlebars.compileInline(templateInline);
// Setup handlebars context
Context handlebarsContext = createContext(context);
// Write template
writeTemplate(template, handlebarsContext, componentsByKey, writer);
} catch (Exception e) {
throw new SystemException("Problems with handlebars inline template. " + e.getMessage(), e);
}
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class WNumberField_Test method testGetValue.
@Test
public void testGetValue() {
WNumberField numberField = new WNumberField();
// Data is null
Assert.assertNull("getValue should be null by default", numberField.getValue());
// Data is a BigDecimal
numberField.setData(REQUEST_VALID_NUMBER_VALUE);
Assert.assertEquals("getValue incorrect value for BigDecimal type", REQUEST_VALID_NUMBER_VALUE, numberField.getValue());
// Data is a Long
BigDecimal value = new BigDecimal(100);
numberField.setData(Long.valueOf(value.intValue()));
Assert.assertEquals("getValue incorrect value for Long type", value, numberField.getValue());
// Data is an empty string
numberField.setData("");
Assert.assertNull("getValue incorrect value for empty String type", numberField.getValue());
// Data is a valid string
numberField.setData(REQUEST_VALID_NUMBER_TEXT);
Assert.assertEquals("getValue incorrect value for String type", REQUEST_VALID_NUMBER_VALUE, numberField.getValue());
// Data is an invalid string
numberField.setData(REQUEST_BAD_NUMBER_TEXT);
try {
numberField.getValue();
Assert.fail("Invalid string should have thrown an exception");
} catch (SystemException e) {
Assert.assertNotNull("Exception should have a message for invalid string", e.getMessage());
}
// Unsupported Type
numberField.setData(Boolean.FALSE);
try {
numberField.getValue();
Assert.fail("Unsupported data type should have thrown an exception");
} catch (SystemException e) {
Assert.assertNotNull("Exception should have a message for unsupported data type", e.getMessage());
}
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class WDateField_Test method testGetValue.
@Test
public void testGetValue() {
WDateField dateField = new WDateField();
// Data is null
Assert.assertNull("Date getValue should be null by default", dateField.getValue());
// Data is a DateType
Date date = DateUtilities.createDate(1, 0, 2008);
dateField.setData(date);
Assert.assertEquals("Date getValue incorrect date value for Date type", date, dateField.getValue());
// Data is a Long
Date dateForLong = DateUtilities.createDate(1, 0, 2009);
Long longValue = dateForLong.getTime();
dateField.setData(longValue);
Assert.assertEquals("Date getValue incorretc date value for Long type", dateForLong, dateField.getValue());
// Data is a Calendar
Date dateForCalendar = DateUtilities.createDate(1, 0, 2010);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateForCalendar);
dateField.setData(calendar);
Assert.assertEquals("Date getValue incorrect date value for Calendar Type", dateForCalendar, dateField.getValue());
// Unsupported Type
dateField.setData(Boolean.FALSE);
try {
dateField.getValue();
Assert.fail("Unsupported data type should have thrown an exception");
} catch (SystemException e) {
Assert.assertNotNull("Exception should have a message for unsupported data type", e.getMessage());
}
}
Aggregations