[Android] in ConfigFile.java do not add [<sectionless!>]

This commit is contained in:
zilmar 2016-10-02 07:48:27 +11:00
parent 628a0d769d
commit e76cc73671
1 changed files with 89 additions and 94 deletions

View File

@ -28,18 +28,15 @@ import android.util.Log;
public class ConfigFile public class ConfigFile
{ {
/** The name we use for the untitled section (preamble) of the config file. */
public static final String SECTIONLESS_NAME = "[<sectionless!>]";
/** Name of the config file. */ /** Name of the config file. */
private final String mFilename; private final String mFilename;
/** Sections mapped by title for easy lookup, with insertion order retained. */ /** Sections mapped by title for easy lookup, with insertion order retained. */
private final LinkedHashMap<String, ConfigSection> mConfigMap; private final LinkedHashMap<String, ConfigSection> mConfigMap;
/** /**
* Reads the entire config file, and saves the data to internal collections for manipulation. * Reads the entire config file, and saves the data to internal collections for manipulation.
* *
* @param filename The config file to read from. * @param filename The config file to read from.
*/ */
public ConfigFile( String filename ) public ConfigFile( String filename )
@ -48,48 +45,48 @@ public class ConfigFile
mConfigMap = new LinkedHashMap<String, ConfigSection>(); mConfigMap = new LinkedHashMap<String, ConfigSection>();
reload(); reload();
} }
/** /**
* Looks up a config section by its title. * Looks up a config section by its title.
* *
* @param sectionTitle Title of the section containing the parameter. * @param sectionTitle Title of the section containing the parameter.
* *
* @return A ConfigSection containing parameters, or null if not found. * @return A ConfigSection containing parameters, or null if not found.
*/ */
public ConfigSection get( String sectionTitle ) public ConfigSection get( String sectionTitle )
{ {
return mConfigMap.get( sectionTitle ); return mConfigMap.get( sectionTitle );
} }
/** /**
* Looks up the specified parameter under the specified section title. * Looks up the specified parameter under the specified section title.
* *
* @param sectionTitle Title of the section containing the parameter. * @param sectionTitle Title of the section containing the parameter.
* @param parameter Name of the parameter. * @param parameter Name of the parameter.
* *
* @return The value of the specified parameter, or null if not found. * @return The value of the specified parameter, or null if not found.
*/ */
public String get( String sectionTitle, String parameter ) public String get( String sectionTitle, String parameter )
{ {
ConfigSection section = mConfigMap.get( sectionTitle ); ConfigSection section = mConfigMap.get( sectionTitle );
// The specified section doesn't exist or is empty.. quit // The specified section doesn't exist or is empty.. quit
if( section == null || section.parameters == null ) if( section == null || section.parameters == null )
return null; return null;
ConfigParameter confParam = section.parameters.get( parameter ); ConfigParameter confParam = section.parameters.get( parameter );
// The specified parameter doesn't exist.. quit // The specified parameter doesn't exist.. quit
if( confParam == null ) if( confParam == null )
return null; return null;
// Got it // Got it
return confParam.value; return confParam.value;
} }
/** /**
* Assigns the specified value to the specified parameter under the specified section. * Assigns the specified value to the specified parameter under the specified section.
* *
* @param sectionTitle The title of the section to contain the parameter. * @param sectionTitle The title of the section to contain the parameter.
* @param parameter The name of the parameter. * @param parameter The name of the parameter.
* @param value The value to give the parameter. * @param value The value to give the parameter.
@ -105,7 +102,7 @@ public class ConfigFile
} }
section.put( parameter, value ); section.put( parameter, value );
} }
/** /**
* Erases any previously loaded data. * Erases any previously loaded data.
*/ */
@ -113,11 +110,11 @@ public class ConfigFile
{ {
mConfigMap.clear(); mConfigMap.clear();
} }
/** /**
* Re-loads the entire config file, overwriting any unsaved changes, and saves the data in * Re-loads the entire config file, overwriting any unsaved changes, and saves the data in
* 'configMap'. * 'configMap'.
* *
* @return True if successful. * @return True if successful.
* @see #save() * @see #save()
*/ */
@ -126,10 +123,10 @@ public class ConfigFile
// Make sure a file was actually specified // Make sure a file was actually specified
if( TextUtils.isEmpty( mFilename ) ) if( TextUtils.isEmpty( mFilename ) )
return false; return false;
// Free any previously loaded data // Free any previously loaded data
clear(); clear();
FileInputStream fstream; FileInputStream fstream;
try try
{ {
@ -140,26 +137,22 @@ public class ConfigFile
// File not found... we can't continue // File not found... we can't continue
return false; return false;
} }
DataInputStream in = new DataInputStream( fstream ); DataInputStream in = new DataInputStream( fstream );
BufferedReader br = new BufferedReader( new InputStreamReader( in ) ); BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
String sectionName = SECTIONLESS_NAME; ConfigSection section = new ConfigSection( "", br ); // section
ConfigSection section = new ConfigSection( sectionName, br ); // Read the 'sectionless'
// section
mConfigMap.put( sectionName, section ); // Save the data to 'configMap'
// Loop through reading the remaining sections // Loop through reading the remaining sections
while( !TextUtils.isEmpty( section.nextName ) ) while( !TextUtils.isEmpty( section.nextName ) )
{ {
// Get the next section name // Get the next section name
sectionName = section.nextName; String sectionName = section.nextName;
// Load the next section // Load the next section
section = new ConfigSection( sectionName, br ); section = new ConfigSection( sectionName, br );
mConfigMap.put( sectionName, section ); // Save the data to 'configMap' mConfigMap.put( sectionName, section ); // Save the data to 'configMap'
} }
try try
{ {
// Finished. Close the file. // Finished. Close the file.
@ -170,14 +163,14 @@ public class ConfigFile
{ {
// (Don't care) // (Don't care)
} }
// Success // Success
return true; return true;
} }
/** /**
* Saves the data from 'configMap' back to the config file. * Saves the data from 'configMap' back to the config file.
* *
* @return True if successful. False otherwise. * @return True if successful. False otherwise.
* @see #reload() * @see #reload()
*/ */
@ -189,16 +182,16 @@ public class ConfigFile
Log.e( "ConfigFile", "Filename not specified in method save()" ); Log.e( "ConfigFile", "Filename not specified in method save()" );
return false; // Quit return false; // Quit
} }
// Ensure parent directories exist before writing file // Ensure parent directories exist before writing file
new File( mFilename ).getParentFile().mkdirs(); new File( mFilename ).getParentFile().mkdirs();
// Write data to file // Write data to file
FileWriter fw = null; FileWriter fw = null;
try try
{ {
fw = new FileWriter( mFilename ); fw = new FileWriter( mFilename );
// Loop through the sections // Loop through the sections
for( ConfigSection section : mConfigMap.values() ) for( ConfigSection section : mConfigMap.values() )
{ {
@ -225,21 +218,21 @@ public class ConfigFile
} }
} }
} }
// Success // Success
return true; return true;
} }
/** /**
* Returns a handle to the configMap keyset. * Returns a handle to the configMap keyset.
* *
* @return keyset containing all the config section titles. * @return keyset containing all the config section titles.
*/ */
public Set<String> keySet() public Set<String> keySet()
{ {
return mConfigMap.keySet(); return mConfigMap.keySet();
} }
/** /**
* The ConfigSection class reads all the parameters in the next section of the config file. * The ConfigSection class reads all the parameters in the next section of the config file.
* Saves the name of the next section (or null if end of file or error). Can also be used to add * Saves the name of the next section (or null if end of file or error). Can also be used to add
@ -251,29 +244,30 @@ public class ConfigFile
private HashMap<String, ConfigParameter> parameters; // Parameters sorted by name for easy private HashMap<String, ConfigParameter> parameters; // Parameters sorted by name for easy
// lookup // lookup
private LinkedList<ConfigLine> lines; // All the lines in this section, including comments private LinkedList<ConfigLine> lines; // All the lines in this section, including comments
// Name of the next section, or null if there are no sections left to read in the file: // Name of the next section, or null if there are no sections left to read in the file:
private String nextName = null; private String nextName = null;
/** /**
* Constructor: Creates an empty config section * Constructor: Creates an empty config section
* *
* @param sectionName The section title. * @param sectionName The section title.
*/ */
public ConfigSection( String sectionName ) public ConfigSection( String sectionName )
{ {
parameters = new HashMap<String, ConfigParameter>(); parameters = new HashMap<String, ConfigParameter>();
lines = new LinkedList<ConfigLine>(); lines = new LinkedList<ConfigLine>();
if( !TextUtils.isEmpty( sectionName ) && !sectionName.equals( SECTIONLESS_NAME ) ) if( !TextUtils.isEmpty( sectionName ))
{
lines.add( new ConfigLine( ConfigLine.LINE_SECTION, "[" + sectionName + "]\n", null ) ); lines.add( new ConfigLine( ConfigLine.LINE_SECTION, "[" + sectionName + "]\n", null ) );
}
name = sectionName; name = sectionName;
} }
/** /**
* Constructor: Reads the next section of the config file, and saves it in 'parameters'. * Constructor: Reads the next section of the config file, and saves it in 'parameters'.
* *
* @param sectionName The section title. * @param sectionName The section title.
* @param br The config file to read from. * @param br The config file to read from.
*/ */
@ -282,19 +276,20 @@ public class ConfigFile
String fullLine, strLine, p, v; String fullLine, strLine, p, v;
ConfigParameter confParam; ConfigParameter confParam;
int x, y; int x, y;
parameters = new HashMap<String, ConfigParameter>(); parameters = new HashMap<String, ConfigParameter>();
lines = new LinkedList<ConfigLine>(); lines = new LinkedList<ConfigLine>();
if( !TextUtils.isEmpty( sectionName ) && !sectionName.equals( SECTIONLESS_NAME ) ) if( !TextUtils.isEmpty( sectionName ))
{
lines.add( new ConfigLine( ConfigLine.LINE_SECTION, "[" + sectionName + "]\n", null ) ); lines.add( new ConfigLine( ConfigLine.LINE_SECTION, "[" + sectionName + "]\n", null ) );
}
name = sectionName; name = sectionName;
// No file to read from. Quit. // No file to read from. Quit.
if( br == null ) if( br == null )
return; return;
try try
{ {
while( ( fullLine = br.readLine() ) != null ) while( ( fullLine = br.readLine() ) != null )
@ -305,7 +300,7 @@ public class ConfigFile
|| ( strLine.substring( 0, 1 ).equals( ";" ) ) || ( strLine.substring( 0, 1 ).equals( ";" ) )
|| ( ( strLine.length() > 1 ) && ( strLine.substring( 0, 2 ) || ( ( strLine.length() > 1 ) && ( strLine.substring( 0, 2 )
.equals( "//" ) ) ) ) .equals( "//" ) ) ) )
{ // A comment or blank line. { // A comment or blank line.
lines.add( new ConfigLine( ConfigLine.LINE_GARBAGE, fullLine + "\n", null ) ); lines.add( new ConfigLine( ConfigLine.LINE_GARBAGE, fullLine + "\n", null ) );
} }
@ -313,20 +308,20 @@ public class ConfigFile
{ {
// This should be a "parameter=value" pair: // This should be a "parameter=value" pair:
x = strLine.indexOf( '=' ); x = strLine.indexOf( '=' );
if( x < 1 ) if( x < 1 )
return; // This shouldn't happen (bad syntax). Quit. return; // This shouldn't happen (bad syntax). Quit.
if( x < ( strLine.length() - 1 ) ) if( x < ( strLine.length() - 1 ) )
{ {
p = strLine.substring( 0, x ).trim(); p = strLine.substring( 0, x ).trim();
if( p.length() < 1 ) if( p.length() < 1 )
return; // This shouldn't happen (bad syntax). Quit. return; // This shouldn't happen (bad syntax). Quit.
v = strLine.substring( x + 1, strLine.length() ).trim(); v = strLine.substring( x + 1, strLine.length() ).trim();
// v = v.replace( "\"", "" ); // I'm doing this later, so I can save // v = v.replace( "\"", "" ); // I'm doing this later, so I can save
// back without losing them // back without losing them
if( v.length() > 0 ) if( v.length() > 0 )
{ {
// Save the parameter=value pair // Save the parameter=value pair
@ -350,18 +345,18 @@ public class ConfigFile
// This should be the beginning of the next section // This should be the beginning of the next section
if( ( strLine.length() < 3 ) || ( !strLine.contains( "]" ) ) ) if( ( strLine.length() < 3 ) || ( !strLine.contains( "]" ) ) )
return; // This shouldn't happen (bad syntax). Quit. return; // This shouldn't happen (bad syntax). Quit.
x = strLine.indexOf( '[' ); x = strLine.indexOf( '[' );
y = strLine.indexOf( ']' ); y = strLine.indexOf( ']' );
if( ( y <= x + 1 ) || ( x == -1 ) || ( y == -1 ) ) if( ( y <= x + 1 ) || ( x == -1 ) || ( y == -1 ) )
return; // This shouldn't happen (bad syntax). Quit. return; // This shouldn't happen (bad syntax). Quit.
p = strLine.substring( x + 1, y ).trim(); p = strLine.substring( x + 1, y ).trim();
// Save the name of the next section. // Save the name of the next section.
nextName = p; nextName = p;
// Done reading parameters. Return. // Done reading parameters. Return.
return; return;
} }
@ -376,26 +371,26 @@ public class ConfigFile
{ {
// (Don't care) // (Don't care)
} }
// Reached end of file or error.. either way, just quit // Reached end of file or error.. either way, just quit
return; return;
} }
/** /**
* Returns a handle to the parameter keyset. * Returns a handle to the parameter keyset.
* *
* @return keyset containing all the parameters. * @return keyset containing all the parameters.
*/ */
public Set<String> keySet() public Set<String> keySet()
{ {
return parameters.keySet(); return parameters.keySet();
} }
/** /**
* Returns the value of the specified parameter. * Returns the value of the specified parameter.
* *
* @param parameter Name of the parameter. * @param parameter Name of the parameter.
* *
* @return Parameter's value, or null if not found. * @return Parameter's value, or null if not found.
*/ */
public String get( String parameter ) public String get( String parameter )
@ -403,21 +398,21 @@ public class ConfigFile
// Error: no parameters, or parameter was null // Error: no parameters, or parameter was null
if( parameters == null || TextUtils.isEmpty( parameter ) ) if( parameters == null || TextUtils.isEmpty( parameter ) )
return null; return null;
ConfigParameter confParam = parameters.get( parameter ); ConfigParameter confParam = parameters.get( parameter );
// Parameter not found // Parameter not found
if( confParam == null ) if( confParam == null )
return null; return null;
// Got it // Got it
return confParam.value; return confParam.value;
} }
/** /**
* Adds the specified parameter to this config section, updates the value if it already * Adds the specified parameter to this config section, updates the value if it already
* exists, or removes the parameter. * exists, or removes the parameter.
* *
* @param parameter The name of the parameter. * @param parameter The name of the parameter.
* @param value The parameter's value, or null to remove. * @param value The parameter's value, or null to remove.
*/ */
@ -440,12 +435,12 @@ public class ConfigFile
confParam.value = value; confParam.value = value;
} }
} }
/** /**
* Writes the entire section to file. * Writes the entire section to file.
* *
* @param fw File to write to. * @param fw File to write to.
* *
* @throws IOException if a writing error occurs. * @throws IOException if a writing error occurs.
*/ */
public void save( FileWriter fw ) throws IOException public void save( FileWriter fw ) throws IOException
@ -457,7 +452,7 @@ public class ConfigFile
} }
} }
} }
/** /**
* The ConfigLine class stores each line of the config file (including comments). * The ConfigLine class stores each line of the config file (including comments).
*/ */
@ -466,14 +461,14 @@ public class ConfigFile
public static final int LINE_GARBAGE = 0; // Comment, whitespace, or blank line public static final int LINE_GARBAGE = 0; // Comment, whitespace, or blank line
public static final int LINE_SECTION = 1; // Section title public static final int LINE_SECTION = 1; // Section title
public static final int LINE_PARAM = 2; // Parameter=value pair public static final int LINE_PARAM = 2; // Parameter=value pair
public int lineType = 0; // LINE_GARBAGE, LINE_SECTION, or LINE_PARAM. public int lineType = 0; // LINE_GARBAGE, LINE_SECTION, or LINE_PARAM.
public String strLine = ""; // Actual line from the config file. public String strLine = ""; // Actual line from the config file.
public ConfigParameter confParam = null; // Null unless this line has a parameter. public ConfigParameter confParam = null; // Null unless this line has a parameter.
/** /**
* Constructor: Saves the relevant information about the line. * Constructor: Saves the relevant information about the line.
* *
* @param type The type of line. * @param type The type of line.
* @param line The line itself. * @param line The line itself.
* @param param Config parameters pertaining to the line. * @param param Config parameters pertaining to the line.
@ -484,12 +479,12 @@ public class ConfigFile
strLine = line; strLine = line;
confParam = param; confParam = param;
} }
/** /**
* Saves the ConfigLine. * Saves the ConfigLine.
* *
* @param fw The file to save the ConfigLine to. * @param fw The file to save the ConfigLine to.
* *
* @throws IOException If a writing error occurs. * @throws IOException If a writing error occurs.
*/ */
public void save( FileWriter fw ) throws IOException public void save( FileWriter fw ) throws IOException
@ -499,12 +494,12 @@ public class ConfigFile
{ {
if( !strLine.contains( "=" ) || confParam == null ) if( !strLine.contains( "=" ) || confParam == null )
return; // This shouldn't happen return; // This shouldn't happen
x = strLine.indexOf( '=' ); x = strLine.indexOf( '=' );
if( x < 1 ) if( x < 1 )
return; // This shouldn't happen either return; // This shouldn't happen either
if( x < strLine.length() ) if( x < strLine.length() )
fw.write( strLine.substring( 0, x + 1 ) + confParam.value + "\n" ); fw.write( strLine.substring( 0, x + 1 ) + confParam.value + "\n" );
} }
@ -514,7 +509,7 @@ public class ConfigFile
} }
} }
} }
/** /**
* The ConfigParameter class associates a parameter with its value. * The ConfigParameter class associates a parameter with its value.
*/ */
@ -523,10 +518,10 @@ public class ConfigFile
@SuppressWarnings( "unused" ) @SuppressWarnings( "unused" )
public String parameter; public String parameter;
public String value; public String value;
/** /**
* Constructor: Associate the parameter and value * Constructor: Associate the parameter and value
* *
* @param parameter The name of the parameter. * @param parameter The name of the parameter.
* @param value The value of the parameter. * @param value The value of the parameter.
*/ */