|
|
/*
** Java currency library package.
** Copyright (c) 1999 by Bruno Antunes
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package org.gjt.currency;
import java.util.*;
import java.io.*;
/**
* Utility to read properties list
*
* @author Bruno Antunes
* @version 1.0 7/05/1999
*/
public class PropertiesHelper {
/**
* Reads a property list (key and element pairs) using this base class with a
* properties list file name.
*
* @param propName the name of the properties file to read
* @return the Properties
* @see Class#getResourceAsStream
* @throws IOException if an error occurred when reading from the
* properties list.
*/
public static final Properties loadProperties(String propName)
throws IOException {
return loadProperties(PropertiesHelper.class, propName);
}
/**
* Reads a property list (key and element pairs) from a base class with a
* properties list file name.
*
* @param clasz the Class to use do load the properties list using the
* getResourceAsStream method
* @param propName the name of the properties file to read
* @return the Properties
* @see Class#getResourceAsStream
* @throws IOException if an error occurred when reading from the
* properties list.
*/
public static final Properties loadProperties(Class clasz, String propName)
throws IOException {
if ((clasz == null) || (propName == null)) {
throw new IllegalArgumentException("Invalid arguments");
}
Properties p = new Properties();
InputStream is = null;
try {
is = clasz.getResourceAsStream(propName);
if (is == null) {
throw new IOException("Properties not found: " + propName);
} else {
p.load(is);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
}
return p;
}
/**
* Reads a property list (key and element pairs) from the input stream
*
* @param a string with the format [<key>=<value>]
* [,<key>=<value>]*
* @return the Properties
* @throws IOException if an error occurred when reading from the
* input stream.
* @throws IllegalArgumentException if an invalid imput stream is passed
*/
public static final Properties loadProperties(InputStream is)
throws IOException {
Properties p = new Properties();
try {
if (is == null) {
throw new IllegalArgumentException("Invalid InputStream: null");
} else {
p.load(is);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
}
return p;
}
/**
* Reads a property list (key and element pairs) from a string
* representation
*
* @param a string with the format [<key>=<value>]
* [,<key>=<value>]*
* @return the Properties
* @throws IllegalArgumentException if an invalid string token is passed
*/
public static final Properties loadTokenizedProperties(String tokens) {
if (tokens == null) {
throw new IllegalArgumentException("Invalid propertie definition:" +
" null");
}
StringTokenizer token = new StringTokenizer(tokens,",");
Properties p = new Properties();
StringTokenizer entry;
while (token.hasMoreTokens()) {
entry = new StringTokenizer(token.nextToken(),"=");
if (entry.countTokens() != 2) {
throw new IllegalArgumentException("Invalid tokenized " +
"propertie entry");
}
p.put(entry.nextToken(),entry.nextToken());
}
return p;
}
}
|