Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 25781 invoked from network); 21 Jun 2004 17:46:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 21 Jun 2004 17:46:11 -0000 Received: (qmail 94445 invoked by uid 500); 21 Jun 2004 17:45:57 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 94280 invoked by uid 500); 21 Jun 2004 17:45:55 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 94014 invoked by uid 500); 21 Jun 2004 17:45:53 -0000 Received: (qmail 93413 invoked by uid 99); 21 Jun 2004 17:45:47 -0000 Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Mon, 21 Jun 2004 10:45:47 -0700 Received: (qmail 25430 invoked by uid 1865); 21 Jun 2004 17:45:30 -0000 Date: 21 Jun 2004 17:45:30 -0000 Message-ID: <20040621174530.25429.qmail@minotaur.apache.org> From: ebourg@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/configuration/xdocs changes.xml X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N ebourg 2004/06/21 10:45:29 Modified: configuration/src/java/org/apache/commons/configuration BasePropertiesConfiguration.java PropertiesConfiguration.java configuration/xdocs changes.xml Log: Added a save() method to PropertiesConfiguration and save(Writer out), save(OutputStream out), save(OutputStream out, String encoding) to BasePropertiesConfiguration. Revision Changes Path 1.12 +86 -44 jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java Index: BasePropertiesConfiguration.java =================================================================== RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- BasePropertiesConfiguration.java 16 Jun 2004 18:13:53 -0000 1.11 +++ BasePropertiesConfiguration.java 21 Jun 2004 17:45:29 -0000 1.12 @@ -16,14 +16,17 @@ package org.apache.commons.configuration; -import java.io.File; import java.io.FileWriter; +import java.io.FilterWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -139,8 +142,7 @@ * * @throws ConfigurationException */ - public void load(InputStream input) - throws ConfigurationException + public void load(InputStream input) throws ConfigurationException { load(input, null); } @@ -165,7 +167,7 @@ } catch (UnsupportedEncodingException e) { - throw new ConfigurationException("Should look up and use default encoding.",e); + throw new ConfigurationException("Should look up and use default encoding.", e); } } @@ -174,7 +176,8 @@ reader = new PropertiesReader(new InputStreamReader(input)); } - try { + try + { while (true) { String line = reader.readProperty(); @@ -214,58 +217,98 @@ } } } - catch (IOException ioe){ + catch (IOException ioe) + { throw new ConfigurationException("Could not load configuration from input stream.",ioe); } } /** - * save properties to a file. - * properties with multiple values are saved comma seperated. + * Save the configuration to a file. Properties with multiple values are + * saved on multiple lines, one value per line. * - * @param filename name of the properties file + * @param filename the name of the properties file * * @throws ConfigurationException */ public void save(String filename) throws ConfigurationException { - PropertiesWriter out = null; - File file = new File(filename); - try { - 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(); - Object value = getProperty(key); + FileWriter writer = null; - if (value instanceof List) - { - out.writeProperty(key, (List) value); - } - else - { - out.writeProperty(key, value); - } - } - out.flush(); - out.close(); + try + { + writer = new FileWriter(filename); + save(writer); } - catch (IOException ioe) + catch (IOException e) { - try { - if (out != null){ - out.close(); + throw new ConfigurationException("Could not save to file " + filename, e); + } + finally + { + // close the writer + try + { + if (writer != null) + { + writer.close(); } } - catch (IOException ioe2){ + catch (IOException ioe2) { } + } + } + + /** + * Save the configuration to the specified stream. + * + * @param out the output stream used to save the configuration + */ + public void save(OutputStream out) throws IOException + { + save(out, null); + } + /** + * Save the configuration to the specified stream. + * + * @param out the output stream used to save the configuration + * @param encoding the charset used to write the configuration + */ + public void save(OutputStream out, String encoding) throws IOException + { + OutputStreamWriter writer = new OutputStreamWriter(out, encoding); + save(writer); + } + + /** + * Save the configuration to the specified stream. + * + * @param writer the output stream used to save the configuration + */ + public void save(Writer writer) throws IOException + { + PropertiesWriter out = new PropertiesWriter(writer); + + out.writeComment("written by PropertiesConfiguration"); + out.writeComment(new Date().toString()); + + Iterator keys = getKeys(); + while (keys.hasNext()) + { + String key = (String) keys.next(); + Object value = getProperty(key); + + if (value instanceof List) + { + out.writeProperty(key, (List) value); + } + else + { + out.writeProperty(key, value); } - throw new ConfigurationException("Could not save to file " + filename,ioe); } + + out.flush(); } /** @@ -378,17 +421,16 @@ /** * This class is used to write properties lines. */ - class PropertiesWriter extends FileWriter + class PropertiesWriter extends FilterWriter { /** * Constructor. * - * @param file the proerties file - * @throws IOException + * @param writer a Writer object providing the underlying stream */ - public PropertiesWriter(File file) throws IOException + public PropertiesWriter(Writer writer) throws IOException { - super(file); + super(writer); } /** 1.9 +31 -22 jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java Index: PropertiesConfiguration.java =================================================================== RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- PropertiesConfiguration.java 2 Jun 2004 16:42:24 -0000 1.8 +++ PropertiesConfiguration.java 21 Jun 2004 17:45:29 -0000 1.9 @@ -1,5 +1,3 @@ -package org.apache.commons.configuration; - /* * Copyright 2001-2004 The Apache Software Foundation. * @@ -16,6 +14,8 @@ * limitations under the License. */ +package org.apache.commons.configuration; + import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -39,12 +39,8 @@ * * @version $Id$ */ -public class PropertiesConfiguration - extends BasePropertiesConfiguration - implements Configuration +public class PropertiesConfiguration extends BasePropertiesConfiguration { - - /** File separator. */ protected String fileSeparator = System.getProperty("file.separator"); @@ -65,7 +61,6 @@ setIncludesAllowed(false); } - /** * Creates and loads the extended properties from the specified file. * The specified file can contain "include = " properties which then @@ -76,7 +71,6 @@ */ public PropertiesConfiguration(String fileName) throws ConfigurationException { - load(fileName); } @@ -98,24 +92,39 @@ */ public void load(String fileName) throws ConfigurationException { - InputStream is=null; - try { - is = getPropertyStream(fileName); - load(is); + InputStream in = null; + try + { + in = getPropertyStream(fileName); + load(in); } - catch (IOException ioe){ - throw new ConfigurationException("Could not load from file " + fileName,ioe); + catch (IOException e) + { + throw new ConfigurationException("Could not load from file " + fileName, e); } - finally{ - if(is !=null){ - try{ - is.close(); + finally + { + // close the input stream + if (in != null) + { + try + { + in.close(); } - catch (IOException ioe2){ - ioe2.printStackTrace(); + catch (IOException e) + { + e.printStackTrace(); } } } + } + + /** + * Save the configuration to the file specified by the fileName attribute. + */ + public void save() throws ConfigurationException + { + save(fileName); } /** 1.23 +5 -0 jakarta-commons/configuration/xdocs/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- changes.xml 16 Jun 2004 15:17:09 -0000 1.22 +++ changes.xml 21 Jun 2004 17:45:29 -0000 1.23 @@ -7,6 +7,11 @@ + + Added a save() method to PropertiesConfiguration and save(Writer out), + save(OutputStream out), save(OutputStream out, String encoding) to + BasePropertiesConfiguration. + List values are now properly stored as comma separated values in the Properties object returned by ConfigurationConverter.getProperties() --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org