commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r729669 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/XMLConfiguration.java test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
Date Sat, 27 Dec 2008 20:11:02 GMT
Author: rgoers
Date: Sat Dec 27 12:11:02 2008
New Revision: 729669

URL: http://svn.apache.org/viewvc?rev=729669&view=rev
Log:
Validate XML document against schema when saving the file

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java?rev=729669&r1=729668&r2=729669&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
Sat Dec 27 12:11:02 2008
@@ -17,11 +17,7 @@
 
 package org.apache.commons.configuration2;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.Writer;
+import java.io.*;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -888,8 +884,38 @@
         {
             Transformer transformer = createTransformer();
             Source source = new DOMSource(createDocument());
-            Result result = new StreamResult(writer);
+            Result result;
+            StringWriter buffer = null;
+            if (isSchemaValidation())
+            {
+                buffer = new StringWriter();
+                result = new StreamResult(buffer);
+            }
+            else
+            {
+                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)
+        {
+            throw new ConfigurationException("Unable to save the configuration", e);
+        }
+        catch (ParserConfigurationException e)
+        {
+            throw new ConfigurationException("Unable to save the configuration", e);
+        }
+        catch (IOException e)
+        {
+            throw new ConfigurationException("Unable to save the configuration", e);
         }
         catch (TransformerException e)
         {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java?rev=729669&r1=729668&r2=729669&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
Sat Dec 27 12:11:02 2008
@@ -40,6 +40,7 @@
 import org.apache.commons.configuration2.reloading.InvariantReloadingStrategy;
 import org.apache.commons.configuration2.tree.ConfigurationNode;
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
+import org.apache.commons.configuration2.resolver.CatalogResolver;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -51,6 +52,8 @@
  */
 public class TestXMLConfiguration extends TestCase
 {
+    /** XML Catalog */
+    private static final String CATALOG_FILES = ConfigurationAssert.getTestFile("catalog.xml").getAbsolutePath();
     /** Constant for the used encoding.*/
     static final String ENCODING = "ISO-8859-1";
 
@@ -74,6 +77,7 @@
     private String testProperties2 = ConfigurationAssert.getTestFile("testDigesterConfigurationInclude1.xml").getAbsolutePath();
     private String testBasePath = ConfigurationAssert.TEST_DIR.getAbsolutePath();
     private File testSaveConf = ConfigurationAssert.getOutFile("testsave.xml");
+    private String testFile2 = ConfigurationAssert.getTestFile("sample.xml").getAbsolutePath();
 
     private XMLConfiguration conf;
 
@@ -1414,6 +1418,49 @@
         assertEquals("Invalid not trimmed", "Some other text", conf
                 .getString("space.testInvalid"));
     }
+    /**
+     * Tests modifying an XML document and saving it with schema validation enabled.
+     */
+    public void testSaveWithValidation() throws Exception
+    {
+        CatalogResolver resolver = new CatalogResolver();
+        resolver.setCatalogFiles(CATALOG_FILES);
+        conf = new XMLConfiguration();
+        conf.setEntityResolver(resolver);
+        conf.setFileName(testFile2);
+        conf.setSchemaValidation(true);
+        conf.load();
+        conf.setProperty("Employee.SSN", "123456789");
+        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.
+     */
+    public void testSaveWithValidationFailure() throws Exception
+    {
+        CatalogResolver resolver = new CatalogResolver();
+        resolver.setCatalogFiles(CATALOG_FILES);
+        conf = new XMLConfiguration();
+        conf.setEntityResolver(resolver);
+        conf.setFileName(testFile2);
+        conf.setSchemaValidation(true);
+        conf.load();
+        conf.setProperty("Employee.Email", "JohnDoe@apache.org");
+        try
+        {
+            conf.save(testSaveConf);
+            fail("No validation failure on save");
+        }
+        catch (Exception e)
+        {
+            Throwable cause = e.getCause();
+            assertNotNull("No cause for exception on save", cause);
+            assertTrue("Incorrect exception on save", cause instanceof SAXParseException);
+        }
+    }
 
     /**
      * Prepares a configuration object for testing a reload operation.



Mime
View raw message