You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ws...@apache.org on 2006/01/01 18:57:36 UTC

svn commit: r360506 - in /struts/shale/trunk: core-library/src/java/org/apache/shale/validator/ core-library/src/test/org/apache/shale/validator/ xdocs/

Author: wsmoak
Date: Sun Jan  1 09:57:28 2006
New Revision: 360506

URL: http://svn.apache.org/viewcvs?rev=360506&view=rev
Log:
Commons Validator integration enhancement.
Allow configuration of a comma delimited list of validation rules files under the init param "org.apache.shale.validator.VALIDATOR_RULES". The implementation comes from the Struts Action ValidatorPlugin.
Bug: 38044

Added:
    struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java   (with props)
    struts/shale/trunk/core-library/src/test/org/apache/shale/validator/ValidatorDefaultTestCase.java
    struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml   (with props)
Modified:
    struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
    struts/shale/trunk/core-library/src/test/org/apache/shale/validator/CommonsValidatorTestCase.java
    struts/shale/trunk/xdocs/features-commons-validator.xml

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java?rev=360506&r1=360505&r2=360506&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java Sun Jan  1 09:57:28 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2005 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -359,8 +359,10 @@
     /**
      * <p>Returns the Commons validator action that's appropriate
      *    for the validator with the given <code>name</code>. This method 
-     *    lazily configures validator resources by reading 
-     *    the <code>validalidator-rules.xml</code> file in shale-core.jar.
+     *    lazily configures validator resources by reading either
+     *    the default <code>validalidator-rules.xml</code> file in 
+     *    shale-core.jar or the list of resources configured using the init 
+     *    param <code>org.apache.shale.validator.VALIDATOR_RULES</code>.</p>
      *
      * @param name The name of the validator
      */
@@ -374,12 +376,34 @@
          = (ValidatorResources) applicationMap.get(VALIDATOR_RESOURCES_KEY);
       if (validatorResources == null) { 
          try {
-            String validatorRules = "/org/apache/shale/validator/validator-rules.xml";
-            URL input = external.getResource(validatorRules);
-            if (input == null) {
-               input = CommonsValidator.class.getResource(validatorRules);
+            String pathnames = external.getInitParameter(Globals.VALIDATOR_RULES);
+            if (pathnames == null || pathnames.length() <= 0) {
+               pathnames = Globals.DEFAULT_VALIDATOR_RULES;
             }
-            validatorResources = new ValidatorResources(input.toExternalForm());
+            StringTokenizer st = new StringTokenizer(pathnames, ",");
+            List urlList = new ArrayList();
+            while (st.hasMoreTokens()) {
+               String validatorRules = st.nextToken().trim();
+               logger.log(Level.INFO, "Loading validation rules file from '"
+                       + validatorRules + "'");
+               URL input = external.getResource(validatorRules);
+               if (input == null) {
+                  input = CommonsValidator.class.getResource(validatorRules);
+               }
+               if (input != null) {
+                  urlList.add(input);
+               } else {
+                  logger.log(Level.WARNING, "Skipping validation rules file from '"
+                          + validatorRules + "'.  No url could be located.");
+               }
+            }
+            int urlSize = urlList.size();
+            String[] urlArray = new String[urlSize];
+            for (int urlIndex = 0; urlIndex < urlSize; urlIndex++) {
+               URL url = (URL) urlList.get(urlIndex);
+               urlArray[urlIndex] = url.toExternalForm();
+            }
+            validatorResources = new ValidatorResources(urlArray);
             applicationMap.put(VALIDATOR_RESOURCES_KEY, validatorResources);
          } catch (IOException ex) {
             logger.log(Level.SEVERE, "can't initialize resources", ex);

Added: struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java?rev=360506&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java (added)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java Sun Jan  1 09:57:28 2006
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2004-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.validator;
+
+/**
+ * <p>Manifest constants that are global to the validator implementation.</p>
+ *
+ * $Id:$
+ */
+
+public class Globals {
+    
+    /**
+     * <p>Context initialization parameter used to specify a comma delimited
+     * list of context relative resource paths to resources containing our
+     * validator configuration information.</p>
+     */
+    public static final String VALIDATOR_RULES =
+      "org.apache.shale.validator.VALIDATOR_RULES";
+
+    /**
+     * <p>Location of the default Validator configuration file.
+     * The rules in this file will be loaded if no
+     * "org.apache.shale.validator.VALIDATOR_RULES"
+     * context initialization parameter is supplied</p>
+     */
+    public static final String DEFAULT_VALIDATOR_RULES =
+      "/org/apache/shale/validator/validator-rules.xml";
+
+}

Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/validator/Globals.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: struts/shale/trunk/core-library/src/test/org/apache/shale/validator/CommonsValidatorTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/validator/CommonsValidatorTestCase.java?rev=360506&r1=360505&r2=360506&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/validator/CommonsValidatorTestCase.java (original)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/validator/CommonsValidatorTestCase.java Sun Jan  1 09:57:28 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2005 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -46,6 +46,10 @@
     public void setUp() {
 
         super.setUp();
+        servletContext.addInitParameter
+            (Globals.VALIDATOR_RULES,
+             Globals.DEFAULT_VALIDATOR_RULES +
+             ", /org/apache/shale/validator/custom-rules.xml");
 
     }
 
@@ -75,12 +79,24 @@
     // Test access to the 'required' validation rule
     public void testRequired() {
 
-        ValidatorAction va = CommonsValidator.getValidatorAction( "required" );
+        ValidatorAction va = CommonsValidator.getValidatorAction("required");
         assertNotNull(va);
         assertEquals("required",va.getName());
         assertEquals("org.apache.shale.validator.CommonsValidator",
                 va.getClassname());
         assertEquals("isSupplied",va.getMethod());
+
+    }
+
+    // Test access to a custom validation rule
+    public void testCustom() {
+
+        ValidatorAction va = CommonsValidator.getValidatorAction("testRule");
+        assertNotNull(va);
+        assertEquals("testRule",va.getName());
+        assertEquals("com.example.myapp.CustomRules",
+                va.getClassname());
+        assertEquals("isValid",va.getMethod());
 
     }
 

Added: struts/shale/trunk/core-library/src/test/org/apache/shale/validator/ValidatorDefaultTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/validator/ValidatorDefaultTestCase.java?rev=360506&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/validator/ValidatorDefaultTestCase.java (added)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/validator/ValidatorDefaultTestCase.java Sun Jan  1 09:57:28 2006
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2004-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.validator;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.validator.ValidatorAction;
+
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+
+/**
+ * <p>Test case for <code>CommonsValidator</code>.</p>
+ */
+public class ValidatorDefaultTestCase extends AbstractJsfTestCase {
+
+
+    // ------------------------------------------------------------ Constructors
+
+
+    // Construct a new instance of this test case.
+    public ValidatorDefaultTestCase(String name) {
+        super(name);
+    }
+
+
+    // ---------------------------------------------------- Overall Test Methods
+
+
+    // Set up instance variables required by this test case.
+    public void setUp() {
+
+        super.setUp();
+
+    }
+
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(ValidatorDefaultTestCase.class));
+
+    }
+
+
+    // Tear down instance variables required by this test case.
+    public void tearDown() {
+
+        super.tearDown();
+
+    }
+
+
+    // ------------------------------------------------------ Instance Variables
+
+
+    // ------------------------------------------------------------ Test Methods
+
+
+    // Test access to the 'required' validation rule with default configuration
+    public void testRequired() {
+
+        ValidatorAction va = CommonsValidator.getValidatorAction( "required" );
+        assertNotNull(va);
+        assertEquals("required",va.getName());
+        assertEquals("org.apache.shale.validator.CommonsValidator",
+                va.getClassname());
+        assertEquals("isSupplied",va.getMethod());
+
+    }
+
+}

Added: struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml?rev=360506&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml (added)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml Sun Jan  1 09:57:28 2006
@@ -0,0 +1,17 @@
+<!DOCTYPE form-validation PUBLIC
+          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
+          "http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
+<form-validation>
+
+   <global>
+
+       <validator name="testRule"
+             classname="com.example.myapp.CustomRules"
+             method="isValid"
+             methodParams="java.lang.String"
+             msg="errors.invalid">
+       </validator>
+
+   </global>
+
+</form-validation>

Propchange: struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/shale/trunk/core-library/src/test/org/apache/shale/validator/custom-rules.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: struts/shale/trunk/xdocs/features-commons-validator.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/xdocs/features-commons-validator.xml?rev=360506&r1=360505&r2=360506&view=diff
==============================================================================
--- struts/shale/trunk/xdocs/features-commons-validator.xml (original)
+++ struts/shale/trunk/xdocs/features-commons-validator.xml Sun Jan  1 09:57:28 2006
@@ -109,6 +109,43 @@
 			  not explicitly specified.</i></p>
       </subsection>
 
+      <a name="validation-custom"/>
+      <subsection name="Using Custom Validaton Rules">
+      
+      <p>Shale's Commons Validator integration also allows you to use custom 
+      validation rules.</p>
+      
+      <p>In most cases, you will want to include the default rule set in
+      addition to your custom rules. In web.xml, use the
+      <code>org.apache.shale.validator.VALIDATOR_RULES</code> context
+      initialization parameter to specify a comma delimited list of files.</p>
+      
+<source>
+    &lt;!-- Shale Validator Configuration Resources -->
+    &lt;context-param>
+       &lt;param-name>org.apache.shale.validator.VALIDATOR_RULES&lt;/param-name>
+       &lt;param-value>
+           /org/apache/shale/validator/validator-rules.xml,
+           /WEB-INF/custom-rules.xml
+       &lt;/param-value>
+    &lt;/context-param>
+</source>
+
+      <p>In this example, the default validator-rules.xml file will be loaded
+      from the shale-core.jar archive, and the custom-rules.xml file will be
+      loaded from WEB-INF. If you need to replace one of the provided rules,
+      place a modified copy of 
+      <a href="http://svn.apache.org/repos/asf/struts/shale/trunk/core-library/src/conf/validator-rules.xml">validator-rules.xml</a>
+      in your webapp, and refer to it instead of the default file.</p>
+
+      <p>If you need to add messages for your own validation rules, or to
+      customize the 
+      <a href="http://svn.apache.org/repos/asf/struts/shale/trunk/core-library/src/java/org/apache/shale/validator/messages.properties">provided messages,</a>
+      configure a message bundle for your application in 
+      /WEB-INF/faces-config.xml.</p>
+      
+      </subsection>
+      
    </section>
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org