Configuration Files Made Easy
Introduction
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 = "'&' means "and""
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>
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
Some examples: