Search in sources :

Example 1 with ValidatorResources

use of org.apache.commons.validator.ValidatorResources in project sonarqube by SonarSource.

the class ValidatorPlugIn method initResources.

/**
     * Initialize the validator resources for this module.
     *
     * @throws IOException      if an input/output error is encountered
     * @throws ServletException if we cannot initialize these resources
     */
protected void initResources() throws IOException, ServletException {
    if ((pathnames == null) || (pathnames.length() <= 0)) {
        return;
    }
    StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
    List urlList = new ArrayList();
    try {
        while (st.hasMoreTokens()) {
            String validatorRules = st.nextToken().trim();
            if (log.isInfoEnabled()) {
                log.info("Loading validation rules file from '" + validatorRules + "'");
            }
            URL input = servlet.getServletContext().getResource(validatorRules);
            // loader which allows the config files to be stored in a jar
            if (input == null) {
                input = getClass().getResource(validatorRules);
            }
            if (input != null) {
                urlList.add(input);
            } else {
                throw new ServletException("Skipping validation rules file from '" + validatorRules + "'.  No url could be located.");
            }
        }
        int urlSize = urlList.size();
        URL[] urlArray = new URL[urlSize];
        for (int urlIndex = 0; urlIndex < urlSize; urlIndex++) {
            urlArray[urlIndex] = (URL) urlList.get(urlIndex);
        }
        this.resources = new ValidatorResources(urlArray);
    } catch (SAXException sex) {
        log.error("Skipping all validation", sex);
        throw new ServletException(sex);
    }
}
Also used : ServletException(javax.servlet.ServletException) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) ValidatorResources(org.apache.commons.validator.ValidatorResources) ArrayList(java.util.ArrayList) List(java.util.List) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 2 with ValidatorResources

use of org.apache.commons.validator.ValidatorResources in project sonar-java by SonarSource.

the class JavascriptValidatorTag method renderJavascript.

/**
 * Returns fully rendered JavaScript.
 *
 * @since Struts 1.2
 */
protected String renderJavascript() throws JspException {
    StringBuffer results = new StringBuffer();
    ModuleConfig config = TagUtils.getInstance().getModuleConfig(pageContext);
    ValidatorResources resources = (ValidatorResources) pageContext.getAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(), PageContext.APPLICATION_SCOPE);
    if (resources == null) {
        throw new JspException("ValidatorResources not found in application scope under key \"" + ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix() + "\"");
    }
    Locale locale = TagUtils.getInstance().getUserLocale(this.pageContext, null);
    Form form = null;
    if ("true".equalsIgnoreCase(dynamicJavascript)) {
        form = resources.getForm(locale, formName);
        if (form == null) {
            throw new JspException("No form found under '" + formName + "' in locale '" + locale + "'.  A form must be defined in the " + "Commons Validator configuration when " + "dynamicJavascript=\"true\" is set.");
        }
    }
    if (form != null) {
        if ("true".equalsIgnoreCase(dynamicJavascript)) {
            results.append(this.createDynamicJavascript(config, resources, locale, form));
        } else if ("true".equalsIgnoreCase(staticJavascript)) {
            results.append(this.renderStartElement());
            if ("true".equalsIgnoreCase(htmlComment)) {
                results.append(HTML_BEGIN_COMMENT);
            }
        }
    }
    if ("true".equalsIgnoreCase(staticJavascript)) {
        results.append(getJavascriptStaticMethods(resources));
    }
    if ((form != null) && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) {
        results.append(getJavascriptEnd());
    }
    return results.toString();
}
Also used : Locale(java.util.Locale) JspException(javax.servlet.jsp.JspException) Form(org.apache.commons.validator.Form) ValidatorResources(org.apache.commons.validator.ValidatorResources) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 3 with ValidatorResources

use of org.apache.commons.validator.ValidatorResources in project sonar-java by SonarSource.

the class Resources method initValidator.

/**
 * Initialize the <code>Validator</code> to perform validation.
 *
 * @param key         The key that the validation rules are under (the
 *                    form elements name attribute).
 * @param bean        The bean validation is being performed on.
 * @param application servlet context
 * @param request     The current request object.
 * @param errors      The object any errors will be stored in.
 * @param page        This in conjunction with  the page property of a
 *                    <code>Field<code> can control the processing of
 *                    fields.  If the field's page is less than or equal
 *                    to this page value, it will be processed.
 */
public static Validator initValidator(String key, Object bean, ServletContext application, HttpServletRequest request, ActionMessages errors, int page) {
    ValidatorResources resources = Resources.getValidatorResources(application, request);
    Locale locale = RequestUtils.getUserLocale(request, null);
    Validator validator = new Validator(resources, key);
    validator.setUseContextClassLoader(true);
    validator.setPage(page);
    validator.setParameter(SERVLET_CONTEXT_PARAM, application);
    validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
    validator.setParameter(Validator.LOCALE_PARAM, locale);
    validator.setParameter(ACTION_MESSAGES_PARAM, errors);
    validator.setParameter(Validator.BEAN_PARAM, bean);
    return validator;
}
Also used : Locale(java.util.Locale) ValidatorResources(org.apache.commons.validator.ValidatorResources) Validator(org.apache.commons.validator.Validator)

Example 4 with ValidatorResources

use of org.apache.commons.validator.ValidatorResources in project sonar-java by SonarSource.

the class ValidatorPlugIn method initResources.

/**
 * Initialize the validator resources for this module.
 *
 * @throws IOException      if an input/output error is encountered
 * @throws ServletException if we cannot initialize these resources
 */
protected void initResources() throws IOException, ServletException {
    if ((pathnames == null) || (pathnames.length() <= 0)) {
        return;
    }
    StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
    List urlList = new ArrayList();
    try {
        while (st.hasMoreTokens()) {
            String validatorRules = st.nextToken().trim();
            if (log.isInfoEnabled()) {
                log.info("Loading validation rules file from '" + validatorRules + "'");
            }
            URL input = servlet.getServletContext().getResource(validatorRules);
            // loader which allows the config files to be stored in a jar
            if (input == null) {
                input = getClass().getResource(validatorRules);
            }
            if (input != null) {
                urlList.add(input);
            } else {
                throw new ServletException("Skipping validation rules file from '" + validatorRules + "'.  No url could be located.");
            }
        }
        int urlSize = urlList.size();
        URL[] urlArray = new URL[urlSize];
        for (int urlIndex = 0; urlIndex < urlSize; urlIndex++) {
            urlArray[urlIndex] = (URL) urlList.get(urlIndex);
        }
        this.resources = new ValidatorResources(urlArray);
    } catch (SAXException sex) {
        log.error("Skipping all validation", sex);
        throw new ServletException(sex);
    }
}
Also used : ServletException(javax.servlet.ServletException) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) ValidatorResources(org.apache.commons.validator.ValidatorResources) ArrayList(java.util.ArrayList) List(java.util.List) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 5 with ValidatorResources

use of org.apache.commons.validator.ValidatorResources in project sonarqube by SonarSource.

the class Resources method initValidator.

/**
     * Initialize the <code>Validator</code> to perform validation.
     *
     * @param key         The key that the validation rules are under (the
     *                    form elements name attribute).
     * @param bean        The bean validation is being performed on.
     * @param application servlet context
     * @param request     The current request object.
     * @param errors      The object any errors will be stored in.
     * @param page        This in conjunction with  the page property of a
     *                    <code>Field<code> can control the processing of
     *                    fields.  If the field's page is less than or equal
     *                    to this page value, it will be processed.
     */
public static Validator initValidator(String key, Object bean, ServletContext application, HttpServletRequest request, ActionMessages errors, int page) {
    ValidatorResources resources = Resources.getValidatorResources(application, request);
    Locale locale = RequestUtils.getUserLocale(request, null);
    Validator validator = new Validator(resources, key);
    validator.setUseContextClassLoader(true);
    validator.setPage(page);
    validator.setParameter(SERVLET_CONTEXT_PARAM, application);
    validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
    validator.setParameter(Validator.LOCALE_PARAM, locale);
    validator.setParameter(ACTION_MESSAGES_PARAM, errors);
    validator.setParameter(Validator.BEAN_PARAM, bean);
    return validator;
}
Also used : Locale(java.util.Locale) ValidatorResources(org.apache.commons.validator.ValidatorResources) Validator(org.apache.commons.validator.Validator)

Aggregations

ValidatorResources (org.apache.commons.validator.ValidatorResources)6 Locale (java.util.Locale)4 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)2 ServletException (javax.servlet.ServletException)2 JspException (javax.servlet.jsp.JspException)2 Form (org.apache.commons.validator.Form)2 Validator (org.apache.commons.validator.Validator)2 ModuleConfig (org.apache.struts.config.ModuleConfig)2 SAXException (org.xml.sax.SAXException)2