|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.LookupINI
public class LookupINI
Class to read/write Windows style INI files.
Java has a standard Properties file
format which is used to list key/value pairs in a ASCII file. The
INI files commonly used by Windows for configuration are similar in
nature, but have the concept of "sections". This class is designed
to work with (and extend slightly) the usage of INI configuration
files. The following features are provided:
read standard INI files.
write standard INI files.
LookupKeyed object.
The best way to see how this works, is to look at the commented example.ini file and the sample
TestINI.java test class.
The INI file format recognized by this class uses the following rules:
comments are ignored. In addition all lines which
are not either "key=value" lines or "[section]" lines are also
ignored.
To look up values from this object, you should set your keys to be preceded with the "[section]" of the area you want to find the value for. For example consider the following INI file:
# # Comment lines (blank lines OK too) # width=640 [Dog] width=800 [Cat]
To get the cat's width, you would use getString("[Cat]width") and
you would get the value "640" (from the default section). If you used
getString("[Dog]width") you would get the value "800" returned.
TagLookup| Constructor Summary | |
|---|---|
LookupINI()
Initializes object to a empty state. |
|
LookupINI(InputStream is)
Initializes object and load from INI file. |
|
| Method Summary | |
|---|---|
void |
add(InputStream is)
Parse the contents of a InputStream
This method parses a ASCII stream and applies
the INI rules mentioned in the class
documentation to load settings (ignoring comments). |
void |
add(String section,
String key,
Object data)
Add/update a key/value pair in a particular section. |
Object |
clone()
Makes a fairly light weight clone of the object. |
Object |
get(Object key)
Fetch the value associated with a "key" Try to lookup the Object which is associated with the key. |
Enumeration |
getKeys()
Get enumeration of all of the keys in the lookup table. |
LookupOrdered |
getSection(String sectName)
Get the key/value pairs for a particular section. |
Enumeration |
getSectionKeys()
Get a list of the defined sections. |
String |
getString(Object key)
Fetch a String "value" associated with a "key". |
String |
getString(String section,
String key,
boolean allowDef)
A "finer control" lookup of a particular key under a particular section. |
boolean |
hasSection(String sectName)
Determine if a particular section is currently defined in the INI file. |
void |
orderSections(Enumeration ia,
boolean addNew,
boolean keepUnlisted)
Method to re-arrange (order) the sections as they would appear when written to a file. |
void |
print(PrintWriter pw)
Print the contents like a Windows INI file. |
static String |
removeBrackets(String s)
Removes (but only if necessary) square brackets from a String. |
void |
removeSection(String sectName)
Removes a entire section from the table based on key value. |
static String |
requireBrackets(String s)
Adds (but only if necessary) square brackets to a String. |
void |
setCommentChars(String chars)
Specify what set of characters should indicate EOL comments. |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public LookupINI()
LookupINI object in this manner you
will want to use a add method to fill
it with data.
LookupINI(InputStream)public LookupINI(InputStream is)
InputStream.
is - Source to read ASCII INI data from.LookupINI(java.io.InputStream)| Method Detail |
|---|
public void setCommentChars(String chars)
add(InputStream)
method ignores all lines which start with "#;/\\ \t\r\n". If this
is unacceptable for your source files, you can use this method to
change what characters appearing at the start of a line indicate
a comment.
chars - String containing the characters which will indicate that a
ASCII line read by add(InputStream) should be
ignored. Pass null if you want the default values.
public void add(String section,
String key,
Object data)
read
INI data from a file. However, this method allows you to
add/update sections and key/value pairs within sections. Here are
the things you can do:
section - Set this to the name of the section you want to
add/update. This should be in the form of "[NAME]" to identify
the section. Alternatively, you can set this parameter to null
to indicate that the key/value pair is to be used as a default
value for ALL sections. If both this this and the key are
null, then nothing is done.key - Must be non-null and have a length greater than zero if you
want to actually add a key/value pair to the section. Set this
to null if you just want to add a section, but don't want to
also add a key/value pair.data - The data to be associated with the key. You can set this to
null if you like.add(InputStream)public void add(InputStream is)
InputStream
This method parses a ASCII stream and applies
the INI rules mentioned in the class
documentation to load settings (ignoring comments).
is - Source to read ASCII INI data from.getKeys()
public String getString(String section,
String key,
boolean allowDef)
This method provides the most flexible mechanism to lookup a
string value under a one of the INI "sections". It is also more
efficient than the methods used to implement the Lookup interface.
It should be noted that the "section" specified should be in
its "bracket" form (pass values like: "[Login]", not "Login". You
can use the requireBrackets(java.lang.String) method to help with this.
section - This should be the name of the section to look the value up
with, it can be null if you want to just check the default
section.key - The "key" to look the value up for under the section
specified. Must not be null.allowDef - Set this parameter to true if you want us to check the default
section if the item isn't found under the specific section you
specified. Set to false to prohibit this extra check.
public Object get(Object key)
get in interface Lookupget in interface LookupKeyedkey - Key to use to look up object with (if you pass null, you will
get null back).
LookupCreatepublic String getString(Object key)
getString in interface LookupgetString in interface LookupKeyedkey - Key to use to look up object with (if you pass null, you will
get null back).
Lookup table all support the Object.toString() method
without Exception.LookupCreatepublic Enumeration getKeys()
getKeys in interface LookupKeyedEnumeration list of all keys in the Lookup table.public static String requireBrackets(String s)
This method forces a string to have the form "[TEXT]". If the original string passed is missing either the start bracket "[" and/or the ending bracket "]", they will be added.
s - String to remove the brackets (if any) from - must not be
null.
String to add the brackets to (if missing) - must not be
null.
removeBrackets(java.lang.String)public static String removeBrackets(String s)
This method takes a string of the form "[TEXT]", "TEXT]", "[TEXT" or "TEXT" and returns "TEXT". It can be useful to remove the brackets off the ends of a source string.
s - String to remove the brackets (if any) from - must not be
null.
requireBrackets(java.lang.String)public Enumeration getSectionKeys()
Enumeration of the names of the defined sections. You can cast
each element to a string if desired, or use it as a parameter to
getSection(java.lang.String).
getKeys()public void print(PrintWriter pw)
lookup
table in a INI form maintaining the original ordering of the
various [sections] and the components (key/value pairs) within
each section.
Comments are not preserved by this object, so if the original INI file had comments in it and you use this method to print over it, the comments will be lost.
pw - Where to print the lookup table in ASCII form.add(java.lang.String, java.lang.String, java.lang.Object)public LookupOrdered getSection(String sectName)
Since you will get a reference to the LookupOrdered object which this object also refers to, it is
important to note that any modifications you make to sections
lookup table will affect this object as well.
sectName - Name of the section in the INI file you want the properties
for (like: "[maps]"). You can pass null if you want to get the
"default" properties (you might get null back if this section
wasn't created).
ordered lookup table of
the properties specifically listed in the section. Any
properties inherited from the default section (if any) will
not be included. In addition, you may get null back if the
section you specify doesn't exist.print(java.io.PrintWriter)public boolean hasSection(String sectName)
This method allows one to test to see if a particular section
(like: "[Cat]", "[Internet]", etc) is defined in the LookupINI object. Be sure to enclose your section name
with brackets (use the requireBrackets
method to help with this).
Note: it is OK to pass null or "" to this method, it will detect this and return false.
sectName - The name of the section (with brackets - like "[Dog]"). If you
pass null or zero length string we return false.
getSection(java.lang.String),
requireBrackets(java.lang.String)public void removeSection(String sectName)
sectName - Name of the section to be removed. If you pass null or the
name of a section which does not exist, nothing is done.get(Object)
public void orderSections(Enumeration ia,
boolean addNew,
boolean keepUnlisted)
This method allows one to re-order the way sections will appear in the output when the INI object is written to a text file.
You have the option of specifying whether or not sections which are not explicitly listed in the new order should be retained or tossed.
It is important to realize that the LookupINI object itself does not care about the ordering (in other words, look ups will always work).
It is also important to realize that the default section (the one that is not preceded by a section name within square brackets) will ALWAYS be at the start of the output file (its the only way possible).
ia - A enumeration (iterator) which we can used to get the names of
sections from. You must not pass null.addNew - Set this to true if you want any new sections to be added to
your INI object (if the enumeration contains sections which
don't currently exist).keepUnlisted - Set this to true if you want us to keep the sections which are
not explicitly listed by your iterator (they will be added at
the end). Set this to false if sections not explicitly listed
should be removed.public Object clone()
This method constructs a lightweight clone of the object. The relationship between the "cloned" object and the original object will be as follows:
clone in class ObjectLookupOrdered object.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||