/tutorial/storable/example2/example.e
Specman e | 59 lines | 46 code | 7 blank | 6 comment | 1 complexity | 0237af41d90846406f00a7f26bb1d9cb MD5 | raw file
1class EXAMPLE 2 -- 3 -- Simple example to start with STORABLEs. 4 -- Here you will learn how to store/retrieve two ARRAYs in an xml file. 5 -- 6 7create {ANY} 8 make 9 10feature {} 11 xml_repository: XML_FILE_REPOSITORY[ARRAY[STRING]] 12 -- One can store several ARRAY[STRING] in that `xml_repository'. 13 14 make 15 local 16 array_1, array_2: ARRAY[STRING] 17 do 18 if (create {FILE_TOOLS}).file_exists(repository_name) then 19 std_output.put_string("Retrieve ARRAYs from the repository %"" + repository_name + "%"...%N") 20 create xml_repository.connect_to(repository_name) 21 array_1 := xml_repository.at("array_1") 22 array_2 := xml_repository.at("array_2") 23 display("array_1", array_1) 24 display("array_2", array_2) 25 -- In order to execute the else branch for the next run: 26 (create {FILE_TOOLS}).delete(repository_name) 27 std_output.put_string("Repository %"" + repository_name + "%" deleted.%N") 28 else 29 std_output.put_string("Saving objects to the repository %"" + repository_name + "%"...%N") 30 create xml_repository.connect_to(repository_name) 31 xml_repository.put( 32 <<"foo", "bar">>, "array_1") 33 xml_repository.put( 34 <<"bar", "foo">>, "array_2") 35 xml_repository.commit 36 std_output.put_string("Curious may have a look at %"" + repository_name + "%" text file.%N") 37 end 38 end 39 40 repository_name: STRING "my_repository.xml" 41 42 display (name: STRING; array: ARRAY[STRING]) 43 local 44 i: INTEGER 45 do 46 std_output.put_string(name + " = << ") 47 from 48 i := array.lower 49 until 50 i > array.upper 51 loop 52 std_output.put_string(array.item(i) + " ") 53 i := i + 1 54 end 55 56 std_output.put_string(">>%N") 57 end 58 59end -- class EXAMPLE