/tutorial/storable/example2/example.e

http://github.com/tybor/Liberty · Specman e · 59 lines · 46 code · 7 blank · 6 comment · 1 complexity · 0237af41d90846406f00a7f26bb1d9cb MD5 · raw file

  1. class 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. create {ANY}
  7. make
  8. feature {}
  9. xml_repository: XML_FILE_REPOSITORY[ARRAY[STRING]]
  10. -- One can store several ARRAY[STRING] in that `xml_repository'.
  11. make
  12. local
  13. array_1, array_2: ARRAY[STRING]
  14. do
  15. if (create {FILE_TOOLS}).file_exists(repository_name) then
  16. std_output.put_string("Retrieve ARRAYs from the repository %"" + repository_name + "%"...%N")
  17. create xml_repository.connect_to(repository_name)
  18. array_1 := xml_repository.at("array_1")
  19. array_2 := xml_repository.at("array_2")
  20. display("array_1", array_1)
  21. display("array_2", array_2)
  22. -- In order to execute the else branch for the next run:
  23. (create {FILE_TOOLS}).delete(repository_name)
  24. std_output.put_string("Repository %"" + repository_name + "%" deleted.%N")
  25. else
  26. std_output.put_string("Saving objects to the repository %"" + repository_name + "%"...%N")
  27. create xml_repository.connect_to(repository_name)
  28. xml_repository.put(
  29. <<"foo", "bar">>, "array_1")
  30. xml_repository.put(
  31. <<"bar", "foo">>, "array_2")
  32. xml_repository.commit
  33. std_output.put_string("Curious may have a look at %"" + repository_name + "%" text file.%N")
  34. end
  35. end
  36. repository_name: STRING "my_repository.xml"
  37. display (name: STRING; array: ARRAY[STRING])
  38. local
  39. i: INTEGER
  40. do
  41. std_output.put_string(name + " = << ")
  42. from
  43. i := array.lower
  44. until
  45. i > array.upper
  46. loop
  47. std_output.put_string(array.item(i) + " ")
  48. i := i + 1
  49. end
  50. std_output.put_string(">>%N")
  51. end
  52. end -- class EXAMPLE