commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r737778 - in /commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/XMLConfiguration.java test/org/apache/commons/configuration/TestXMLConfiguration.java
Date Mon, 26 Jan 2009 18:40:43 GMT
Author: rgoers
Date: Mon Jan 26 18:40:43 2009
New Revision: 737778

URL: http://svn.apache.org/viewvc?rev=737778&view=rev
Log:
Do not validate on save. Add validate method

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=737778&r1=737777&r2=737778&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
Mon Jan 26 18:40:43 2009
@@ -954,46 +954,51 @@
         {
             Transformer transformer = createTransformer();
             Source source = new DOMSource(createDocument());
-            Result result;
-            StringWriter buffer = null;
-            if (isSchemaValidation())
-            {
-                buffer = new StringWriter();
-                result = new StreamResult(buffer);
-            }
-            else
-            {
-                result = new StreamResult(writer);
-            }
+            Result result = new StreamResult(writer);
             transformer.transform(source, result);
-            if (isSchemaValidation())
-            {
-                DocumentBuilder builder = createDocumentBuilder();
-                Reader reader = new StringReader(buffer.getBuffer().toString());
-                builder.parse(new InputSource(reader));
-                writer.write(buffer.getBuffer().toString());
-                writer.close();
-            }
         }
-        catch (SAXException e)
+        catch (TransformerException e)
         {
             throw new ConfigurationException("Unable to save the configuration", e);
         }
-        catch (ParserConfigurationException e)
+        catch (TransformerFactoryConfigurationError e)
         {
             throw new ConfigurationException("Unable to save the configuration", e);
         }
+    }
+
+    /**
+     * Validate the document against the Schema.
+     * @throws ConfigurationException if the validation fails.
+     */
+    public void validate() throws ConfigurationException
+    {
+        try
+        {
+            Transformer transformer = createTransformer();
+            Source source = new DOMSource(createDocument());
+            StringWriter writer = new StringWriter();
+            Result result = new StreamResult(writer);
+            transformer.transform(source, result);
+            Reader reader = new StringReader(writer.getBuffer().toString());
+            DocumentBuilder builder = createDocumentBuilder();
+            builder.parse(new InputSource(reader));
+        }
+        catch (SAXException e)
+        {
+            throw new ConfigurationException("Validation failed", e);
+        }
         catch (IOException e)
         {
-            throw new ConfigurationException("Unable to save the configuration", e);
+            throw new ConfigurationException("Validation failed", e);
         }
         catch (TransformerException e)
         {
-            throw new ConfigurationException("Unable to save the configuration", e);
+            throw new ConfigurationException("Validation failed", e);
         }
-        catch (TransformerFactoryConfigurationError e)
+        catch (ParserConfigurationException pce)
         {
-            throw new ConfigurationException("Unable to save the configuration", e);
+            throw new ConfigurationException("Validation failed", pce);
         }
     }
 

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?rev=737778&r1=737777&r2=737778&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
Mon Jan 26 18:40:43 2009
@@ -1491,13 +1491,14 @@
         conf.setSchemaValidation(true);
         conf.load();
         conf.setProperty("Employee.SSN", "123456789");
+        conf.validate();
         conf.save(testSaveConf);
         conf = new XMLConfiguration(testSaveConf);
         assertEquals("123456789", conf.getString("Employee.SSN"));
     }
 
     /**
-     * Tests modifying an XML document and saving it with schema validation enabled.
+     * Tests modifying an XML document and validating it against the schema.
      */
     public void testSaveWithValidationFailure() throws Exception
     {
@@ -1511,7 +1512,7 @@
         conf.setProperty("Employee.Email", "JohnDoe@apache.org");
         try
         {
-            conf.save(testSaveConf);
+            conf.validate();
             fail("No validation failure on save");
         }
         catch (Exception e)



Mime
View raw message