Search in sources :

Example 11 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class RendererFactoryImpl method createRenderer.

/**
 * Attempts to create a Renderer with the given name.
 *
 * @param rendererName the name of the Renderer
 * @return a LayoutManager of the given type, or null if the class was not found.
 */
private Renderer createRenderer(final String rendererName) {
    try {
        Class<?> managerClass = Class.forName(rendererName);
        Object manager = managerClass.newInstance();
        if (!(manager instanceof Renderer)) {
            throw new SystemException(rendererName + " is not a Renderer");
        }
        return (Renderer) manager;
    } catch (ClassNotFoundException e) {
        // Legal - there might not a renderer implementation for the component in this format
        return null;
    } catch (InstantiationException e) {
        throw new SystemException("Failed to instantiate " + rendererName, e);
    } catch (IllegalAccessException e) {
        throw new SystemException("Failed to access " + rendererName, e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Renderer(com.github.bordertech.wcomponents.Renderer)

Example 12 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class WTableRenderer method doRender.

@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WTable table = (WTable) component;
    XmlStringBuilder xml = renderContext.getWriter();
    xml.appendTagOpen("ui:table");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", table.isHidden(), "true");
    xml.appendOptionalAttribute("caption", table.getCaption());
    switch(table.getType()) {
        case TABLE:
            xml.appendAttribute("type", "table");
            break;
        case HIERARCHIC:
            xml.appendAttribute("type", "hierarchic");
            break;
        default:
            throw new SystemException("Unknown table type: " + table.getType());
    }
    switch(table.getStripingType()) {
        case ROWS:
            xml.appendAttribute("striping", "rows");
            break;
        case COLUMNS:
            xml.appendAttribute("striping", "cols");
            break;
        case NONE:
            break;
        default:
            throw new SystemException("Unknown striping type: " + table.getStripingType());
    }
    switch(table.getSeparatorType()) {
        case HORIZONTAL:
            xml.appendAttribute("separators", "horizontal");
            break;
        case VERTICAL:
            xml.appendAttribute("separators", "vertical");
            break;
        case BOTH:
            xml.appendAttribute("separators", "both");
            break;
        case NONE:
            break;
        default:
            throw new SystemException("Unknown separator type: " + table.getSeparatorType());
    }
    xml.appendClose();
    // Render margin
    MarginRendererUtil.renderMargin(table, renderContext);
    if (table.getPaginationMode() != PaginationMode.NONE) {
        paintPaginationDetails(table, xml);
    }
    if (table.getSelectMode() != SelectMode.NONE) {
        paintSelectionDetails(table, xml);
    }
    if (table.getExpandMode() != ExpandMode.NONE) {
        paintExpansionDetails(table, xml);
    }
    if (table.isSortable()) {
        paintSortDetails(table, xml);
    }
    // Headers
    paintColumnHeaderFooter(table, renderContext, true);
    // Body
    paintRows(table, renderContext);
    // Footers
    if (table.isRenderColumnFooters()) {
        paintColumnHeaderFooter(table, renderContext, false);
    }
    // Actions
    paintTableActions(table, renderContext);
    xml.appendEndTag("ui:table");
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) WTable(com.github.bordertech.wcomponents.WTable) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 13 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class SubordinateControlHelper method registerSubordinateControl.

/**
 * Register the Subordinate Control so that it can be applied by the {@link SubordinateControlInterceptor}.
 *
 * @param controlId the subordinate id
 */
public static void registerSubordinateControl(final String controlId) {
    UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
    if (uic == null) {
        throw new SystemException("No User Context available to register Subordinate Control.");
    }
    Set<String> controls = (Set<String>) uic.getFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY);
    if (controls == null) {
        controls = new HashSet<>();
        uic.setFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY, controls);
    }
    controls.add(controlId);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 14 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class PlainTextRendererImpl 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 plain text template " + templateName);
    // Expects path to be absolute.
    String name = templateName.startsWith("/") ? templateName : "/" + templateName;
    boolean xmlEncode = options.containsKey(XML_ENCODE);
    String cacheKey = templateName + "-" + xmlEncode;
    InputStream stream = null;
    // Caching
    Object value = options.get(USE_CACHE);
    boolean cache = (isCaching() && value == null) || (value != null && "true".equalsIgnoreCase(value.toString()));
    try {
        String output = null;
        if (cache) {
            output = getCache().get(cacheKey);
        }
        if (output == null) {
            stream = getClass().getResourceAsStream(name);
            if (stream == null) {
                throw new SystemException("Could not find plain text template [" + templateName + "].");
            }
            output = new String(StreamUtil.getBytes(stream), StandardCharsets.UTF_8);
            if (xmlEncode) {
                output = WebUtilities.encode(output);
            }
            if (cache) {
                getCache().put(cacheKey, output);
            }
        }
        writer.write(output);
    } catch (SystemException e) {
        throw e;
    } catch (Exception e) {
        throw new SystemException("Problems with plain text template [" + templateName + "]. " + e.getMessage(), e);
    } finally {
        StreamUtil.safeClose(stream);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) InputStream(java.io.InputStream) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 15 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class VelocityRendererImpl 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 velocity template [" + templateName + "].");
    // Velocity uses a ClassLoader so dont use an absolute path.
    String name = templateName.startsWith("/") ? templateName.substring(1) : templateName;
    try {
        // Load template
        Template template = getVelocityEngine().getTemplate(name);
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Setup context
        VelocityContext velocityContext = new VelocityContext();
        for (Map.Entry<String, Object> entry : context.entrySet()) {
            velocityContext.put(entry.getKey(), entry.getValue());
        }
        // Write template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            template.merge(velocityContext, velocityWriter);
        }
    } catch (ResourceNotFoundException e) {
        throw new SystemException("Could not find velocity template [" + templateName + "]. " + e.getMessage(), e);
    } catch (Exception e) {
        throw new SystemException("Problems with velocity template [" + templateName + "]. " + e.getMessage(), e);
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Map(java.util.Map)

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