com.sunwesttek.util.config
Class XMLConfig

java.lang.Object
  |
  +--com.sunwesttek.util.config.XMLConfig

public class XMLConfig
extends java.lang.Object

XMLConfig is an XML-based generic configuration management API for Java. It can automatically transform XML based configuration files to and from Java objects.

A configuration file is divided into sections, while each section can have name, description and configuration fields. A configuration field can be of most Java primitive data types. The Java data types currently supported are:

Most of Java container types are supported as well. The data elements in those containers have to be of the primitive Java data types listed above.

To use XMLConfig, an application developer only needs to extend ConfigSection class to define each section of the configuration file. A configuraion field of the configuration section can be defined as either a public data member or a pair of get and set functions. For example, a field named "foo" of type short can be defined as

or

XMLConfig can also be used as a lightweight XML serialization protocol for Java objects. A Java oject can be serialized into an XML string, which can then be stored in database or send over network. On the other hand, when such an XML string is received, the corresponding Java object can be retrieved.

See Also:
ConfigSection

Constructor Summary
XMLConfig()
          Construct a config object without description.
XMLConfig(java.lang.String description)
          Construct a config object with description.
 
Method Summary
 void addSection(java.lang.String sectName, ConfigSection cs)
          Add a new section into config.
static XMLConfig fromXML(java.lang.String xml, boolean validating)
          Create a config object from the specified XML string.
 java.lang.String[] getAllSectionNames()
          Get all section names of the config.
 ConfigSection[] getAllSections()
          Get all sections of the config.
 java.lang.String getEncoding()
          Get encoding.
 int getIndent()
          Get indent.
 ConfigSection getSection(int index)
          Get section in config with specified index.
 ConfigSection getSection(java.lang.String sectName)
          Get section in config with specified name.
static XMLConfig load(java.io.File file, boolean validating)
          Create a config object from the specified config file.
static XMLConfig load(java.io.InputStream istream, boolean validating)
          Create a config object from the specified input stream.
static XMLConfig load(java.io.Reader reader, boolean validating)
          Create a config object from the specified reader.
 void removeAllSections()
          Remove all sections from config.
 void removeSection(java.lang.String sectName)
          Remove section from config with specified name.
 void save(java.io.File file)
          Save config to a file.
 void save(java.io.OutputStream ostream)
          Save config to an output stream.
 void save(java.io.Writer writer)
          Save config to a writer.
 void setEncoding(java.lang.String encoding)
          Set encoding.
 void setIndent(int indent)
          Set indent.
 int size()
          Returns the number of sections in the config.
 java.lang.String toXML()
          Generate XML from a config object.
 void updateSection(java.lang.String sectName, ConfigSection cs)
          Update section with specified name in config.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLConfig

public XMLConfig()
Construct a config object without description.


XMLConfig

public XMLConfig(java.lang.String description)
Construct a config object with description.

Parameters:
description - Description for the config
Method Detail

fromXML

public static XMLConfig fromXML(java.lang.String xml,
                                boolean validating)
                         throws InvalidConfigSyntaxException
Create a config object from the specified XML string.

Parameters:
xml - XML string
validating - To validate input or not
Returns:
XMLConfig object
Throws:
InvalidConfigSyntaxException
java.io.IOException

toXML

public java.lang.String toXML()
Generate XML from a config object.

Returns:
XML representation of the config object

load

public static XMLConfig load(java.io.File file,
                             boolean validating)
                      throws java.io.IOException,
                             InvalidConfigSyntaxException
Create a config object from the specified config file.

Parameters:
file - Config file
validating - To validate input or not
Returns:
XMLConfig object
Throws:
java.io.IOException
InvalidConfigSyntaxException

load

public static XMLConfig load(java.io.InputStream istream,
                             boolean validating)
                      throws java.io.IOException,
                             InvalidConfigSyntaxException
Create a config object from the specified input stream.

Parameters:
istream - Input stream
validating - To validate input or not
Returns:
XMLConfig object
Throws:
java.io.IOException
InvalidConfigSyntaxException

load

public static XMLConfig load(java.io.Reader reader,
                             boolean validating)
                      throws java.io.IOException,
                             InvalidConfigSyntaxException
Create a config object from the specified reader.

Parameters:
reader - Reader
validating - To validate input or not
Returns:
XMLConfig object
Throws:
java.io.IOException
InvalidConfigSyntaxException

save

public void save(java.io.File file)
          throws java.io.IOException
Save config to a file.

Parameters:
file - Output file
Throws:
java.io.IOException

save

public void save(java.io.OutputStream ostream)
          throws java.io.IOException
Save config to an output stream.

Parameters:
ostream - Output stream
Throws:
java.io.IOException

save

public void save(java.io.Writer writer)
          throws java.io.IOException
Save config to a writer.

Parameters:
writer - Output writer
Throws:
java.io.IOException

getIndent

public int getIndent()
Get indent.

Returns:
Indent value

setIndent

public void setIndent(int indent)
Set indent.

Parameters:
indent - Indent value

getEncoding

public java.lang.String getEncoding()
Get encoding.

Returns:
Encoding value

setEncoding

public void setEncoding(java.lang.String encoding)
Set encoding.

Parameters:
encoding - Encoding value

size

public int size()
Returns the number of sections in the config.

Returns:
the number of sections in the config

addSection

public void addSection(java.lang.String sectName,
                       ConfigSection cs)
                throws DuplicateConfigSectionException,
                       InvalidConfigParamException
Add a new section into config.

Parameters:
sectName - Section name
cs - Config section
Throws:
DuplicateConfigSectionException - when a section with the same name has added to the config already
InvalidConfigParamException - if the input config section object does not comply with the ConfigSection requirements

updateSection

public void updateSection(java.lang.String sectName,
                          ConfigSection cs)
                   throws NoSuchConfigSectionException,
                          InvalidConfigParamException
Update section with specified name in config.

Parameters:
sectName - Section name
cs - Config section
Throws:
NoSuchConfigSectionException - when the specified section can not be found in the config
InvalidConfigParamException - if the input config section object does not comply with the ConfigSection requirements

getSection

public ConfigSection getSection(java.lang.String sectName)
Get section in config with specified name.

Parameters:
sectName - Section name
Returns:
Secion with specified name or null if no such section is found

getSection

public ConfigSection getSection(int index)
Get section in config with specified index.

Parameters:
index - Index of the section to return
Returns:
Secion at the specified index
Throws:
java.lang.ArrayIndexOutOfBoundsException - index is out of range (index < 0 || index >= size()).

removeSection

public void removeSection(java.lang.String sectName)
                   throws NoSuchConfigSectionException
Remove section from config with specified name.

Parameters:
sectName - Section name
Throws:
NoSuchConfigSectionException - when the sepcified section can not be found in config

getAllSectionNames

public java.lang.String[] getAllSectionNames()
Get all section names of the config.

Returns:
All section names of the config

getAllSections

public ConfigSection[] getAllSections()
Get all sections of the config.

Returns:
All sections of the config

removeAllSections

public void removeAllSections()
Remove all sections from config.



Copyright (c) 2002 Sunwest Technologies. http://www.SunwestTek.com/