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();
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations