Search in sources :

Example 6 with MessageResources

use of org.apache.struts.util.MessageResources in project sonarqube by SonarSource.

the class TagUtils method message.

/**
     * Look up and return a message string, based on the specified
     * parameters.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @param args        Replacement parameters for this message
     * @return message string
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
public String message(PageContext pageContext, String bundle, String locale, String key, Object[] args) throws JspException {
    MessageResources resources = retrieveMessageResources(pageContext, bundle, false);
    Locale userLocale = getUserLocale(pageContext, locale);
    String message = null;
    if (args == null) {
        message = resources.getMessage(userLocale, key);
    } else {
        message = resources.getMessage(userLocale, key, args);
    }
    if ((message == null) && log.isDebugEnabled()) {
        // log missing key to ease debugging
        log.debug(resources.getMessage("message.resources", key, bundle, locale));
    }
    return message;
}
Also used : Locale(java.util.Locale) MessageResources(org.apache.struts.util.MessageResources)

Example 7 with MessageResources

use of org.apache.struts.util.MessageResources in project sonarqube by SonarSource.

the class TagUtils method present.

/**
     * Return true if a message string for the specified message key is
     * present for the specified <code>Locale</code> and bundle.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @return true if a message string for message key exists
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
public boolean present(PageContext pageContext, String bundle, String locale, String key) throws JspException {
    MessageResources resources = retrieveMessageResources(pageContext, bundle, true);
    Locale userLocale = getUserLocale(pageContext, locale);
    return resources.isPresent(userLocale, key);
}
Also used : Locale(java.util.Locale) MessageResources(org.apache.struts.util.MessageResources)

Example 8 with MessageResources

use of org.apache.struts.util.MessageResources in project sonarqube by SonarSource.

the class Resources method getArgValues.

/**
     * Gets the message arguments based on the current <code>ValidatorAction</code>
     * and <code>Field</code>.
     *
     * @param application     the servlet context
     * @param request         the servlet request
     * @param defaultMessages Default message resources
     * @param locale          the locale
     * @param args            The arguments for the message
     */
private static String[] getArgValues(ServletContext application, HttpServletRequest request, MessageResources defaultMessages, Locale locale, Arg[] args) {
    if ((args == null) || (args.length == 0)) {
        return null;
    }
    String[] values = new String[args.length];
    for (int i = 0; i < args.length; i++) {
        if (args[i] != null) {
            if (args[i].isResource()) {
                MessageResources messages = defaultMessages;
                if (args[i].getBundle() != null) {
                    messages = getMessageResources(application, request, args[i].getBundle());
                }
                values[i] = messages.getMessage(locale, args[i].getKey());
            } else {
                values[i] = args[i].getKey();
            }
        }
    }
    return values;
}
Also used : MessageResources(org.apache.struts.util.MessageResources)

Example 9 with MessageResources

use of org.apache.struts.util.MessageResources in project sonarqube by SonarSource.

the class Resources method getMessage.

/**
     * Gets the <code>Locale</code> sensitive value based on the key passed
     * in.
     *
     * @param application     the servlet context
     * @param request         the servlet request
     * @param defaultMessages The default Message resources
     * @param locale          The locale
     * @param va              The Validator Action
     * @param field           The Validator Field
     */
public static String getMessage(ServletContext application, HttpServletRequest request, MessageResources defaultMessages, Locale locale, ValidatorAction va, Field field) {
    Msg msg = field.getMessage(va.getName());
    if ((msg != null) && !msg.isResource()) {
        return msg.getKey();
    }
    String msgKey = null;
    String msgBundle = null;
    MessageResources messages = defaultMessages;
    if (msg == null) {
        msgKey = va.getMsg();
    } else {
        msgKey = msg.getKey();
        msgBundle = msg.getBundle();
        if (msg.getBundle() != null) {
            messages = getMessageResources(application, request, msg.getBundle());
        }
    }
    if ((msgKey == null) || (msgKey.length() == 0)) {
        return "??? " + va.getName() + "." + field.getProperty() + " ???";
    }
    // Get the arguments
    Arg[] args = field.getArgs(va.getName());
    String[] argValues = getArgValues(application, request, messages, locale, args);
    // Return the message
    return messages.getMessage(locale, msgKey, argValues);
}
Also used : Msg(org.apache.commons.validator.Msg) MessageResources(org.apache.struts.util.MessageResources) Arg(org.apache.commons.validator.Arg)

Example 10 with MessageResources

use of org.apache.struts.util.MessageResources in project sonarqube by SonarSource.

the class Resources method getActionMessage.

/**
     * Gets the <code>ActionMessage</code> based on the
     * <code>ValidatorAction</code> message and the <code>Field</code>'s arg
     * objects.
     *
     * @param validator the Validator
     * @param request   the servlet request
     * @param va        Validator action
     * @param field     the validator Field
     */
public static ActionMessage getActionMessage(Validator validator, HttpServletRequest request, ValidatorAction va, Field field) {
    Msg msg = field.getMessage(va.getName());
    if ((msg != null) && !msg.isResource()) {
        return new ActionMessage(msg.getKey(), false);
    }
    String msgKey = null;
    String msgBundle = null;
    if (msg == null) {
        msgKey = va.getMsg();
    } else {
        msgKey = msg.getKey();
        msgBundle = msg.getBundle();
    }
    if ((msgKey == null) || (msgKey.length() == 0)) {
        return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false);
    }
    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    MessageResources messages = getMessageResources(application, request, msgBundle);
    Locale locale = RequestUtils.getUserLocale(request, null);
    Arg[] args = field.getArgs(va.getName());
    String[] argValues = getArgValues(application, request, messages, locale, args);
    ActionMessage actionMessage = null;
    if (msgBundle == null) {
        actionMessage = new ActionMessage(msgKey, argValues);
    } else {
        String message = messages.getMessage(locale, msgKey, argValues);
        actionMessage = new ActionMessage(message, false);
    }
    return actionMessage;
}
Also used : Msg(org.apache.commons.validator.Msg) Locale(java.util.Locale) MessageResources(org.apache.struts.util.MessageResources) Arg(org.apache.commons.validator.Arg) ActionMessage(org.apache.struts.action.ActionMessage) ServletContext(javax.servlet.ServletContext)

Aggregations

MessageResources (org.apache.struts.util.MessageResources)17 Locale (java.util.Locale)4 ServletContext (javax.servlet.ServletContext)4 ModuleConfig (org.apache.struts.config.ModuleConfig)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 JspException (javax.servlet.jsp.JspException)2 Arg (org.apache.commons.validator.Arg)2 Msg (org.apache.commons.validator.Msg)2 ActionServlet (org.apache.struts.action.ActionServlet)2 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)2 MessageResourcesFactory (org.apache.struts.util.MessageResourcesFactory)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Field (org.apache.commons.validator.Field)1 ValidatorAction (org.apache.commons.validator.ValidatorAction)1 Var (org.apache.commons.validator.Var)1 ActionMapping (org.apache.struts.action.ActionMapping)1