pl.chilldev.facelets.taglib.spring.web.MessageTag.java Source code

Java tutorial

Introduction

Here is the source code for pl.chilldev.facelets.taglib.spring.web.MessageTag.java

Source

/**
 * This file is part of the ChillDev-Facelets.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2014  by Rafa Wrzeszcz - Wrzasq.pl.
 */

package pl.chilldev.facelets.taglib.spring.web;

import java.text.MessageFormat;

import java.util.Collection;
import java.util.Locale;

import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.view.facelets.FaceletContext;
import javax.faces.view.facelets.TagAttribute;
import javax.faces.view.facelets.TagConfig;
import javax.faces.view.facelets.TagHandler;

import org.springframework.context.MessageSource;

import org.springframework.util.StringUtils;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

/**
 * Outputs translated message.
 *
 * @version 0.0.1
 * @since 0.0.1
 */
public class MessageTag extends TagHandler {
    /**
     * `message=""` attribute name.
     */
    public static final String ATTRIBUTE_MESSAGE = "message";

    /**
     * `locale=""` attribute name.
     */
    public static final String ATTRIBUTE_LOCALE = "locale";

    /**
     * `args=""` attribute name.
     */
    public static final String ATTRIBUTE_ARGS = "args";

    /**
     * `separator=""` attribute name.
     */
    public static final String ATTRIBUTE_SEPARATOR = "separator";

    /**
     * `var=""` attribute name.
     */
    public static final String ATTRIBUTE_VAR = "var";

    /**
     * Message key attribute.
     */
    protected TagAttribute message;

    /**
     * Locale attribute.
     */
    protected TagAttribute locale;

    /**
     * Arguments attribute.
     */
    protected TagAttribute args;

    /**
     * Arguments separator attribute.
     */
    protected TagAttribute separator;

    /**
     * Variable name attribute.
     */
    protected TagAttribute var;

    /**
     * Initializes handler configuration.
     *
     * @param config Handler configuration.
     * @since 0.0.1
     */
    public MessageTag(TagConfig config) {
        super(config);

        this.message = this.getRequiredAttribute(MessageTag.ATTRIBUTE_MESSAGE);
        this.locale = this.getAttribute(MessageTag.ATTRIBUTE_LOCALE);
        this.args = this.getAttribute(MessageTag.ATTRIBUTE_ARGS);
        this.separator = this.getAttribute(MessageTag.ATTRIBUTE_SEPARATOR);
        this.var = this.getAttribute(MessageTag.ATTRIBUTE_VAR);
    }

    /**
     * {@inheritDoc}
     *
     * @since 0.0.1
     */
    @Override
    public void apply(FaceletContext context, UIComponent parent) {
        // get required components
        WebApplicationContext applicationContext = FacesContextUtils
                .getRequiredWebApplicationContext(context.getFacesContext());
        MessageSource messageSource = applicationContext.getBean(MessageSource.class);

        String message = this.message.getValue(context);
        Locale locale = this.locale == null ? context.getLocale()
                : (Locale) this.locale.getObject(context, Locale.class);

        // if message source is not available simply stay with original message
        if (messageSource != null) {
            // note that we don't pass any arguments, we will handle interpolation on our as Spring's implementation
            // doesn't interpolate default message
            message = messageSource.getMessage(message, null, locale);
        }

        // interpolate parameters
        String separator = this.separator == null ? "," : this.separator.getValue(context);
        message = MessageFormat.format(message,
                this.args == null ? null : this.resolveArguments(this.args.getObject(context), separator));

        // just output the text
        if (this.var == null) {
            UIOutput component = new UIOutput();
            component.setValue(message);
            parent.getChildren().add(component);
        } else {
            // assign result to the variable
            context.setAttribute(this.var.getValue(context), message);
        }
    }

    /**
     * Builds interpolation arguments list.
     *
     * @param arguments Arguments list.
     * @param separator Argulemts separator.
     * @return Arguments list.
     * @since 0.0.1
     */
    protected Object[] resolveArguments(Object arguments, String separator) {
        // this methos is taken from original Spring MessageTag implementation for JSP
        if (arguments instanceof String) {
            return StringUtils.delimitedListToStringArray((String) arguments, separator);
        } else if (arguments instanceof Object[]) {
            return (Object[]) arguments;
        } else if (arguments instanceof Collection) {
            return ((Collection<?>) arguments).toArray();
        } else if (arguments != null) {
            // assume a single argument object.
            return new Object[] { arguments };
        } else {
            return new Object[0];
        }
    }
}