Commits

Anonymous committed f621a96

Added DTD for validator configs as suggested by Jasper Rosenberg
o added DTD mapping with validation into ValidatorFileParser
o added unit test for newly added DTD validation

Issue Number: XW-504

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1442e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork2/validator/ValidatorFileParser.java

         
         InputSource in = new InputSource(is);
         in.setSystemId(resourceName);
-            
-        Document doc = DomHelper.parse(in);
-        
-        NodeList nodes = doc.getElementsByTagName("validator");
-
-        for (int i = 0; i < nodes.getLength(); i++) {
-            Element validatorElement = (Element) nodes.item(i);
-            String name = validatorElement.getAttribute("name");
-            String className = validatorElement.getAttribute("class");
-
-            try {
-                // catch any problems here
-                objectFactory.buildValidator(className, new HashMap(), null);
-                ValidatorFactory.registerValidator(name, className);
-            } catch (Exception e) {
-                throw new XWorkException("Unable to load validator class " + className, e, validatorElement);
+
+        Map dtdMappings = new HashMap();
+        dtdMappings.put("-//OpenSymphony Group//XWork Validator Config 1.0//EN", "xwork-validator-config-1.0.dtd");
+
+        Document doc = DomHelper.parse(in, dtdMappings);
+
+        if (doc != null) {
+            NodeList nodes = doc.getElementsByTagName("validator");
+
+            for (int i = 0; i < nodes.getLength(); i++) {
+                Element validatorElement = (Element) nodes.item(i);
+                String name = validatorElement.getAttribute("name");
+                String className = validatorElement.getAttribute("class");
+
+                try {
+                    // catch any problems here
+                    objectFactory.buildValidator(className, new HashMap(), null);
+                    ValidatorFactory.registerValidator(name, className);
+                } catch (Exception e) {
+                    throw new XWorkException("Unable to load validator class " + className, e, validatorElement);
+                }
             }
         }
     }

src/test/com/opensymphony/xwork2/validator/ValidatorFileParserTest.java

     private static final String testFileName3 = "com/opensymphony/xwork2/validator/validator-parser-test3.xml";
     private static final String testFileName4 = "com/opensymphony/xwork2/validator/validator-parser-test4.xml";
     private static final String testFileName5 = "com/opensymphony/xwork2/validator/validator-parser-test5.xml";
+    private static final String testFileName6 = "com/opensymphony/xwork2/validator/validators-fail.xml";
 
     public void testParserActionLevelValidatorsShouldBeBeforeFieldLevelValidators() throws Exception {
         InputStream is = ClassLoaderUtil.getResourceAsStream(testFileName2, this.getClass());
         assertTrue("Validation file should have thrown exception", pass);
     }
 
+    public void testParserWithBadXML2() {
+        InputStream is = ClassLoaderUtil.getResourceAsStream(testFileName6, this.getClass());
+
+        boolean pass = false;
+        try {
+            ValidatorFileParser.parseValidatorDefinitions(is, testFileName6);
+        } catch (XWorkException ex) {
+            assertTrue("Wrong line number: " + ex.getLocation(), 8 == ex.getLocation().getLineNumber());
+            pass = true;
+        }
+        assertTrue("Validation file should have thrown exception", pass);
+    }
+
     public void testValidatorDefinitionsWithBadClassName() {
         InputStream is = ClassLoaderUtil.getResourceAsStream(testFileName5, this.getClass());
 

src/test/com/opensymphony/xwork2/validator/validators-fail.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE validators PUBLIC
+        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
+        "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
+<!-- START SNIPPET: validators-default -->
+<validators>
+    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
+    <validator nameFail="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
+    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
+    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
+    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
+    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
+    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
+    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
+    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
+    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
+    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
+    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
+    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
+</validators>
+<!--  END SNIPPET: validators-default -->