/tutorial/xml/sax/example.e

http://github.com/tybor/Liberty · Specman e · 164 lines · 130 code · 25 blank · 9 comment · 2 complexity · 8475f2537f9ead54232e39b768d641f8 MD5 · raw file

  1. class EXAMPLE
  2. --
  3. -- Use the event-oriented interface
  4. --
  5. inherit
  6. XML_CALLBACKS
  7. insert
  8. ARGUMENTS
  9. create {}
  10. make
  11. feature {}
  12. parser: XML_PARSER
  13. make
  14. local
  15. in: URL
  16. do
  17. create in.absolute("file://" + argument(1))
  18. -- connect the parser
  19. create parser.connect_to(in)
  20. -- create the nodes stack (current_node must be correctly
  21. -- implemented because the contracts check that)
  22. create nodes.make
  23. -- parse the flow. Everything else is handled by the
  24. -- parser that calls back the following features.
  25. parser.parse(Current)
  26. -- then disconnect
  27. parser.disconnect
  28. if at_error then
  29. die_with_code(1)
  30. end
  31. end
  32. feature {XML_PARSER}
  33. nodes: STACK[UNICODE_STRING]
  34. with_attribute (attribute_name: UNICODE_STRING; attribute_value: UNICODE_STRING; line, column: INTEGER)
  35. do
  36. io.put_string(once "with attribute: ")
  37. io.put_unicode_string(attribute_name)
  38. io.put_string(once "='")
  39. io.put_unicode_string(attribute_value)
  40. io.put_string(once "' at ")
  41. io.put_integer(line)
  42. io.put_string(once ", ")
  43. io.put_integer(column)
  44. io.put_new_line
  45. end
  46. open_node (node_name: UNICODE_STRING; line, column: INTEGER)
  47. do
  48. io.put_string(once "open node %"")
  49. io.put_unicode_string(node_name)
  50. io.put_string(once "%" at ")
  51. io.put_integer(line)
  52. io.put_string(once ", ")
  53. io.put_integer(column)
  54. io.put_new_line
  55. nodes.push(node_name)
  56. end
  57. close_node (node_name: UNICODE_STRING; line, column: INTEGER)
  58. do
  59. io.put_string(once "close node %"")
  60. io.put_unicode_string(node_name)
  61. io.put_string(once "%" at ")
  62. io.put_integer(line)
  63. io.put_string(once ", ")
  64. io.put_integer(column)
  65. io.put_new_line
  66. nodes.pop
  67. end
  68. open_close_node (node_name: UNICODE_STRING; line, column: INTEGER)
  69. do
  70. io.put_string(once "open and close node %"")
  71. io.put_unicode_string(node_name)
  72. io.put_string(once "%" at ")
  73. io.put_integer(line)
  74. io.put_string(once ", ")
  75. io.put_integer(column)
  76. io.put_new_line
  77. end
  78. xml_header (line, column: INTEGER)
  79. do
  80. io.put_string(once "that's the XML header at ")
  81. io.put_integer(line)
  82. io.put_string(once ", ")
  83. io.put_integer(column)
  84. io.put_new_line
  85. end
  86. processing_instruction (a_target, a_data: UNICODE_STRING)
  87. do
  88. io.put_string(once "a processing instruction: target is %"")
  89. io.put_unicode_string(a_target)
  90. io.put_string(once "%", data is %"")
  91. io.put_unicode_string(a_data)
  92. io.put_string(once "%"")
  93. end
  94. current_node: UNICODE_STRING
  95. do
  96. if not nodes.is_empty then
  97. Result := nodes.top
  98. end
  99. end
  100. entity (a_entity: UNICODE_STRING; line, column: INTEGER): UNICODE_STRING
  101. do -- no entity recognized
  102. end
  103. data (a_data: UNICODE_STRING; line, column: INTEGER)
  104. do
  105. io.put_string(once "data at ")
  106. io.put_integer(line)
  107. io.put_string(once ", ")
  108. io.put_integer(column)
  109. io.put_string(once ":%N")
  110. io.put_unicode_string(a_data)
  111. io.put_string(once "%N.%N")
  112. end
  113. parse_error (line, column: INTEGER; message: STRING)
  114. do
  115. at_error := True -- that's useless since we die, but respect the postcondition anyway ;-)
  116. io.put_string(once "error at ")
  117. io.put_integer(line)
  118. io.put_string(once ", ")
  119. io.put_integer(column)
  120. io.put_string(once "!%N")
  121. io.put_line(message)
  122. sedb_breakpoint
  123. end
  124. at_error: BOOLEAN
  125. open_entity_url (a_entity: UNICODE_STRING; a_url: URL)
  126. do
  127. io.put_string(once "open entity url &")
  128. io.put_string(a_entity.as_utf8)
  129. io.put_string(once ": ")
  130. io.put_line(a_url.out)
  131. end
  132. close_entity_url (a_entity: UNICODE_STRING; a_url: URL)
  133. do
  134. io.put_string(once "close entity url &")
  135. io.put_string(a_entity.as_utf8)
  136. io.put_string(once ": ")
  137. io.put_line(a_url.out)
  138. end
  139. end -- class EXAMPLE