ch.javasoft.xml.config
Class XmlConfig

java.lang.Object
  extended by ch.javasoft.xml.config.XmlConfig

public class XmlConfig
extends Object

The XmlConfig class is a generic xml configuration reader, supporting features like referred configuration blocks, accessing system property values and program invocation parameters (args of the main method).


Nested Class Summary
static class XmlConfig.ResolveKey
          Key elements be used in {...} brackets, will be resolved by the corresponding content.
static class XmlConfig.XmlAttribute
          Predefined and recognized xml attributes.
static class XmlConfig.XmlElement
          Predefined and recognized xml elements
 
Field Summary
protected  String[] mArgs
           
protected  Document mDocument
           
protected  Date mTimestamp
           
static String NO_APP_NAME
           
 
Constructor Summary
protected XmlConfig(String appName, Document xmlDoc)
           
protected XmlConfig(String appName, Document xmlDoc, String[] args)
           
 
Method Summary
static XmlConfig fromXmlDocument(Document doc)
          Parses the given document and returns an XmlConfig instance.
static XmlConfig fromXmlDocument(File file)
          Parses the given document and returns an XmlConfig instance.
static XmlConfig fromXmlDocument(Reader reader)
          Parses the given document and returns an XmlConfig instance.
 String getAppName()
           
protected  String getArg(int index)
           
protected  int getArgCount()
           
 Element getConfig(String name)
           
 Element getDefaultConfig()
           
 Properties getLoggingProperties()
          Returns the logging properties read from the logging element, or null if no logging properties found in the xml config.
 Properties getLoggingProperties(String configName)
          Returns the logging properties read from the logging element, or null if no logging properties found in the xml config.
protected  List<Element> getReferredElementContent(String name, String path)
           
protected  String getResolvedAttributeValue(Attribute att, String path, String resolveKey)
           
protected  Element getRootElement()
           
static XmlConfig getXmlConfig(File xmlFile)
           
static XmlConfig getXmlConfig(File xmlFile, String[] args)
           
static XmlConfig getXmlConfig(InputStream xmlFile)
           
static XmlConfig getXmlConfig(InputStream xmlFile, String[] args)
           
static XmlConfig getXmlConfig(Reader xmlFile)
           
static XmlConfig getXmlConfig(String appName, File xmlFile)
           
static XmlConfig getXmlConfig(String appName, File xmlFile, String[] args)
           
static XmlConfig getXmlConfig(String appName, InputStream xmlFile)
           
static XmlConfig getXmlConfig(String appName, InputStream xmlFile, String[] args)
           
static XmlConfig getXmlConfig(String appName, Reader xmlFile)
           
 void initLogManagerConfiguration()
          Initializes the log manager configuration with logging configuration from the the default config.
 void initLogManagerConfiguration(String configName)
          Initializes the log manager configuration with logging configuration from the the specified config.
 void printUsage(PrintStream stream, String usageName)
           
protected  List<Element> resolve(Element element, String path)
           
protected  void resolveAttributeValue(Attribute att, String path)
           
 void setAppName(String appName)
           
 void setDefaultConfig(String name)
           
 Document toXmlDocument()
          Returns a copy of the underlying xml document, including main arguments if any have been specified.
 void writeTo(OutputStream out)
          Writes this xml config to the given output stream
 void writeTo(Writer writer)
          Writes this xml config to the given writer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_APP_NAME

public static final String NO_APP_NAME
See Also:
Constant Field Values

mDocument

protected final Document mDocument

mArgs

protected final String[] mArgs

mTimestamp

protected final Date mTimestamp
Constructor Detail

XmlConfig

protected XmlConfig(String appName,
                    Document xmlDoc)

XmlConfig

protected XmlConfig(String appName,
                    Document xmlDoc,
                    String[] args)
Method Detail

getXmlConfig

public static XmlConfig getXmlConfig(File xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(Reader xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(InputStream xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(String appName,
                                     File xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(String appName,
                                     Reader xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(String appName,
                                     InputStream xmlFile)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(File xmlFile,
                                     String[] args)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(InputStream xmlFile,
                                     String[] args)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(String appName,
                                     File xmlFile,
                                     String[] args)
                              throws DocumentException
Throws:
DocumentException

getXmlConfig

public static XmlConfig getXmlConfig(String appName,
                                     InputStream xmlFile,
                                     String[] args)
                              throws DocumentException
Throws:
DocumentException

writeTo

public void writeTo(OutputStream out)
Writes this xml config to the given output stream


writeTo

public void writeTo(Writer writer)
Writes this xml config to the given writer


toXmlDocument

public Document toXmlDocument()
Returns a copy of the underlying xml document, including main arguments if any have been specified. From the returned document,


fromXmlDocument

public static XmlConfig fromXmlDocument(File file)
                                 throws DocumentException
Parses the given document and returns an XmlConfig instance. Note that this document is expected to contain an application element (direct child of root element) with a name attribute and possible with arg child nodes. Such a document is usually received by calling toXmlDocument().

Throws:
DocumentException - if a dom4j xml exception occurs

fromXmlDocument

public static XmlConfig fromXmlDocument(Reader reader)
                                 throws DocumentException
Parses the given document and returns an XmlConfig instance. Note that this document is expected to contain an application element (direct child of root element) with a name attribute and possible with arg child nodes. Such a document is usually received by calling toXmlDocument().

Throws:
DocumentException - if a dom4j xml exception occurs

fromXmlDocument

public static XmlConfig fromXmlDocument(Document doc)
Parses the given document and returns an XmlConfig instance. Note that this document is expected to contain an application element (direct child of root element) with a name attribute and possible with arg child nodes. Such a document is usually received by calling toXmlDocument().


getArgCount

protected int getArgCount()

getArg

protected String getArg(int index)

getRootElement

protected Element getRootElement()

getAppName

public String getAppName()

setAppName

public void setAppName(String appName)

setDefaultConfig

public void setDefaultConfig(String name)

getDefaultConfig

public Element getDefaultConfig()
                         throws XmlConfigException
Throws:
XmlConfigException

getConfig

public Element getConfig(String name)
                  throws XmlConfigException
Throws:
XmlConfigException

resolve

protected List<Element> resolve(Element element,
                                String path)
                         throws XmlConfigException
Throws:
XmlConfigException

resolveAttributeValue

protected void resolveAttributeValue(Attribute att,
                                     String path)
                              throws XmlConfigException
Throws:
XmlConfigException

getResolvedAttributeValue

protected String getResolvedAttributeValue(Attribute att,
                                           String path,
                                           String resolveKey)
                                    throws XmlConfigException
Throws:
XmlConfigException

getReferredElementContent

protected List<Element> getReferredElementContent(String name,
                                                  String path)
                                           throws XmlConfigException,
                                                  MissingReferableException
Throws:
XmlConfigException
MissingReferableException

initLogManagerConfiguration

public void initLogManagerConfiguration()
                                 throws XmlConfigException
Initializes the log manager configuration with logging configuration from the the default config. Calls LogManager.readConfiguration(InputStream) with properties read from the logging element.

The logging element is expected to look like this:

                
                          
                        
                        ...
                
 

Throws:
XmlConfigException - if an xml configuration error occurs

initLogManagerConfiguration

public void initLogManagerConfiguration(String configName)
                                 throws XmlConfigException
Initializes the log manager configuration with logging configuration from the the specified config. Calls LogManager.readConfiguration(InputStream) with properties read from the logging element.

The logging element is expected to look like this:

                
                          
                        
                        ...
                
 

Parameters:
configName - the config to use, corresponds to getConfig(String)
Throws:
XmlConfigException - if an xml configuration error occurs

getLoggingProperties

public Properties getLoggingProperties()
                                throws XmlConfigException
Returns the logging properties read from the logging element, or null if no logging properties found in the xml config.

The logging element is expected to look like this:

                
                          
                        
                        ...
                
 

Throws:
XmlConfigException - if an xml configuration error occurs

getLoggingProperties

public Properties getLoggingProperties(String configName)
                                throws XmlConfigException
Returns the logging properties read from the logging element, or null if no logging properties found in the xml config.

The logging element is expected to look like this:

                
                          
                        
                        ...
                
 

Parameters:
configName - the config to use, corresponds to getConfig(String)
Throws:
XmlConfigException - if an xml configuration error occurs

printUsage

public void printUsage(PrintStream stream,
                       String usageName)
                throws XmlConfigException
Throws:
XmlConfigException