Search in sources :

Example 11 with MessageResources

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

the class JavascriptValidatorTag method createDynamicJavascript.

/**
     * Generates the dynamic JavaScript for the form.
     *
     * @param config
     * @param resources
     * @param locale
     * @param form
     */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException {
    StringBuffer results = new StringBuffer();
    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();
    List actions = this.createActionList(resources, form);
    final String methods = this.createMethods(actions, this.stopOnError(config));
    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }
    results.append(this.getJavascriptBegin(methods));
    for (Iterator i = actions.iterator(); i.hasNext(); ) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;
        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }
        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
            Field field = (Field) x.next();
            // retrieve from scope?))
            if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) {
                continue;
            }
            String message = Resources.getMessage(application, request, messages, locale, va, field);
            message = (message != null) ? message : "";
            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", ");
            results.append("new Function (\"varName\", \"");
            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }
                String varValueEscaped = escapeJavascript(varValue);
                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                // So everyone using the latest format doesn't need to
                // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 12 with MessageResources

use of org.apache.struts.util.MessageResources in project head by mifos.

the class LabelTagUtils method getLabel.

public String getLabel(PageContext pageContext, String bundle, Locale locale, String key, String[] args) throws JspException {
    String message = null;
    MessageResources resources = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, false);
    if (args == null) {
        message = resources.getMessage(locale, key);
    } else {
        message = resources.getMessage(locale, key, args);
    }
    if (StringUtils.isBlank(message)) {
        String labelKey = key;
        if (ConfigurationConstants.SERVICE_CHARGE.equalsIgnoreCase(key)) {
            labelKey = ConfigurationConstants.INTEREST;
        }
        if (ConfigurationConstants.CENTER.equalsIgnoreCase(labelKey) || ConfigurationConstants.GROUP.equalsIgnoreCase(labelKey) || ConfigurationConstants.CLIENT.equalsIgnoreCase(labelKey) || ConfigurationConstants.LOAN.equalsIgnoreCase(labelKey) || ConfigurationConstants.SAVINGS.equalsIgnoreCase(labelKey) || ConfigurationConstants.INTEREST.equalsIgnoreCase(labelKey) || ConfigurationConstants.STATE.equalsIgnoreCase(labelKey) || ConfigurationConstants.POSTAL_CODE.equalsIgnoreCase(labelKey) || ConfigurationConstants.ETHNICITY.equalsIgnoreCase(labelKey) || ConfigurationConstants.CITIZENSHIP.equalsIgnoreCase(labelKey) || ConfigurationConstants.HANDICAPPED.equalsIgnoreCase(labelKey) || ConfigurationConstants.GOVERNMENT_ID.equalsIgnoreCase(labelKey) || ConfigurationConstants.ADDRESS1.equalsIgnoreCase(labelKey) || ConfigurationConstants.ADDRESS2.equalsIgnoreCase(labelKey) || ConfigurationConstants.ADDRESS3.equalsIgnoreCase(labelKey) || ConfigurationConstants.EXTERNALID.equalsIgnoreCase(labelKey) || ConfigurationConstants.BULKENTRY.equalsIgnoreCase(labelKey) || ConfigurationConstants.CITY.equalsIgnoreCase(labelKey)) {
            message = ApplicationContextProvider.getBean(MessageLookup.class).lookupLabel(labelKey);
        }
    }
    if (StringUtils.isBlank(message)) {
        // FOR PersonnelUIResources.properties
        String newKey = "Personnel." + key;
        message = resources.getMessage(locale, newKey);
    }
    if (StringUtils.isBlank(message)) {
        message = "";
    }
    return ApplicationContextProvider.getBean(MessageLookup.class).replaceSubstitutions(message);
}
Also used : MessageResources(org.apache.struts.util.MessageResources) MessageLookup(org.mifos.application.master.MessageLookup)

Example 13 with MessageResources

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

the class PerformForward method perform.

// ------------------------------------------------------- Protected Methods
/**
     * <p>Perform the appropriate processing on the specified
     * <code>ForwardConfig</code>.</p>
     *
     * @param context       The context for this request
     * @param forwardConfig The forward to be performed
     */
protected void perform(ActionContext context, ForwardConfig forwardConfig) throws Exception {
    ServletActionContext sacontext = (ServletActionContext) context;
    String uri = forwardConfig.getPath();
    if (uri == null) {
        ActionServlet servlet = sacontext.getActionServlet();
        MessageResources resources = servlet.getInternal();
        throw new IllegalArgumentException(resources.getMessage("forwardPathNull"));
    }
    HttpServletRequest request = sacontext.getRequest();
    ServletContext servletContext = sacontext.getContext();
    HttpServletResponse response = sacontext.getResponse();
    // If the forward can be unaliased into an action, then use the path of the action
    String actionIdPath = RequestUtils.actionIdURL(forwardConfig, sacontext.getRequest(), sacontext.getActionServlet());
    if (actionIdPath != null) {
        uri = actionIdPath;
        ForwardConfig actionIdForwardConfig = new ForwardConfig(forwardConfig);
        actionIdForwardConfig.setPath(actionIdPath);
        forwardConfig = actionIdForwardConfig;
    }
    if (uri.startsWith("/")) {
        uri = resolveModuleRelativePath(forwardConfig, servletContext, request);
    }
    if (response.isCommitted() && !forwardConfig.getRedirect()) {
        handleAsInclude(uri, servletContext, request, response);
    } else if (forwardConfig.getRedirect()) {
        handleAsRedirect(uri, request, response);
    } else {
        handleAsForward(uri, servletContext, request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageResources(org.apache.struts.util.MessageResources) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionServlet(org.apache.struts.action.ActionServlet)

Example 14 with MessageResources

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

the class Resources method getVarValue.

/**
     * Get the value of a variable.
     *
     * @param var         the validator variable
     * @param application The ServletContext
     * @param request     the servlet request
     * @param required    Whether the variable is mandatory
     * @return The variables values
     */
public static String getVarValue(Var var, ServletContext application, HttpServletRequest request, boolean required) {
    String varName = var.getName();
    String varValue = var.getValue();
    // Non-resource variable
    if (!var.isResource()) {
        return varValue;
    }
    // Get the message resources
    String bundle = var.getBundle();
    MessageResources messages = getMessageResources(application, request, bundle);
    // Retrieve variable's value from message resources
    Locale locale = RequestUtils.getUserLocale(request, null);
    String value = messages.getMessage(locale, varValue, null);
    // Not found in message resources
    if ((value == null) && required) {
        throw new IllegalArgumentException(sysmsgs.getMessage("variable.resource.notfound", varName, varValue, bundle));
    }
    if (log.isDebugEnabled()) {
        log.debug("Var=[" + varName + "], " + "bundle=[" + bundle + "], " + "key=[" + varValue + "], " + "value=[" + value + "]");
    }
    return value;
}
Also used : Locale(java.util.Locale) MessageResources(org.apache.struts.util.MessageResources)

Example 15 with MessageResources

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

the class Resources method getMessageResources.

/**
     * Retrieve <code>MessageResources</code> for the module and bundle.
     *
     * @param application the servlet context
     * @param request     the servlet request
     * @param bundle      the bundle key
     */
public static MessageResources getMessageResources(ServletContext application, HttpServletRequest request, String bundle) {
    if (bundle == null) {
        bundle = Globals.MESSAGES_KEY;
    }
    MessageResources resources = (MessageResources) request.getAttribute(bundle);
    if (resources == null) {
        ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, application);
        resources = (MessageResources) application.getAttribute(bundle + moduleConfig.getPrefix());
    }
    if (resources == null) {
        resources = (MessageResources) application.getAttribute(bundle);
    }
    if (resources == null) {
        throw new NullPointerException("No message resources found for bundle: " + bundle);
    }
    return resources;
}
Also used : MessageResources(org.apache.struts.util.MessageResources) ModuleConfig(org.apache.struts.config.ModuleConfig)

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