// 2012-11-30 johnpfeiffer
// TODO: ascertain whether when duplicates exist it always gets the last value from the source?
// TODO: case insensitive method?
// TODO: static factory constructor build() that allows for caseInsensitive option
package kittyandbear.net;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class PropertiesReader
{
public static final String CLASSVERSION = "0.30";
static final String NEWLINE = System.getProperty("line.separator");
private Properties properties;
PropertiesReader( String pathFile ) throws IOException, IllegalArgumentException
{
if( pathFile == null )
{
throw new IllegalArgumentException( "ERROR: pathFile cannot be null" );
}
if( pathFile.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: pathFile cannot be empty" );
}
try
{
// FileReader can support UTF8 better and BufferedReader should support larger files better
BufferedReader input = new BufferedReader( new FileReader( pathFile ) );
properties = new Properties();
properties.load( input );
input.close();
}catch( FileNotFoundException e )
{
throw new IllegalArgumentException( "ERROR: file not found: " + e.getMessage() );
}
}
protected Properties get()
{
return properties;
}
public String getPropertyValue( String key ) throws IllegalArgumentException
{
if( key == null )
{
throw new IllegalArgumentException( "ERROR: key cannot be null" );
}
if( key.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: key cannot be empty" );
}
return properties.getProperty( key );
}
public ArrayList <String> getKeysAndValues()
{
ArrayList <String> unsortedList = new ArrayList <String>();
Enumeration <?> props = properties.propertyNames();
while( props.hasMoreElements() )
{
String key = (String) props.nextElement();
String value = properties.getProperty( key );
unsortedList.add( key + "=" + value );
}
return unsortedList;
}
public ArrayList <String> getKeysSorted()
{
ArrayList <String> sortedList = new ArrayList <String>();
Set <Object> set = properties.keySet();
Iterator <Object> it = set.iterator();
while( it.hasNext() )
{
sortedList.add( it.next().toString() );
}
Collections.sort( sortedList ); // case sensitive so A before a
return sortedList;
}
public ArrayList <String> getKeysAndValuesSortedByKeys()
{
ArrayList <String> sortedKeyAndValueList = new ArrayList <String>();
ArrayList <String> sortedKeyList = getKeysSorted(); // case sensitive so A before a
for( String key : sortedKeyList )
{
sortedKeyAndValueList.add( key + "=" + properties.getProperty( key ) );
}
return sortedKeyAndValueList;
}
public String getKeysAndValuesSortedByKeysString() // convenience method
{
StringBuilder strb = new StringBuilder();
ArrayList <String> source = getKeysAndValuesSortedByKeys(); // case sensitive so A before a
for( String line : source )
{
strb.append( line + NEWLINE );
}
return strb.toString();
}
} // end class