Search in sources :

Example 16 with SystemException

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;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) SystemException(com.github.bordertech.wcomponents.util.SystemException) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 17 with SystemException

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);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Context(com.github.jknack.handlebars.Context) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) Handlebars(com.github.jknack.handlebars.Handlebars) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SystemException(com.github.bordertech.wcomponents.util.SystemException) Template(com.github.jknack.handlebars.Template)

Example 18 with SystemException

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);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Context(com.github.jknack.handlebars.Context) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) Handlebars(com.github.jknack.handlebars.Handlebars) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SystemException(com.github.bordertech.wcomponents.util.SystemException) Template(com.github.jknack.handlebars.Template)

Example 19 with SystemException

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());
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 20 with SystemException

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());
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Calendar(java.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SystemException (com.github.bordertech.wcomponents.util.SystemException)94 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)17 WComponent (com.github.bordertech.wcomponents.WComponent)15 UIContext (com.github.bordertech.wcomponents.UIContext)13 Test (org.junit.Test)12 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 WebElement (org.openqa.selenium.WebElement)9 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)6 IOException (java.io.IOException)5 Select (org.openqa.selenium.support.ui.Select)5 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)4 Request (com.github.bordertech.wcomponents.Request)4 InputStream (java.io.InputStream)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 Environment (com.github.bordertech.wcomponents.Environment)3 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 List (java.util.List)3