Enhydra 3.1 API

com.lutris.util
Class ConfigFile

java.lang.Object
  |
  +--com.lutris.util.ConfigFile

public class ConfigFile
extends java.lang.Object

ConfigFile is used to manipulate Multiserver configuration (.conf) files. Presents all configuration elements in the form of a keyed table. Configuration elements are grouped by string "keys" according to the function they configure. The syntax is described more formally below.

 	stream ::= entry | stream entry
 	entry ::= key "=" value_list | comment | blank
 	value_list ::= value | value_list "," value
 	value ::= fragment | value "+" fragment
 	fragment ::= key | quoted_string
 	quoted_string ::= (C/C++ style quoted string)
 	key ::= (A string matching [A-Za-z_\./][A-Za-z0-9_-\./]*)
 	comment ::= "#" (any text up to a newline)
 	blank ::= (A line containing only white space)
 
In addition to the above syntax, some additional semantic rules apply. The operator "+" concatenates the fragment immediately to the left and to the right of it. Thus ab + cd results in abcd. The operator "," terminates one element and begins the next element. Thus, the line val = hello, world creates a configuration entry with the key "val" and the two elements "hello", and "world". If the characters "+", ",", or "\" occur at the end of a line, then the entry is continued on the next line. Trailing and leading whitespaces are ignored, and multiple whitespace characters are converted by default into a single space. The "+" operator leaves no whitespace between operands. Finally, within quoted strings, C-style backslash escapes are recognized. These include "\n" for newline, "\t" for tab, etc. An example configuration input file is given below.
	#==============================================================
	# Sample.config
	#==============================================================
	LDAP_SERVERS[] =  "server1.ldap.gov:338",
			  "server2.ldap.gov:1000"
	USER_TIMEOUT =  3600  # seconds.  Comments can follow on same line.
	STRANGE_ENTRY = "This is a long long long long long " +
			"long long line.  Notice how the \"+\" " +
			"operator is used to break this line up " +
			"into more than one line in the config file." ,
			"And this is the second element."
	# etc.
 

Version:
$Revision: 1.21.2.1.2.1 $
See Also:
Config

Field Summary
static java.lang.String TRAILING_COMMENT
          The key under which the trailing comment is stored.
 
Constructor Summary
ConfigFile()
          Default constructor for an empty config file.
ConfigFile(java.io.File configFile)
          Constructor from a File.
ConfigFile(java.io.InputStream inputStream)
          Constructor from an InputStream.
ConfigFile(KeywordValueTable kvt)
          Constructor from a KeywordValueTable.
 
Method Summary
 void addEntry(java.lang.String key, java.lang.String[] values, java.lang.String comment)
          Add an entry to the config file.
 void addEntry(java.lang.String key, java.lang.String value, java.lang.String comment)
          Add an entry to the config file.
 java.lang.String getComment(java.lang.String key)
          Returns the comment associated with a given key, or null if there is no comment.
 Config getConfig()
          Returns the Config object representing the config data in the file.
 java.io.File getFile()
          Get the associated file.
 void removeEntry(java.lang.String key)
          Remove an entry from the config file.
 void write()
          Writes this config to the associated file.
 void write(java.io.OutputStream outputStream)
          Writes out a config file to the OutputStream specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRAILING_COMMENT

public static final java.lang.String TRAILING_COMMENT
The key under which the trailing comment is stored.
Constructor Detail

ConfigFile

public ConfigFile()
Default constructor for an empty config file.

ConfigFile

public ConfigFile(java.io.InputStream inputStream)
           throws ConfigException
Constructor from an InputStream.
Parameters:
inputStream - The input stream from which to parse the config file.

ConfigFile

public ConfigFile(java.io.File configFile)
           throws ConfigException,
                  java.io.IOException
Constructor from a File. Allows to later write back the configuration to the same file.
Parameters:
configFile - The name of a local file to parse.

ConfigFile

public ConfigFile(KeywordValueTable kvt)
           throws ConfigException
Constructor from a KeywordValueTable.
Parameters:
kvt - A KeywordValueTable from which to populate the config file.
Method Detail

getConfig

public Config getConfig()
Returns the Config object representing the config data in the file.
Returns:
The Config object for this ConfigFile.

getComment

public java.lang.String getComment(java.lang.String key)
Returns the comment associated with a given key, or null if there is no comment. Pass in ConfigFile.TRAILING_COMMENT to get the trailing comment.
Parameters:
key - the key to look up.
Returns:
the associated comment or null

addEntry

public void addEntry(java.lang.String key,
                     java.lang.String[] values,
                     java.lang.String comment)
              throws KeywordValueException
Add an entry to the config file.
Parameters:
key - The config element name.
values - A string array of values.
comment - A string containing a properly commented config file comment (i.e. beginning with "#")

addEntry

public void addEntry(java.lang.String key,
                     java.lang.String value,
                     java.lang.String comment)
              throws KeywordValueException
Add an entry to the config file.
Parameters:
key - The config element name.
value - A String value.
comment - A string containing a properly commented config file comment (i.e. beginning with "#")

removeEntry

public void removeEntry(java.lang.String key)
                 throws KeywordValueException
Remove an entry from the config file.
Parameters:
key - The config element name.

getFile

public java.io.File getFile()
Get the associated file. If no file is associated with this config, null is returned.

write

public void write()
           throws java.io.IOException,
                  java.io.FileNotFoundException
Writes this config to the associated file. If no file is associated with this config, throws a FileNotFoundException

write

public void write(java.io.OutputStream outputStream)
Writes out a config file to the OutputStream specified. Note that Objects other than String or String[] will be converted into a String.
Parameters:
outputStream - The output stream on which to write the config file.

Enhydra 3.1 API