use of java.util.Locale in project sonarqube by SonarSource.
the class FieldChecks method validateDate.
/**
* Checks if the field is a valid date. If the field has a datePattern
* variable, that will be used to format <code>java.text.SimpleDateFormat</code>.
* If the field has a datePatternStrict variable, that will be used to
* format <code>java.text.SimpleDateFormat</code> and the length will be
* checked so '2/12/1999' will not pass validation with the format
* 'MM/dd/yyyy' because the month isn't two digits. If no datePattern
* variable is specified, then the field gets the DateFormat.SHORT format
* for the locale. The setLenient method is set to <code>false</code> for
* all variations.
*
* @param bean The bean validation is being performed on.
* @param va The <code>ValidatorAction</code> that is currently
* being performed.
* @param field The <code>Field</code> object associated with the
* current field being validated.
* @param errors The <code>ActionMessages</code> object to add errors
* to if any validation errors occur.
* @param validator The <code>Validator</code> instance, used to access
* other field values.
* @param request Current request object.
* @return true if valid, false otherwise.
*/
public static Object validateDate(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) {
Object result = null;
String value = null;
value = evaluateBean(bean, field);
boolean isStrict = false;
String datePattern = Resources.getVarValue("datePattern", field, validator, request, false);
if (GenericValidator.isBlankOrNull(datePattern)) {
datePattern = Resources.getVarValue("datePatternStrict", field, validator, request, false);
if (!GenericValidator.isBlankOrNull(datePattern)) {
isStrict = true;
}
}
Locale locale = RequestUtils.getUserLocale(request, null);
if (GenericValidator.isBlankOrNull(value)) {
return Boolean.TRUE;
}
try {
if (GenericValidator.isBlankOrNull(datePattern)) {
result = GenericTypeValidator.formatDate(value, locale);
} else {
result = GenericTypeValidator.formatDate(value, datePattern, isStrict);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if (result == null) {
errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
}
return (result == null) ? Boolean.FALSE : result;
}
use of java.util.Locale in project sonarqube by SonarSource.
the class FieldChecks method validateDoubleLocale.
/**
* Checks if the field can safely be converted to a double primitive.
*
* @param bean The bean validation is being performed on.
* @param va The <code>ValidatorAction</code> that is currently
* being performed.
* @param field The <code>Field</code> object associated with the
* current field being validated.
* @param errors The <code>ActionMessages</code> object to add errors
* to if any validation errors occur.
* @param validator The <code>Validator</code> instance, used to access
* other field values.
* @param request Current request object.
* @return true if valid, false otherwise.
*/
public static Object validateDoubleLocale(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) {
Object result = null;
String value = null;
value = evaluateBean(bean, field);
if (GenericValidator.isBlankOrNull(value)) {
return Boolean.TRUE;
}
Locale locale = RequestUtils.getUserLocale(request, null);
result = GenericTypeValidator.formatDouble(value, locale);
if (result == null) {
errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
}
return (result == null) ? Boolean.FALSE : result;
}
use of java.util.Locale in project sonarqube by SonarSource.
the class FieldChecks method validateLongLocale.
/**
* Checks if the field can safely be converted to a long primitive.
*
* @param bean The bean validation is being performed on.
* @param va The <code>ValidatorAction</code> that is currently
* being performed.
* @param field The <code>Field</code> object associated with the
* current field being validated.
* @param errors The <code>ActionMessages</code> object to add errors
* to if any validation errors occur.
* @param validator The <code>Validator</code> instance, used to access
* other field values.
* @param request Current request object.
* @return true if valid, false otherwise.
*/
public static Object validateLongLocale(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) {
Object result = null;
String value = null;
value = evaluateBean(bean, field);
if (GenericValidator.isBlankOrNull(value)) {
return Boolean.TRUE;
}
Locale locale = RequestUtils.getUserLocale(request, null);
result = GenericTypeValidator.formatLong(value, locale);
if (result == null) {
errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
}
return (result == null) ? Boolean.FALSE : result;
}
use of java.util.Locale 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;
}
use of java.util.Locale in project sonarqube by SonarSource.
the class MessageTag method doStartTag.
// --------------------------------------------------------- Public Methods
/**
* Process the start tag.
*
* @throws JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
String key = this.key;
if (key == null) {
// Look up the requested property value
Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
if ((value != null) && !(value instanceof String)) {
JspException e = new JspException(messages.getMessage("message.property", key));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
key = (String) value;
}
// Construct the optional arguments array we will be using
Object[] args = new Object[] { arg0, arg1, arg2, arg3, arg4 };
// Retrieve the message string we are looking for
String message = TagUtils.getInstance().message(pageContext, this.bundle, this.localeKey, key, args);
if (message == null) {
Locale locale = TagUtils.getInstance().getUserLocale(pageContext, this.localeKey);
String localeVal = (locale == null) ? "default locale" : locale.toString();
JspException e = new JspException(messages.getMessage("message.message", "\"" + key + "\"", "\"" + ((bundle == null) ? "(default bundle)" : bundle) + "\"", localeVal));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
TagUtils.getInstance().write(pageContext, message);
return (SKIP_BODY);
}
Aggregations