commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph.R...@dlr.de
Subject Re: [configuration] XmlConfiguration
Date Fri, 06 Dec 2002 14:32:35 GMT
Hi,

for anyone interested, I've a proweful/flexible XmlConfiguration implementation
that goes beyond just strings/bools/ints. It allows defining/creating objects
in an generic XML format that then can be randomly accessed from code (example
below).

The difference to Digester is that it uses a generic syntax and instantiates
an caches the object at first access, thus improving startup time for
frameworks using it.

The real power is in the INCLUDE and REF statements that allows assembling
and reusing definitions. TODO: change the access/xref syntax to XPATH.

Below is an XML file presenting the syntax and example usage.

It is mainly made up of 3 files:
   XmlConfiguration - The root class
   XmlConfigReader  - A static class to comfortably access a central config.
   XmlConfigWriter  - A class to serialize and store objects in our syntax.

Give me a mail if you think this could flow into commons. I can release it
under Apache license if desired.

-- 
:) Christoph Reck


The following text is an example of an XML configuration file with embedded comments describing
its syntax.

<!-- XML CONFIGURATION File
  ! $RCSfile: XML_Configuration.html,v $
  !
  ! Specification of the generic java object constructor configuration
  ! language.
  !
  ! Version: $Revision: 1.5 $
  ! Date:    $Date: 2000/05/09 13:42:16 $
  ! Author:  Christoph Reck, last modified by $Author: kiemles $
  !
  ! Copyright DLR 1999-2002
  !
  !-->

<!--
  ! SYNTAX DESCRIPTION:
  !
  ! <OBJECT      ID="string" NAME="string" CLASS="string" REF="string">
  ! <METHOD      ID="string" NAME="string" CLASS="string" REF="string">
  ! <FIELD       ID="string" NAME="string" CLASS="string" REF="string">
  ! <ARRAY       ID="string" NAME="string" CLASS="string">
  ! <STRING      ID="string" NAME="string" VALUE="string">
  ! <HASH        ID="string" NAME="string">
  ! <CONSTRUCTOR ID="string">
  ! <PACKAGE     ID="string">
  !
  ! COMMENTS:
  !  A STRING appends the contents to the value. The value may contain white spaces.
  !  A HASH permits getting an ordered list of the named entries via getNames().
  !
  ! ATTRIBUTES:
  ! ID="string"     - identifier to access it externally
  ! NAME="string"   - a pretty name used to title the object,
  !                   identify the field (only for field type),
  !                   or the method (only for method type)
  ! CLASS="string"  - dotted.classname in case of complex classes, or
  !                   simple classname in case of base classes (like int)
  ! REF="string"    - ID path of another Object, specified in the
  !                   same XML file. It do not have to be created before
  !                   it is being referenced. This is done automatically.
  ! "string"        - a text, that has to be quoted.
  !
  !
  ! USAGE:
  !
  ! The contents of XML Configuration files following this schema
  ! can be accessed via the static class de.dlr.dfd.dims.ot.control.Configuration
  !
  ! Initilization is done via:
  !   XmlConfigReader.init(parameter)   - Parameter can either be a file name
  !                                       (both absolute or relative), an URL
  !                                       or an InputStream.
  !
  ! After initialization the specific configuration item is retrieved via
  !   Object XmlConfigReader.get(String IDpath)
  ! or any of the other XmlConfigReader.* convenience methods getString(...).
  !
  ! IDpath          - The contents of the configuration file are accessed via
  !                   a string identifier. Items at inner hierarchy levels can
  !                   be accessed by concatenating the IDs in the path with
  !                   dots '.' beginning at the root node (exclusivly).
  !                 - An Object can not be accessed if there is a level
  !                   without ID between itself and the root node.
  !                 - The "PACKAGE" construct can be used to build
  !                   hierachical IDs without having to repeat the path
  !                   for all IDs within that level.
  ! Object          - The retrieved content is the instantiated Object, an
  !                   Array, a Hashtable, or the return value of methods or
  !                   fields.
  !
  ! Defaults        - The default class of ARRAY tags is Object.
  !                 - It is not neccessary to put Strings into an OBJECT tag.
  !
  ! Processing      - Creating an Object only the Constructor, the Method
  !                   and the Field tags, that are direct childs of this Object,
  !                   are processed.
  !                 - Other tags on this child-level are ignored, but can be
  !                   used to hold additional information for the Object (for
  !                   example a Hash, containing the allowed methods of a class,
  !                   or an OBJECT tag, containing just a name), which may be
  !                   accessed via user code, using the XmlConfigReader.get()
  !                   method with "ObjectIDPath.infoID" as parameter String.
  !
  !-->

<!-- EXAMPLES -->

<CONFIGURATION>
   <!-- Instantiate a class without a constructor (see NOTE 2) -->
   <OBJECT ID="HELLO" NAME="greeting message">Hello World!</OBJECT>

   <!-- Catenate String values -->
   <STRING ID="HELLO.AGAIN" VALUE="I say: ">Hello<STRING VALUE=" World " />
     <STRING>Again!</STRING>
   </STRING>

   <!-- Takeover the value of a (public) static constant -->
   <FIELD ID="CENTER" CLASS="javax.swing.SwingConstants" NAME="CENTER">
   </FIELD>

   <!-- Takeover the value from a static method -->
   <METHOD ID="ORB" CLASS="org.omg.CORBA" NAME="init">
   </METHOD>

   <!-- Invoke a static method -->
   <METHOD ID="FALSE" CLASS="java.lang.String" NAME="valueOf">
     <OBJECT CLASS="boolean">0</OBJECT>
   </METHOD>

   <!-- Instantiate a class with constructor values, fields, methods -->
   <OBJECT ID="Rectangle1" CLASS="java.awt.Rectangle">
     <CONSTRUCTOR>
       <OBJECT CLASS="int">1</OBJECT>
       <OBJECT CLASS="int">2</OBJECT>
       <OBJECT CLASS="int">3</OBJECT>
       <OBJECT CLASS="int">4</OBJECT>
     </CONSTRUCTOR>
     <FIELD NAME="witdh">
       <OBJECT CLASS="int">6</OBJECT>
     </FIELD>
     <METHOD NAME="setLocation">
       <OBJECT CLASS="int">7</OBJECT>
       <OBJECT CLASS="int">8</OBJECT>
     </METHOD>
     <OBJECT ID="NAME">The name of the rectangle</OBJECT>
   </OBJECT>

   <!-- Assign a value to a public field refferring an already
        instantiated object (see NOTE 3) -->
   <FIELD REF="Rectangle1" NAME="heigth">
     <OBJECT CLASS="int">9</OBJECT>
   </FIELD>

   <!-- Instantiate a class using fields and methods from another class -->
   <OBJECT ID="Rectangle2" CLASS="java.awt.Rectangle">
     <METHOD NAME="setLocation">
       <METHOD REF="Rectangle1" NAME="getLocation"></METHOD>
     </METHOD>
     <METHOD NAME="setSize">
       <OBJECT CLASS="java.awt.Point">
         <FIELD REF="Rectangle1" NAME="width"/>
         <FIELD REF="Rectangle1" NAME="heigth"/>
       </OBJECT>
     </METHOD>
   </OBJECT>

   <!-- Instantiate an array  -->
   <ARRAY ID="rectangle_array" CLASS="java.awt.Rectangle">
     <OBJECT REF="Rectangle1"/>
     <OBJECT REF="Rectangle2"/>
   </ARRAY>

   <!-- Instantiate a hashtable  -->
   <HASH ID="hashtable">
     <OBJECT CLASS="int" NAME="eins">1</OBJECT>
     <OBJECT CLASS="int" NAME="zwei">2</OBJECT>
   </HASH>

   <!-- Instantiate a Panel with a Gridlayout -->
   <OBJECT ID="Panel" CLASS="java.awt.Panel">
     <OBJECT CLASS="java.awt.GridLayout">
       <OBJECT ID="px" CLASS="int">3</OBJECT>
       <OBJECT ID="py" CLASS="int">6</OBJECT>
     </OBJECT>
   </OBJECT>

   <!-- Create an array with two rectangles, that are referenced -->
   <ARRAY ID="ReferenceArray" CLASS="java.awt.Rectangle">
     <OBJECT REF="Square"></OBJECT>
     <OBJECT REF="Square"></OBJECT>
   </ARRAY>
</CONFIGURATION>



<!-- NOTES:
  !
  ! NOTE1: due to java internals, only objects can be defined.
  !        Reflection-constructors and method-invocations will always
  !        use object[] as parameters, and the class parameter defines
  !        the exact type, e.g. 'int' instead of 'java.lang.Integer'.
  !        As a result of this, an 'int' class will return an 'Integer'.
  !
  ! NOTE2: the default class is 'java.lang.String'.
  !
  ! NOTE3: setting fields should normally be done via set-methods.
  !
  ! NOTE4: due to the use of SUNs XML-parser, a series of blanks is
  !        reduced to a single blank.
  !
  !-->

<!-- END of XML CONFIGURATION File -->

Mime
View raw message