/** * Load <code>ValidatorResources</code> from multiple xml files. */ @Override protected void setUp() throws IOException, SAXException { InputStream[] streams = new InputStream[] { this.getClass().getResourceAsStream( "RetrieveFormTest-config.xml")}; this.resources = new ValidatorResources(streams); for (int i = 0; i < streams.length; i++) { streams[i].close(); } }
/** * <p>Gets a <code>Form</code> based on the name of the form and the * <code>Locale</code> that most closely matches the <code>Locale</code> * passed in. The order of <code>Locale</code> matching is:</p> * <ol> * <li>language + country + variant</li> * <li>language + country</li> * <li>language</li> * <li>default locale</li> * </ol> * @param locale The Locale. * @param formKey The key for the Form. * @return The validator Form. * @since Validator 1.1 */ public Form getForm(Locale locale, String formKey) { return this.getForm(locale.getLanguage(), locale.getCountry(), locale .getVariant(), formKey); }
private ValidatorResources setupDateResources(String property, String action) { ValidatorResources resources = new ValidatorResources(); ValidatorAction va = new ValidatorAction(); va.setName(action); va.setClassname("org.apache.commons.validator.ValidatorTest"); va.setMethod("formatDate"); va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field"); FormSet fs = new FormSet(); Form form = new Form(); form.setName("testForm"); Field field = new Field(); field.setProperty(property); field.setDepends(action); form.addField(field); fs.addForm(form); resources.addValidatorAction(va); resources.addFormSet(fs); resources.process(); return resources; }
/** * Add a <code>FormSet</code> to this <code>ValidatorResources</code> * object. It will be associated with the <code>Locale</code> of the * <code>FormSet</code>. * @param fs The form set to add. * @since Validator 1.1 */ public void addFormSet(FormSet fs) { String key = this.buildKey(fs); if (key.length() == 0) {// there can only be one default formset if (getLog().isWarnEnabled() && defaultFormSet != null) { // warn the user he might not get the expected results getLog().warn("Overriding default FormSet definition."); } defaultFormSet = fs; } else { FormSet formset = getFormSets().get(key); if (formset == null) {// it hasn't been included yet if (getLog().isDebugEnabled()) { getLog().debug("Adding FormSet '" + fs.toString() + "'."); } } else if (getLog().isWarnEnabled()) {// warn the user he might not // get the expected results getLog() .warn("Overriding FormSet definition. Duplicate for locale: " + key); } getFormSets().put(key, fs); } }
/** * Create a ValidatorResources object from several URL. * * @param urls An array of URL to several validation.xml * configuration files that will be read in order and merged into this object. * @throws SAXException if the validation XML files are not valid or well * formed. * @throws IOException if an I/O error occurs processing the XML files * @since Validator 1.3.1 */ public ValidatorResources(URL[] urls) throws IOException, SAXException { super(); Digester digester = initDigester(); for (int i = 0; i < urls.length; i++) { digester.push(this); digester.parse(urls[i]); } this.process(); }
/** * Initialize the digester. */ private Digester initDigester() { URL rulesUrl = this.getClass().getResource(VALIDATOR_RULES); if (rulesUrl == null) { // Fix for Issue# VALIDATOR-195 rulesUrl = ValidatorResources.class.getResource(VALIDATOR_RULES); } if (getLog().isDebugEnabled()) { getLog().debug("Loading rules from '" + rulesUrl + "'"); } Digester digester = DigesterLoader.createDigester(rulesUrl); digester.setNamespaceAware(true); digester.setValidating(true); digester.setUseContextClassLoader(true); // Add rules for arg0-arg3 elements addOldArgRules(digester); // register DTDs for (int i = 0; i < REGISTRATIONS.length; i += 2) { URL url = this.getClass().getResource(REGISTRATIONS[i + 1]); if (url != null) { digester.register(REGISTRATIONS[i], url.toString()); } } return digester; }
public Object invoke(MethodInvocation invocation) throws Throwable { String formKey; if (invocation.getArguments().length == 2) { formKey = (String) invocation.getArguments()[1]; } else { formKey = (String) invocation.getArguments()[3]; } if (formKey == null) { return invocation.proceed(); } ModuleConfig config = ModuleUtils.getInstance().getModuleConfig(S2StrutsContextUtil.getRequest()); Form form = this.validationCreator.createForm(config, formKey); if (form != null) { if (log.isDebugEnabled()) { log.debug("auto create " + form); } // initialize form... ValidatorResources resources = new ValidatorResources(); FormSet formSet = new FormSet(); formSet.addForm(form); resources.addFormSet(formSet); resources.process(); // ...initialized form return form; } return invocation.proceed(); }
/** * Tests loading ValidatorResources from a URL */ public void testParseURL() throws Exception { URL url = getClass().getResource("EntityImportTest-config.xml"); ValidatorResources resources = new ValidatorResources(url); assertNotNull("Form should be found", resources.getForm(Locale.getDefault(), "byteForm")); } }
/** * Performs validations based on the configured resources. * * @return The <code>Map</code> returned uses the property of the * <code>Field</code> for the key and the value is the number of error the * field had. * @throws ValidatorException If an error occurs during validation */ public ValidatorResults validate() throws ValidatorException { Locale locale = (Locale) this.getParameterValue(LOCALE_PARAM); if (locale == null) { locale = Locale.getDefault(); } this.setParameter(VALIDATOR_PARAM, this); Form form = this.resources.getForm(locale, this.formName); if (form != null) { this.setParameter(FORM_PARAM, form); return form.validate( this.parameters, this.resources.getValidatorActions(), this.page, this.fieldName); } return new ValidatorResults(); }
Form form = resources.getForm(locale, formName); if (form != null) { if ("true".equalsIgnoreCase(dynamicJavascript)) { ValidatorAction va = resources.getValidatorAction(depends);
FormSet formSet = resources.getFormSet(language, country, variant); if (formSet != null) { if (getLog().isDebugEnabled()) { resources.addFormSet(formSet);
public Map getValidatorActions() { return resources.getValidatorActions(); }
public ValidatorAction getValidatorAction(String arg0) { return resources.getValidatorAction(arg0); }
public void addFormSet(FormSet arg0) { resources.addFormSet(arg0); }
public void process() { resources.process(); }
/** * Add a <code>FormSet</code> to this <code>ValidatorResources</code> * object. It will be associated with the <code>Locale</code> of the * <code>FormSet</code>. * @param fs The form set to add. * @since Validator 1.1 */ public void addFormSet(FormSet fs) { String key = this.buildKey(fs); if (key.length() == 0) {// there can only be one default formset if (getLog().isWarnEnabled() && defaultFormSet != null) { // warn the user he might not get the expected results getLog().warn("Overriding default FormSet definition."); } defaultFormSet = fs; } else { FormSet formset = (FormSet) hFormSets.get(key); if (formset == null) {// it hasn't been included yet if (getLog().isDebugEnabled()) { getLog().debug("Adding FormSet '" + fs.toString() + "'."); } } else if (getLog().isWarnEnabled()) {// warn the user he might not // get the expected results getLog() .warn("Overriding FormSet definition. Duplicate for locale: " + key); } hFormSets.put(key, fs); } }
private void registerValidations(ModuleConfig config) { // Create Forms. ValidatorResources currentResources = getValidatorResources(config); FormSet formSet = new FormSet(); FormBeanConfig[] formConfigs = config.findFormBeanConfigs(); for (int i = 0; i < formConfigs.length; i++) { if (!registeredValidation(currentResources, formConfigs[i])) { Class clazz = ClassUtil.forName(formConfigs[i].getType()); String name = formConfigs[i].getName(); Form form = this.validationCreator.createForm(config, clazz, name); if (form != null) { formSet.addForm(form); if (log.isDebugEnabled()) { log.debug("auto register " + form); } } } } // Initialize ValidatorResources if (formSet.getForms().size() != 0) { ValidatorResources resources = new ValidatorResources(); resources.addFormSet(formSet); resources.process(); addValidatorResources(config, resources); } }
/** * Tests the entity import loading the <code>byteForm</code> form. */ public void testEntityImport() throws Exception { URL url = getClass().getResource("EntityImportTest-config.xml"); ValidatorResources resources = new ValidatorResources(url.toExternalForm()); assertNotNull("Form should be found", resources.getForm(Locale.getDefault(), "byteForm")); }
/** * Performs validations based on the configured resources. * * @return The <code>Map</code> returned uses the property of the * <code>Field</code> for the key and the value is the number of error the * field had. * @throws ValidatorException If an error occurs during validation */ public ValidatorResults validate() throws ValidatorException { Locale locale = (Locale) this.getParameterValue(LOCALE_PARAM); if (locale == null) { locale = Locale.getDefault(); } this.setParameter(VALIDATOR_PARAM, this); Form form = this.resources.getForm(locale, this.formName); if (form != null) { this.setParameter(FORM_PARAM, form); return form.validate( this.parameters, this.resources.getValidatorActions(), this.page, this.fieldName); } return new ValidatorResults(); }
/** * Create a ValidatorResources object from several uris * * @param uris An array of uris to several validation.xml * configuration files that will be read in order and merged into this object. * @throws SAXException if the validation XML files are not valid or well * formed. * @throws IOException if an I/O error occurs processing the XML files * @since Validator 1.2 */ public ValidatorResources(String[] uris) throws IOException, SAXException { super(); Digester digester = initDigester(); for (int i = 0; i < uris.length; i++) { digester.push(this); digester.parse(uris[i]); } this.process(); }