You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mp...@apache.org on 2002/08/28 21:43:14 UTC

cvs commit: jakarta-commons-sandbox/configuration project.xml

mpoeschl    2002/08/28 12:43:14

  Modified:    configuration/src/java/org/apache/commons/configuration
                        PropertiesConfiguration.java
               configuration project.xml
  Added:       configuration/src/test/org/apache/commons/configuration
                        TestPropertiesConfiguration.java test.properties
  Log:
  add the ability to write a properties file and a testcase for loading and saving
  
  Revision  Changes    Path
  1.4       +67 -2     jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java
  
  Index: PropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PropertiesConfiguration.java	28 Aug 2002 18:16:02 -0000	1.3
  +++ PropertiesConfiguration.java	28 Aug 2002 19:43:13 -0000	1.4
  @@ -55,13 +55,16 @@
    */
   
   import java.io.File;
  -import java.io.FileInputStream;
  +import java.io.*;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.IOException;
   import java.io.LineNumberReader;
   import java.io.Reader;
   import java.io.UnsupportedEncodingException;
  +import java.util.Iterator;
  +import org.apache.commons.lang.StringUtils;
  +import java.util.Date;
   
   /**
    * loads the configuration from a properties file. <p>
  @@ -347,6 +350,30 @@
       }
   
       /**
  +     * save properties to a file.
  +     * properties with multiple values are saved comma seperated.
  +     *
  +     * @param filename name of the properties file
  +     * @throws IOException
  +     */
  +    public void save(String filename) throws IOException
  +    {
  +        File file = new File(filename);
  +        PropertiesWriter out = new PropertiesWriter(file);
  +
  +        out.writeComment("written by PropertiesConfiguration");
  +        out.writeComment(new Date().toString());
  +        for (Iterator i = this.getKeys(); i.hasNext();)
  +        {
  +            String key = (String) i.next();
  +            String value = StringUtils.join(this.getStringArray(key), ", ");
  +            out.writeProperty(key, value);
  +        }
  +        out.flush();
  +        out.close();
  +    }
  +
  +    /**
        * Gets the property value for including other properties files.
        * By default it is "include".
        *
  @@ -426,5 +453,43 @@
           }
       } // class PropertiesReader
   
  +    /**
  +     * This class is used to write properties lines.
  +     */
  +    class PropertiesWriter extends FileWriter
  +    {
  +        /**
  +         * Constructor.
  +         *
  +         * @param file the proerties file
  +         * @throws IOException
  +         */
  +        public PropertiesWriter(File file) throws IOException
  +        {
  +            super(file);
  +        }
  +
  +        /**
  +         * Write a property.
  +         *
  +         * @param key
  +         * @param value
  +         * @exception IOException
  +         */
  +        public void writeProperty(String key, String value) throws IOException
  +        {
  +            write(key + " = " + value + "\n");
  +        }
   
  +        /**
  +         * Write a comment.
  +         *
  +         * @param comment
  +         * @exception IOException
  +         */
  +        public void writeComment(String comment) throws IOException
  +        {
  +            write("# " + comment + "\n");
  +        }
  +    } // class PropertiesWriter
   }
  
  
  
  1.1                  jakarta-commons-sandbox/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
  
  Index: TestPropertiesConfiguration.java
  ===================================================================
  package org.apache.commons.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import junit.framework.*;
  import java.io.*;
  import java.util.Vector;
  
  
  /**
   * test foir loading and saving properties files
   *
   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
   * @version $Id: TestPropertiesConfiguration.java,v 1.1 2002/08/28 19:43:13 mpoeschl Exp $
   */
  public class TestPropertiesConfiguration extends TestCase
  {
  
      public TestPropertiesConfiguration(String s)
      {
          super(s);
      }
  
      public void testLoad()
      {
          PropertiesConfiguration conf = new PropertiesConfiguration();
          InputStream input = this.getClass().getResourceAsStream(
                  "/org/apache/commons/configuration/test.properties");
          try
          {
              conf.load(input);
              String loaded = conf.getString("configuration.loaded");
              assertEquals("true", loaded);
          }
          catch (Exception e)
          {
              System.err.println("Exception thrown:  " + e);
          }
      }
  
      public void testSave()
      {
          PropertiesConfiguration conf = new PropertiesConfiguration();
          conf.addProperty("string", "value1");
          Vector vec = new Vector();
          for (int i = 1; i < 5; i++)
          {
              vec.add("value" + i);
          }
          conf.addProperty("array", vec);
          String filename = "STRING0";
          try
          {
              conf.save(filename);
          }
          catch (Exception e)
          {
              System.err.println("Exception thrown:  " + e);
          }
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/configuration/src/test/org/apache/commons/configuration/test.properties
  
  Index: test.properties
  ===================================================================
  configuration.loaded = true
  
  
  
  
  1.9       +4 -0      jakarta-commons-sandbox/configuration/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/project.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- project.xml	28 Aug 2002 18:16:02 -0000	1.8
  +++ project.xml	28 Aug 2002 19:43:14 -0000	1.9
  @@ -82,6 +82,10 @@
         <id>commons-collections</id>
         <version>2.0</version>
       </dependency>
  +    <dependency>
  +      <id>commons-lang</id>
  +      <version>1.0-b1</version>
  +    </dependency>
   <!--
       <dependency>
         <id>commons-logging</id>
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>