org.rhq.core.util
Class IniEditor.Section

java.lang.Object
  extended by org.rhq.core.util.IniEditor.Section
Enclosing class:
IniEditor

public static class IniEditor.Section
extends Object

Loads, edits and saves a section of an INI-style configuration file. This class does actually belong to the internals of IniEditor and should rarely ever be used directly. It's exposed because it can be useful for plain, section-less configuration files (Java-style properties, for example).


Field Summary
static String DEFAULT_OPTION_FORMAT
           
static char HEADER_END
           
static char HEADER_START
           
 
Constructor Summary
IniEditor.Section(String name)
          Constructs a new section.
IniEditor.Section(String name, boolean isCaseSensitive)
          Constructs a new section, specifying case-sensitivity.
IniEditor.Section(String name, char[] delims)
          Constructs a new section, defining comment delimiters.
IniEditor.Section(String name, char[] delims, boolean isCaseSensitive)
          Constructs a new section, defining comment delimiters.
 
Method Summary
 void addBlankLine()
          Adds a blank line to the end of this section.
 void addComment(String comment)
          Adds a comment line to the end of this section.
 void addComment(String comment, char delim)
          Adds a comment line to the end of this section.
 String get(String option)
          Returns an option's value.
 boolean hasOption(String name)
          Checks whether a given option exists in this section.
 void load(BufferedReader reader)
          Loads options from a reader into this instance.
 List<String> optionNames()
          Returns the names of all options in this section.
 boolean remove(String option)
          Removes an option if it exists.
 void save(PrintWriter writer)
          Prints this section to a print writer.
 void set(String option, String value)
          Sets an option's value and creates the option if it doesn't exist.
 void set(String option, String value, char delim)
          Sets an option's value and creates the option if it doesn't exist.
 void setOptionFormat(org.rhq.core.util.IniEditor.OptionFormat format)
          Sets the option format for this section.
 void setOptionFormatString(String formatString)
          Sets the option format for this section to the given string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_OPTION_FORMAT

public static final String DEFAULT_OPTION_FORMAT
See Also:
Constant Field Values

HEADER_START

public static final char HEADER_START
See Also:
Constant Field Values

HEADER_END

public static final char HEADER_END
See Also:
Constant Field Values
Constructor Detail

IniEditor.Section

public IniEditor.Section(String name)
Constructs a new section.

Parameters:
name - the section's name
Throws:
IllegalArgumentException - the section's name is illegal

IniEditor.Section

public IniEditor.Section(String name,
                         boolean isCaseSensitive)
Constructs a new section, specifying case-sensitivity.

Parameters:
name - the section's name
isCaseSensitive - option names are case-sensitive if this is true
Throws:
IllegalArgumentException - the section's name is illegal

IniEditor.Section

public IniEditor.Section(String name,
                         char[] delims)
Constructs a new section, defining comment delimiters.

Parameters:
name - the section's name
delims - an array of characters to be recognized as starters of comment lines; the first of them will be used for newly created comments
Throws:
IllegalArgumentException - the section's name is illegal

IniEditor.Section

public IniEditor.Section(String name,
                         char[] delims,
                         boolean isCaseSensitive)
Constructs a new section, defining comment delimiters.

Parameters:
name - the section's name
delims - an array of characters to be recognized as starters of comment lines; the first of them will be used for newly created comments
isCaseSensitive - option names are case-sensitive if this is true
Throws:
IllegalArgumentException - the section's name is illegal
Method Detail

setOptionFormatString

public void setOptionFormatString(String formatString)
Sets the option format for this section to the given string. Options in this section will be rendered according to the given format string. The string must contain %s three times, these will be replaced with the option name, the option separator and the option value in this order. Literal percentage signs must be escaped by preceding them with another percentage sign (i.e., %% corresponds to one percentage sign). The default format string is "%s %s %s". Option formats may look like format strings as supported by Java 1.5, but the string is in fact parsed in a custom fashion to guarantee backwards compatibility. So don't try clever stuff like using format conversion types other than %s.

Parameters:
formatString - a format string, containing %s exactly three times
Throws:
IllegalArgumentException - if the format string is illegal

setOptionFormat

public void setOptionFormat(org.rhq.core.util.IniEditor.OptionFormat format)
Sets the option format for this section. Options will be rendered according to the given format when printed.

Parameters:
format - a compiled option format

optionNames

public List<String> optionNames()
Returns the names of all options in this section.

Returns:
list of names of this section's options in original/insertion order

hasOption

public boolean hasOption(String name)
Checks whether a given option exists in this section.

Parameters:
name - the name of the option to test for
Returns:
true if the option exists in this section

get

public String get(String option)
Returns an option's value.

Parameters:
option - the name of the option
Returns:
the requested option's value or null if no option with the specified name exists

set

public void set(String option,
                String value)
Sets an option's value and creates the option if it doesn't exist.

Parameters:
option - the option's name
value - the option's value
Throws:
IllegalArgumentException - the option name is illegal, ie contains a '=' character or consists only of white space

set

public void set(String option,
                String value,
                char delim)
Sets an option's value and creates the option if it doesn't exist.

Parameters:
option - the option's name
value - the option's value
delim - the delimiter between name and value for this option
Throws:
IllegalArgumentException - the option name is illegal, ie contains a '=' character or consists only of white space

remove

public boolean remove(String option)
Removes an option if it exists.

Parameters:
option - the name of the option
Returns:
true if the option was actually removed

addComment

public void addComment(String comment)
Adds a comment line to the end of this section. A comment spanning several lines (ie with line breaks) will be split up, one comment line for each line.

Parameters:
comment - the comment

addComment

public void addComment(String comment,
                       char delim)
Adds a comment line to the end of this section. A comment spanning several lines (ie with line breaks) will be split up, one comment line for each line.

Parameters:
comment - the comment
delim - the delimiter used to mark the start of this comment

addBlankLine

public void addBlankLine()
Adds a blank line to the end of this section.


load

public void load(BufferedReader reader)
          throws IOException
Loads options from a reader into this instance. Will read from the stream until it hits a section header, ie a '[' character, and resets the reader to point to this character.

Parameters:
reader - where to read from
Throws:
IOException - at an I/O problem

save

public void save(PrintWriter writer)
          throws IOException
Prints this section to a print writer.

Parameters:
writer - where to write
Throws:
IOException - at an I/O problem


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.