Configuration Files Made Easy

11 views
Skip to first unread message

Finnian Reilly

unread,
Jun 11, 2019, 4:10:28 PM6/11/19
to Eiffel Users
Configuration Files Made Easy

Introduction

I have been busying adding some new classes to Eiffel-Loop that completely automate saving and restoring Eiffel objects form XML configuration files. The automation is made possible by combining the reflection classes from Base Library: Runtime and the Eiffel object building classes from Persistency Library: Xpath Orientated Node-scanning and Object Building.
Behind  the scenes, the XML is parsed using the Expat parser.

An Example

The following 2 classes illustrate how to use them. Output in XML is also shown below that. The main thing to notice is that the output to XML and restoration from XML operates recursively as both TEST_CONFIGURATION and TEST_VALUES conform to class EL_REFLECTIVE_EIF_OBJ_BUILDER_CONTEXT.

class
   TEST_CONFIGURATION

inherit
   EL_REFLECTIVE_BUILDABLE_AND_STORABLE_AS_XML
      redefine
         make_default
     
end

create
   make_from_file
, make

feature
{NONE} -- Initialization

   make
(a_image_path: like image_path; a_name: like name; a_values: like values)
     
do
         make_default
         image_path
:= a_image_path; name := a_name; values := a_values
     
end

   make_default
     
do
         register_default_values
         create values_list
.make (3)
         values_list
.compare_objects
         create colors
.make (3)
         create integer_list
.make (3)
         colors
.compare_objects
         clipping
:= True
         
Precursor
     
end

feature
{NONE} -- Implementation

   register_default_values
      once
         
Default_value_table.extend_from_array (<< create {like values}.make_default >>)
     
end

feature
-- Access

   colors
: ARRAYED_LIST [STRING]

   image_path
: EL_FILE_PATH

   integer_list
: ARRAYED_LIST [INTEGER]

   name
: STRING

   values
: TEST_VALUES

   values_list
: ARRAYED_LIST [TEST_VALUES]

feature
-- Status query

   clipping
: EL_BOOLEAN_OPTION
end


class
   TEST_VALUES

inherit
   EL_REFLECTIVE_EIF_OBJ_BUILDER_CONTEXT
      rename
         xml_names
as export_default,
         element_node_type
as   Attribute_node
     
end

create
   make
, make_default

feature
{NONE} -- Initialization

   make
(a_double: like double; a_integer: like integer)
     
do
         
double := a_double; integer := a_integer
         make_default
     
end

feature
-- Element change

   set_double
(a_double: like double)
     
do
         
double := a_double
     
end

   set_integer
(a_integer: like integer)
     
do
         integer
:= a_integer
     
end

feature
-- Access

   
double: DOUBLE

   integer
: INTEGER

end


XML OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<test_configuration
   
name = "&apos;&amp;&apos; means &quot;and&quot;"
   
image_path = "/home/finnian/Graphics/icon.png"
   
clipping = "True"
   
last_store_ok = "False"
>
   
<values
     
integer = "1"
     
double = "1.1000000000000001"
   
/>
   
<values_list>
     
<item
         
integer = "1"
         
double = "1.1000000000000001"
     
/>
     
<item
         
integer = "2"
         
double = "2.2000000000000002"
     
/>
     
<item
         
integer = "3"
         
double = "3.3000000000000003"
     
/>
   
</values_list>
   
<colors>
     
<item>Red</item>
     
<item>Green</item>
     
<item>Blue</item>
   
</colors>
   
<integer_list>
     
<item>1</item>
     
<item>2</item>
     
<item>4</item>
     
<item>8</item>
   
</integer_list>
</test_configuration>


You will notice that the root element corresponds to the class name TEST_CONFIGURATION, and that the element and attribute names correspond to class attribute names. This output was generated by the test set class: REFLECTIVE_BUILDABLE_AND_STORABLE_AS_XML_TEST_EVALUATOR

Rules for Use
FAQ: What types of attributes can be serialized and restored? The following is a summary of the rules:

1. Basic Types and Strings
Attributes can be any of the basic expanded types, as well as STRING_8, STRING_32 and the ZSTRING class from Eiffel-Loop.

2. Recursive Sub-contexts
Attributes conforming to the recursive sub-context class: 
Any class like TEST_VALUES that implements this sub-context must a register a global instance for duplication. See the once routine register_default_values from the class TEST_CONFIGURATION called from make_default

(NOTE TO SELF: I should probably make `register_default_values' a deferred routine and have it called from EL_REFLECTIVE_BUILDABLE_AND_STORABLE_AS_XML.)

3. Collections
Attributes conforming to COLLECTION [X] where X is a type mentioned in 1 and 2.

4. Makeable from Strings
Attributes conforming to class EL_MAKEABLE_FROM_STRING_GENERAL
Some examples:

EL_MAKEABLE_FROM_STRING_GENERAL*
   EL_MAKEABLE_FROM_STRING_8*
      AIA_CREDENTIAL_ID
      EL_BOOLEAN_REF
         PP_ADDRESS_STATUS
      EL_ENUMERATION_VALUE*
         AIA_PURCHASE_REASON
         EL_CURRENCY_CODE
         PP_PAYMENT_PENDING_REASON
         PP_PAYMENT_STATUS
         PP_TRANSACTION_TYPE
      EL_ENCODING
      EL_UUID
   EL_MAKEABLE_FROM_STRING_32*
   EL_MAKEABLE_FROM_ZSTRING*

5. Other String Convertable Types
Objects conforming to DATE_TIME, EL_BOOLEAN_REF and EL_PATH

6. TUPLES
Not currently implemented, but a future version will provide for tuples of basic expanded
types and strings.

Reply all
Reply to author
Forward
0 new messages