/tutorial/xml/dom/example1.e

http://github.com/tybor/Liberty · Specman e · 113 lines · 92 code · 13 blank · 8 comment · 5 complexity · 423843eeaf4c7159a6f0c1b2d5975e56 MD5 · raw file

  1. class EXAMPLE1
  2. --
  3. -- This simple example just uses XML_TREE.
  4. --
  5. -- See EXAMPLE2 on how to extend XML_TREE to add validation features.
  6. --
  7. inherit
  8. XML_NODE_VISITOR
  9. insert
  10. ARGUMENTS
  11. create {}
  12. make
  13. feature {}
  14. make
  15. local
  16. in: TEXT_FILE_READ; tree: XML_TREE; version: UNICODE_STRING
  17. do
  18. if argument_count = 0 then
  19. std_error.put_line(once "Usage: #(1) <file.xml>" # command_name)
  20. die_with_code(1)
  21. end
  22. -- first create the stream
  23. create in.connect_to(argument(1))
  24. if in.is_connected then
  25. -- then create the tree
  26. create tree.with_error_handler(in.url, agent error(?, ?))
  27. -- now display the results
  28. version := tree.attribute_at(once U"version")
  29. if version /= Void then
  30. io.put_string(once "XML version: ")
  31. io.put_string(version.as_utf8)
  32. io.put_new_line
  33. end
  34. check
  35. indent = 0
  36. end
  37. tree.root.accept(Current)
  38. end
  39. end
  40. indent: INTEGER
  41. feature {XML_DATA_NODE}
  42. visit_data_node (node: XML_DATA_NODE)
  43. do -- data not displayed in this example
  44. end
  45. feature {XML_COMPOSITE_NODE}
  46. visit_composite_node (node: XML_COMPOSITE_NODE)
  47. local
  48. i, start_indent: INTEGER
  49. do
  50. from
  51. i := 1
  52. until
  53. i > indent
  54. loop
  55. io.put_string(once " ")
  56. i := i + 1
  57. end
  58. io.put_string(node.name.as_utf8)
  59. if node.attributes_count > 0 then
  60. io.put_character('(')
  61. from
  62. i := 1
  63. until
  64. i > node.attributes_count
  65. loop
  66. if i > 1 then
  67. io.put_string(once ", ")
  68. end
  69. io.put_string(node.attribute_name(i).as_utf8)
  70. io.put_character('=')
  71. io.put_string(node.attribute_value(i).as_utf8)
  72. i := i + 1
  73. end
  74. io.put_character(')')
  75. end
  76. io.put_new_line
  77. from
  78. start_indent := indent
  79. indent := start_indent + 1
  80. i := 1
  81. invariant
  82. indent = start_indent + 1
  83. until
  84. i > node.children_count
  85. loop
  86. node.child(i).accept(Current)
  87. i := i + 1
  88. end
  89. indent := start_indent
  90. end
  91. error (line, column: INTEGER)
  92. do
  93. std_error.put_string(once "Error at ")
  94. std_error.put_integer(line)
  95. std_error.put_string(once ", ")
  96. std_error.put_integer(column)
  97. std_error.put_string(once "!%N")
  98. die_with_code(1)
  99. end
  100. end -- class EXAMPLE1