You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2005/11/05 17:36:10 UTC

svn commit: r331008 - in /jakarta/commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java src/java/org/apache/commons/configuration/XMLConfiguration.java xdocs/changes.xml

Author: oheger
Date: Sat Nov  5 08:35:57 2005
New Revision: 331008

URL: http://svn.apache.org/viewcvs?rev=331008&view=rev
Log:
XMLConfiguration now extends AbstractHierarchicalFileConfiguration

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java?rev=331008&r1=331007&r2=331008&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java Sat Nov  5 08:35:57 2005
@@ -22,6 +22,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
+import java.util.Iterator;
 
 import org.apache.commons.configuration.reloading.ReloadingStrategy;
 
@@ -33,30 +34,18 @@
  * @author Emmanuel Bourg
  * @version $Revision$, $Date$
  */
-public abstract class AbstractHierarchicalFileConfiguration extends HierarchicalConfiguration implements FileConfiguration
+public abstract class AbstractHierarchicalFileConfiguration
+extends HierarchicalConfiguration implements FileConfiguration
 {
-    protected AbstractHierarchicalFileConfiguration.FileConfigurationDelegate delegate = new AbstractHierarchicalFileConfiguration.FileConfigurationDelegate();
+    /** Stores the delegate used for implementing functionality related to the
+     * <code>FileConfiguration</code> interface.
+     */
+    private FileConfigurationDelegate delegate = createDelegate();
 
-    protected class FileConfigurationDelegate extends AbstractFileConfiguration
+    protected AbstractHierarchicalFileConfiguration()
     {
-        public void load(Reader in) throws ConfigurationException
-        {
-            AbstractHierarchicalFileConfiguration.this.load(in);
-        }
-
-        public void save(Writer out) throws ConfigurationException
-        {
-            AbstractHierarchicalFileConfiguration.this.save(out);
-        }
-
-        public void clear()
-        {
-            AbstractHierarchicalFileConfiguration.this.clear();
-        }
     }
 
-    protected AbstractHierarchicalFileConfiguration() { }
-
     /**
      * Creates and loads the configuration from the specified file.
      *
@@ -264,4 +253,83 @@
         delegate.setEncoding(encoding);
     }
 
+    public boolean containsKey(String key)
+    {
+        reload();
+        return super.containsKey(key);
+    }
+
+    public Iterator getKeys(String prefix)
+    {
+        reload();
+        return super.getKeys(prefix);
+    }
+
+    public Object getProperty(String key)
+    {
+        reload();
+        return super.getProperty(key);
+    }
+
+    public boolean isEmpty()
+    {
+        reload();
+        return super.isEmpty();
+    }
+
+    /**
+     * Creates the file configuration delegate, i.e. the object that implements
+     * functionality required by the <code>FileConfiguration</code> interface.
+     * This base implementation will return an instance of the
+     * <code>FileConfigurationDelegate</code> class. Derived classes may
+     * override it to create a different delegate object.
+     *
+     * @return the file configuration delegate
+     */
+    protected FileConfigurationDelegate createDelegate()
+    {
+        return new FileConfigurationDelegate();
+    }
+
+    /**
+     * Returns the file configuration delegate.
+     *
+     * @return the delegate
+     */
+    protected FileConfigurationDelegate getDelegate()
+    {
+        return delegate;
+    }
+
+    /**
+     * Allows to set the file configuration delegate.
+     * @param delegate the new delegate
+     */
+    protected void setDelegate(FileConfigurationDelegate delegate)
+    {
+        this.delegate = delegate;
+    }
+
+    /**
+     * A special implementation of the <code>FileConfiguration</code> interface that is
+     * used internally to implement the <code>FileConfiguration</code> methods
+     * for hierarchical configurations.
+     */
+    protected class FileConfigurationDelegate extends AbstractFileConfiguration
+    {
+        public void load(Reader in) throws ConfigurationException
+        {
+            AbstractHierarchicalFileConfiguration.this.load(in);
+        }
+
+        public void save(Writer out) throws ConfigurationException
+        {
+            AbstractHierarchicalFileConfiguration.this.save(out);
+        }
+
+        public void clear()
+        {
+            AbstractHierarchicalFileConfiguration.this.clear();
+        }
+    }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=331008&r1=331007&r2=331008&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Sat Nov  5 08:35:57 2005
@@ -18,7 +18,6 @@
 
 import java.io.File;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
@@ -51,7 +50,6 @@
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
-import org.apache.commons.configuration.reloading.ReloadingStrategy;
 
 /**
  * <p>A specialized hierarchical configuration class that is able to parse XML
@@ -75,14 +73,11 @@
  * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger </a>
  * @version $Revision$, $Date$
  */
-public class XMLConfiguration extends HierarchicalConfiguration implements FileConfiguration
+public class XMLConfiguration extends AbstractHierarchicalFileConfiguration
 {
     /** Constant for the default root element name. */
     private static final String DEFAULT_ROOT_NAME = "configuration";
 
-    /** A helper object for implementing the file configuration interface. */
-    private FileConfigurationDelegate delegate = new FileConfigurationDelegate();
-
     /** The document from this configuration's data source. */
     private Document document;
 
@@ -255,40 +250,6 @@
     }
 
     /**
-     * Adds a property to this configuration.
-     *
-     * @param key the key of the property
-     * @param obj the property's value
-     */
-    protected void addPropertyDirect(String key, Object obj)
-    {
-        super.addPropertyDirect(key, obj);
-        delegate.possiblySave();
-    }
-
-    /**
-     * Removes the property with the given key.
-     *
-     * @param key the key of the property to remove
-     */
-    public void clearProperty(String key)
-    {
-        super.clearProperty(key);
-        delegate.possiblySave();
-    }
-
-    /**
-     * Removes all properties in the specified sub tree.
-     *
-     * @param key the key of the properties to be removed
-     */
-    public void clearTree(String key)
-    {
-        super.clearTree(key);
-        delegate.possiblySave();
-    }
-
-    /**
      * Removes all properties from this configuration. If this configuration
      * was loaded from a file, the associated DOM document is also cleared.
      */
@@ -299,18 +260,6 @@
     }
 
     /**
-     * Sets the value of the specified property.
-     *
-     * @param key the key of the property
-     * @param value the new value
-     */
-    public void setProperty(String key, Object value)
-    {
-        super.setProperty(key, value);
-        delegate.possiblySave();
-    }
-
-    /**
      * Initializes this configuration from an XML document.
      *
      * @param document the document to be parsed
@@ -509,41 +458,18 @@
     }
 
     /**
-     * Loads this configuration.
+     * Loads the configuration from the given input stream.
+     *
+     * @param in the input stream
      * @throws ConfigurationException if an error occurs
      */
-    public void load() throws ConfigurationException
-    {
-        delegate.load();
-    }
-
-    public void load(String fileName) throws ConfigurationException
-    {
-        delegate.load(fileName);
-    }
-
-    public void load(File file) throws ConfigurationException
-    {
-        delegate.load(file);
-    }
-
-    public void load(URL url) throws ConfigurationException
-    {
-        delegate.load(url);
-    }
-
     public void load(InputStream in) throws ConfigurationException
     {
         load(new InputSource(in));
     }
 
-    public void load(InputStream in, String encoding) throws ConfigurationException
-    {
-        delegate.load(in, encoding);
-    }
-
     /**
-     * Load the properties from the given reader.
+     * Load the configuration from the given reader.
      * Note that the <code>clear()</code> method is not called, so
      * the properties contained in the loaded file will be added to the
      * actual set of properties.
@@ -566,7 +492,7 @@
     {
         try
         {
-            URL sourceURL = delegate.getURL();
+            URL sourceURL = getDelegate().getURL();
             if (sourceURL != null)
             {
                 source.setSystemId(sourceURL.toString());
@@ -585,36 +511,6 @@
         }
     }
 
-    public void save() throws ConfigurationException
-    {
-        delegate.save();
-    }
-
-    public void save(String fileName) throws ConfigurationException
-    {
-        delegate.save(fileName);
-    }
-
-    public void save(File file) throws ConfigurationException
-    {
-        delegate.save(file);
-    }
-
-    public void save(URL url) throws ConfigurationException
-    {
-        delegate.save(url);
-    }
-
-    public void save(OutputStream out) throws ConfigurationException
-    {
-        delegate.save(out);
-    }
-
-    public void save(OutputStream out, String encoding) throws ConfigurationException
-    {
-        delegate.save(out, encoding);
-    }
-
     /**
      * Saves the configuration to the specified writer.
      *
@@ -656,7 +552,7 @@
 
         // clear document related properties
         copy.document = null;
-        copy.delegate = copy.new FileConfigurationDelegate();
+        copy.setDelegate(createDelegate());
         // clear all references in the nodes, too
         copy.getRoot().visit(new NodeVisitor()
         {
@@ -669,103 +565,15 @@
         return copy;
     }
 
-    public String getFileName()
-    {
-        return delegate.getFileName();
-    }
-
-    public void setFileName(String fileName)
-    {
-        delegate.setFileName(fileName);
-    }
-
-    public String getBasePath()
-    {
-        return delegate.getBasePath();
-    }
-
-    public void setBasePath(String basePath)
-    {
-        delegate.setBasePath(basePath);
-    }
-
-    public File getFile()
-    {
-        return delegate.getFile();
-    }
-
-    public void setFile(File file)
-    {
-        delegate.setFile(file);
-    }
-
-    public URL getURL()
-    {
-        return delegate.getURL();
-    }
-
-    public void setURL(URL url)
-    {
-        delegate.setURL(url);
-    }
-
-    public void setAutoSave(boolean autoSave)
-    {
-        delegate.setAutoSave(autoSave);
-    }
-
-    public boolean isAutoSave()
-    {
-        return delegate.isAutoSave();
-    }
-
-    public ReloadingStrategy getReloadingStrategy()
-    {
-        return delegate.getReloadingStrategy();
-    }
-
-    public void setReloadingStrategy(ReloadingStrategy strategy)
-    {
-        delegate.setReloadingStrategy(strategy);
-    }
-
-    public void reload()
-    {
-        delegate.reload();
-    }
-
-    public String getEncoding()
-    {
-        return delegate.getEncoding();
-    }
-
-    public void setEncoding(String encoding)
-    {
-        delegate.setEncoding(encoding);
-    }
-
-    public boolean containsKey(String key)
-    {
-        reload();
-        return super.containsKey(key);
-    }
-
-    public Iterator getKeys(String prefix)
-    {
-        reload();
-        return super.getKeys(prefix);
-    }
-
-    public Object getProperty(String key)
-    {
-        reload();
-        return super.getProperty(key);
-    }
-
-    public boolean isEmpty()
+    /**
+     * Creates the file configuration delegate for this object. This implementation
+     * will return an instance of a class derived from <code>FileConfigurationDelegate</code>
+     * that deals with some specialities of <code>XMLConfiguration</code>.
+     * @return the delegate for this object
+     */
+    protected FileConfigurationDelegate createDelegate()
     {
-        reload();
-        return super.isEmpty();
+        return new XMLFileConfigurationDelegate();
     }
 
     /**
@@ -967,6 +775,7 @@
          * @param parent the parent node
          * @param sibling1 the first sibling
          * @param sibling2 the second sibling
+         * @return the new node
          */
         protected Object insert(Node newNode, Node parent, Node sibling1, Node sibling2)
         {
@@ -1076,26 +885,11 @@
      * used internally to implement the <code>FileConfiguration</code> methods
      * for <code>XMLConfiguration</code>, too.
      */
-    private class FileConfigurationDelegate extends AbstractFileConfiguration
+    private class XMLFileConfigurationDelegate extends FileConfigurationDelegate
     {
         public void load(InputStream in) throws ConfigurationException
         {
             XMLConfiguration.this.load(in);
-        }
-
-        public void load(Reader in) throws ConfigurationException
-        {
-            XMLConfiguration.this.load(in);
-        }
-
-        public void save(Writer out) throws ConfigurationException
-        {
-            XMLConfiguration.this.save(out);
-        }
-
-        public void clear()
-        {
-            XMLConfiguration.this.clear();
         }
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=331008&r1=331007&r2=331008&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Nov  5 08:35:57 2005
@@ -23,6 +23,11 @@
   <body>
 
     <release version="1.2-dev" date="in SVN">
+      <action dev="ebourg, oheger" type="update">
+        AbstractHierarchicalFileConfiguration, a new base class for file based
+        hierarchical configurations, was introduced. XMLConfiguration now
+        extends this class.
+      </action>
       <action dev="oheger" type="update" due-to="Kay Doebl" issue="37293">
         XMLConfiguration now prints the used encoding in the xml declaration of
         generated files. In earlier versions always the default encoding was



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