commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r833017 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/base/impl/ test/java/org/apache/commons/configuration2/base/impl/
Date Thu, 05 Nov 2009 07:32:09 GMT
Author: oheger
Date: Thu Nov  5 07:32:09 2009
New Revision: 833017

URL: http://svn.apache.org/viewvc?rev=833017&view=rev
Log:
Added a load(InputStream) method to XMLConfigurationSource, which allows the XML parser to
detect the encoding of the document to be loaded.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/impl/XMLConfigurationSource.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/impl/TestXMLConfigurationSource.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/impl/XMLConfigurationSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/impl/XMLConfigurationSource.java?rev=833017&r1=833016&r2=833017&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/impl/XMLConfigurationSource.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/impl/XMLConfigurationSource.java
Thu Nov  5 07:32:09 2009
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration2.base.impl;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -172,7 +173,30 @@
     public XMLConfigurationSource()
     {
         xmlNodeHandler = new XMLNodeHandler();
-        locatorSupport = new DefaultLocatorSupport(this);
+        locatorSupport = new DefaultLocatorSupport(this)
+        {
+            /**
+             * Loads data from the specified {@code InputStream}. This
+             * implementation directly delegates to the {@code
+             * load(InputStream)} method of the owning {@code
+             * XMLConfigurationSource}.
+             *
+             * @param in the {@code InputStream}
+             * @throws ConfigurationException if an error occurs
+             */
+            @Override
+            public void load(InputStream in) throws ConfigurationException
+            {
+                try
+                {
+                    XMLConfigurationSource.this.load(in);
+                }
+                catch (IOException ioex)
+                {
+                    throw new ConfigurationException(ioex);
+                }
+            }
+        };
     }
 
     /**
@@ -483,6 +507,23 @@
     }
 
     /**
+     * Loads the data from the specified {@code InputStream} and adds it to this
+     * {@code ConfigurationSource} object. Note that the{@code clear()} method
+     * is not called, so the properties contained in the loaded file will be
+     * added to the current set of properties. Note: This method is provided in
+     * addition to {@link #load(Reader)} because it allows the XML parser to
+     * auto-detect the encoding of the XML document to be loaded.
+     *
+     * @param in the input stream
+     * @throws IOException if an I/O error occurs
+     * @throws ConfigurationException if an error occurs
+     */
+    public void load(InputStream in) throws IOException, ConfigurationException
+    {
+        load(new InputSource(in));
+    }
+
+    /**
      * Writes the content of this configuration source to the specified {@code
      * Writer}. This implementation calls {@link #createTransformer()} for
      * obtaining a transformer. This transformer is then used to serialize the

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/impl/TestXMLConfigurationSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/impl/TestXMLConfigurationSource.java?rev=833017&r1=833016&r2=833017&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/impl/TestXMLConfigurationSource.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/impl/TestXMLConfigurationSource.java
Thu Nov  5 07:32:09 2009
@@ -773,6 +773,22 @@
     }
 
     /**
+     * Tests whether the encoding is taken into account when loading a document
+     * if it is explicitly specified.
+     */
+    @Test
+    public void testLoadWithEncodingExplicit() throws ConfigurationException,
+            IOException
+    {
+        LocatorSupport locSupport = source.getCapability(LocatorSupport.class);
+        locSupport.setEncoding("UTF-16");
+        locSupport.setLocator(new URLLocator(ConfigurationAssert
+                .getTestURL("testEncoding.xml")));
+        locSupport.load();
+        assertEquals("Wrong value", "test3_yoge", conf.getString("yoge"));
+    }
+
+    /**
      * Tests whether the encoding is correctly detected by the XML parser. This
      * is done by loading an XML file with the encoding "UTF-16". If this
      * encoding is not detected correctly, an exception will be thrown that
@@ -780,15 +796,13 @@
      * 34204.
      */
     @Test
-    public void testLoadWithEncoding() throws ConfigurationException,
-            IOException
+    public void testLoadWithEncoding() throws ConfigurationException
     {
         LocatorSupport locSupport = source.getCapability(LocatorSupport.class);
-        locSupport.setEncoding("UTF-16");
         locSupport.setLocator(new URLLocator(ConfigurationAssert
                 .getTestURL("testEncoding.xml")));
         locSupport.load();
-        assertEquals("Wrong value", "test3_yoge", conf.getString("yoge"));
+        assertEquals("test3_yoge", conf.getString("yoge"));
     }
 
     /**



Mime
View raw message