commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r289160 - in /jakarta/commons/proper/validator/trunk: conf/share/ src/share/org/apache/commons/validator/ src/test/org/apache/commons/validator/
Date Thu, 15 Sep 2005 05:48:47 GMT
Author: niallp
Date: Wed Sep 14 22:48:33 2005
New Revision: 289160

URL: http://svn.apache.org/viewcvs?rev=289160&view=rev
Log:
Bug #32522 Enable variables <var> to come from Resource Bundles.

In commons-validator this just means adding "resource" and "bundle" attributes to the DTD
along with corresponding properties in org.apache.commons.validator.Var. Its up to down-stream
systems, such as Struts to use these attributes to retrieve the values from the appropriate
resource.

Added:
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
  (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
  (with props)
Modified:
    jakarta/commons/proper/validator/trunk/conf/share/validator_1_2_0.dtd
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Var.java
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java

Modified: jakarta/commons/proper/validator/trunk/conf/share/validator_1_2_0.dtd
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/conf/share/validator_1_2_0.dtd?rev=289160&r1=289159&r2=289160&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/conf/share/validator_1_2_0.dtd (original)
+++ jakarta/commons/proper/validator/trunk/conf/share/validator_1_2_0.dtd Wed Sep 14 22:48:33
2005
@@ -202,8 +202,15 @@
      one of its validators, such as the minimum and maximum values in a
      range validation. These parameters may also be referenced by one of the
      arg? elements using a shell syntax: ${var:var-name}.
+
+     N.B. resource ='true' indicates that the var-value is a resource key,
+          with the option to specify the bundle name. Validator doesn't do
+          anything itself with these values. Down-stream systems need
+          to resolve resource values.
 -->
 <!ELEMENT var (var-name, var-value, var-jstype?)>
+<!ATTLIST var resource CDATA #IMPLIED>
+<!ATTLIST var bundle CDATA #IMPLIED>
 
 
 

Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Var.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Var.java?rev=289160&r1=289159&r2=289160&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Var.java
(original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Var.java
Wed Sep 14 22:48:33 2005
@@ -54,7 +54,7 @@
     private String name = null;
 
     /**
-     * The name of the value.
+     * The key or value the variable.
      */
     private String value = null;
 
@@ -64,6 +64,16 @@
     private String jsType = null;
 
     /**
+     * Whether the variable is a resource [false]
+     */
+    private boolean resource = false;
+
+    /**
+     * The bundle for a variable (when resource = 'true').
+     */
+    private String bundle = null;
+
+    /**
      * Default Constructor.
      */
     public Var() {
@@ -116,6 +126,42 @@
     }
 
     /**
+     * Tests whether or not the value is a resource key or literal value.
+     * @return <code>true</code> if value is a resource key.
+     * @since Validator 1.2.0
+     */
+    public boolean isResource() {
+        return this.resource;
+    }
+
+    /**
+     * Sets whether or not the value is a resource.
+     * @param resource If true indicates the value is a resource.
+     * @since Validator 1.2.0
+     */
+    public void setResource(boolean resource) {
+        this.resource = resource;
+    }
+
+    /**
+     * Returns the resource bundle name.
+     * @return The bundle name.
+     * @since Validator 1.2.0
+     */
+    public String getBundle() {
+        return this.bundle;
+    }
+
+    /**
+     * Sets the resource bundle name.
+     * @param bundle The new bundle name.
+     * @since Validator 1.2.0
+     */
+    public void setBundle(String bundle) {
+        this.bundle = bundle;
+    }
+
+    /**
      * Gets the JavaScript type of the variable.
      * @return The Javascript type of the variable.
      */
@@ -155,6 +201,12 @@
         results.append(name);
         results.append("  value=");
         results.append(value);
+        results.append("  resource=");
+        results.append(resource);
+        if (resource) {
+            results.append("  bundle=");
+            results.append(bundle);
+        }
         results.append("  jsType=");
         results.append(jsType);
         results.append("\n");

Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml?rev=289160&r1=289159&r2=289160&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml
(original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml
Wed Sep 14 22:48:33 2005
@@ -53,10 +53,18 @@
 				<set-next-rule methodname="addField" paramtype="org.apache.commons.validator.Field"
/>
 				
 				<pattern value="var">
-					<call-method-rule methodname="addVar" paramcount="3" />
-					<call-param-rule pattern="var-name" paramnumber="0" />
-					<call-param-rule pattern="var-value" paramnumber="1" />
-					<call-param-rule pattern="var-jstype" paramnumber="2" />
+					<object-create-rule classname="org.apache.commons.validator.Var" />
+					<set-properties-rule/>
+					<pattern value="var-name">
+						<call-method-rule methodname="setName" paramcount="0" />
+					</pattern>
+					<pattern value="var-value">
+						<call-method-rule methodname="setValue" paramcount="0" />
+					</pattern>
+					<pattern value="var-jstype">
+						<call-method-rule methodname="setJsType" paramcount="0" />
+					</pattern>
+					<set-next-rule methodname="addVar" paramtype="org.apache.commons.validator.Var" />
 				</pattern>
 				
 				<pattern value="msg">

Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java?rev=289160&r1=289159&r2=289160&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java
(original)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTestSuite.java
Wed Sep 14 22:48:33 2005
@@ -63,6 +63,7 @@
        suite.addTestSuite(FlagsTest.class);
        suite.addTestSuite(ExceptionTest.class);
        suite.addTest(UrlTest.suite());
+       suite.addTest(VarTest.suite());
 
        return suite;
     }

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java?rev=289160&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
(added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
Wed Sep 14 22:48:33 2005
@@ -0,0 +1,130 @@
+/*
+ * $Id$
+ * $Rev$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2001-2005 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.commons.validator;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import java.util.Locale;
+import java.io.IOException;
+import org.xml.sax.SAXException;
+
+/**                                                       
+ * Test that the new Var attributes and the
+ * digester rule changes work.
+ */
+public class VarTest extends TestCommon {
+
+   /**
+    * The key used to retrieve the set of validation
+    * rules from the xml file.
+    */
+   protected static String FORM_KEY = "testForm";
+
+   /**
+    * The key used to retrieve the validator action.
+    */
+   protected static String ACTION = "byte";
+
+
+
+   public VarTest(String name) {
+       super(name);
+   }
+
+   /**
+    * Start the tests.
+    *
+    * @param theArgs the arguments. Not used
+    */
+   public static void main(String[] theArgs) {
+       junit.awtui.TestRunner.main(new String[] {VarTest.class.getName()});
+   }
+
+   /**
+    * @return a test suite (<code>TestSuite</code>) that includes all methods
+    *         starting with "test"
+    */
+   public static Test suite() {
+       // All methods starting with "test" will be executed in the test suite.
+       return new TestSuite(VarTest.class);
+   }
+
+   /**
+    * Load <code>ValidatorResources</code> from
+    * validator-multipletest.xml.
+    */
+   protected void setUp() throws IOException, SAXException {
+      // Load resources
+      loadResources("validator-var-test.xml");
+   }
+
+   protected void tearDown() {
+   }
+
+   /**
+    * With nothing provided, we should fail both because both are required.
+    */
+   public void testVars() throws ValidatorException {
+
+       Form form = resources.getForm(Locale.getDefault(), FORM_KEY);
+
+       // Get field 1
+       Field field1 = form.getField("field-1");
+       assertNotNull("field-1 is null.", field1);
+       assertEquals("field-1 property is wrong", "field-1", field1.getProperty());
+
+       // Get var-1-1
+       Var var11 = field1.getVar("var-1-1");
+       assertNotNull("var-1-1 is null.", var11);
+       assertEquals("var-1-1 name is wrong", "var-1-1", var11.getName());
+       assertEquals("var-1-1 value is wrong", "value-1-1", var11.getValue());
+       assertEquals("var-1-1 jstype is wrong", "jstype-1-1", var11.getJsType());
+       assertFalse("var-1-1 resource is true", var11.isResource());
+       assertNull("var-1-1 bundle is not null.", var11.getBundle());
+
+       // Get field 2
+       Field field2 = form.getField("field-2");
+       assertNotNull("field-2 is null.", field2);
+       assertEquals("field-2 property is wrong", "field-2", field2.getProperty());
+
+       // Get var-2-1
+       Var var21 = field2.getVar("var-2-1");
+       assertNotNull("var-2-1 is null.", var21);
+       assertEquals("var-2-1 name is wrong", "var-2-1", var21.getName());
+       assertEquals("var-2-1 value is wrong", "value-2-1", var21.getValue());
+       assertEquals("var-2-1 jstype is wrong", "jstype-2-1", var21.getJsType());
+       assertTrue("var-2-1 resource is false", var21.isResource());
+       assertEquals("var-2-1 bundle is wrong", "bundle-2-1", var21.getBundle());
+
+       // Get var-2-2
+       Var var22 = field2.getVar("var-2-2");
+       assertNotNull("var-2-2 is null.", var22);
+       assertEquals("var-2-2 name is wrong", "var-2-2", var22.getName());
+       assertEquals("var-2-2 value is wrong", "value-2-2", var22.getValue());
+       assertNull("var-2-2 jstype is not null", var22.getJsType());
+       assertFalse("var-2-2 resource is true", var22.isResource());
+       assertEquals("var-2-2 bundle is wrong", "bundle-2-2", var22.getBundle());
+
+   }
+
+}                                                         
\ No newline at end of file

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/VarTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml?rev=289160&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
(added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
Wed Sep 14 22:48:33 2005
@@ -0,0 +1,36 @@
+<!DOCTYPE form-validation PUBLIC
+     "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN"
+     "http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
+     
+<form-validation>
+   <global>
+      <validator name="byte"
+                 classname="org.apache.commons.validator.TestValidator"
+                 method="validateByte"
+                 methodParams="java.lang.Object,org.apache.commons.validator.Field"
+                 msg=""/>
+   </global>
+
+   <formset>
+      <form name="testForm">
+         <field property="field-1" depends="byte">
+             <var>
+                 <var-name>var-1-1</var-name>
+                 <var-value>value-1-1</var-value>
+                 <var-jstype>jstype-1-1</var-jstype>
+             </var>
+         </field>
+         <field property="field-2" depends="byte">
+             <var resource="true" bundle="bundle-2-1">
+                 <var-name>var-2-1</var-name>
+                 <var-value>value-2-1</var-value>
+                 <var-jstype>jstype-2-1</var-jstype>
+             </var>
+             <var resource="false" bundle="bundle-2-2">
+                 <var-name>var-2-2</var-name>
+                 <var-value>value-2-2</var-value>
+             </var>
+         </field>
+      </form>
+   </formset>   
+</form-validation>

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/validator-var-test.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



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


Mime
View raw message