Search in sources :

Example 86 with Locale

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;
}
Also used : Locale(java.util.Locale)

Example 87 with Locale

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;
}
Also used : Locale(java.util.Locale)

Example 88 with Locale

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;
}
Also used : Locale(java.util.Locale)

Example 89 with Locale

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

Example 90 with Locale

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);
}
Also used : Locale(java.util.Locale) JspException(javax.servlet.jsp.JspException)

Aggregations

Locale (java.util.Locale)5854 Test (org.junit.Test)902 HashMap (java.util.HashMap)548 GenericValue (org.apache.ofbiz.entity.GenericValue)504 ArrayList (java.util.ArrayList)486 Delegator (org.apache.ofbiz.entity.Delegator)484 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)398 IOException (java.io.IOException)313 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)296 Date (java.util.Date)273 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)271 Map (java.util.Map)244 BigDecimal (java.math.BigDecimal)214 SimpleDateFormat (java.text.SimpleDateFormat)198 ResourceBundle (java.util.ResourceBundle)197 File (java.io.File)166 LinkedList (java.util.LinkedList)158 ULocale (android.icu.util.ULocale)156 List (java.util.List)147 Test (org.junit.jupiter.api.Test)132