|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.sunwesttek.util.config.XMLConfig
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:
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
public short foo;
public short getFoo() {...}
public void setFoo(short foo) {...}
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.
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 |
public XMLConfig()
public XMLConfig(java.lang.String description)
description
- Description for the configMethod Detail |
public static XMLConfig fromXML(java.lang.String xml, boolean validating) throws InvalidConfigSyntaxException
xml
- XML stringvalidating
- To validate input or not
InvalidConfigSyntaxException
java.io.IOException
public java.lang.String toXML()
public static XMLConfig load(java.io.File file, boolean validating) throws java.io.IOException, InvalidConfigSyntaxException
file
- Config filevalidating
- To validate input or not
java.io.IOException
InvalidConfigSyntaxException
public static XMLConfig load(java.io.InputStream istream, boolean validating) throws java.io.IOException, InvalidConfigSyntaxException
istream
- Input streamvalidating
- To validate input or not
java.io.IOException
InvalidConfigSyntaxException
public static XMLConfig load(java.io.Reader reader, boolean validating) throws java.io.IOException, InvalidConfigSyntaxException
reader
- Readervalidating
- To validate input or not
java.io.IOException
InvalidConfigSyntaxException
public void save(java.io.File file) throws java.io.IOException
file
- Output file
java.io.IOException
public void save(java.io.OutputStream ostream) throws java.io.IOException
ostream
- Output stream
java.io.IOException
public void save(java.io.Writer writer) throws java.io.IOException
writer
- Output writer
java.io.IOException
public int getIndent()
public void setIndent(int indent)
indent
- Indent valuepublic java.lang.String getEncoding()
public void setEncoding(java.lang.String encoding)
encoding
- Encoding valuepublic int size()
public void addSection(java.lang.String sectName, ConfigSection cs) throws DuplicateConfigSectionException, InvalidConfigParamException
sectName
- Section namecs
- Config section
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 requirementspublic void updateSection(java.lang.String sectName, ConfigSection cs) throws NoSuchConfigSectionException, InvalidConfigParamException
sectName
- Section namecs
- Config section
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 requirementspublic ConfigSection getSection(java.lang.String sectName)
sectName
- Section name
public ConfigSection getSection(int index)
index
- Index of the section to return
java.lang.ArrayIndexOutOfBoundsException
- index is out of range (index
< 0 || index >= size()).public void removeSection(java.lang.String sectName) throws NoSuchConfigSectionException
sectName
- Section name
NoSuchConfigSectionException
- when the sepcified
section can not be found in configpublic java.lang.String[] getAllSectionNames()
public ConfigSection[] getAllSections()
public void removeAllSections()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |