/src/tools/wrappers-generator/workaround/xml_tree.e

http://github.com/tybor/Liberty · Specman e · 217 lines · 153 code · 26 blank · 38 comment · 7 complexity · 26f8fb3fe642a3c15e68426a386697c0 MD5 · raw file

  1. -- See the Copyright notice at the end of th file.
  2. --
  3. class XML_TREE
  4. --
  5. -- DOM-like representation of an XML document
  6. --
  7. -- See also XML_PARSER
  8. --
  9. inherit
  10. XML_CALLBACKS
  11. create {ANY}
  12. make, with_error_handler
  13. feature {ANY}
  14. root: XML_NODE
  15. -- The root of the tree
  16. attribute_at (a_attribute_name: STRING): STRING
  17. -- Usually to recover the "version" or "encoding" attributes
  18. do
  19. Result := tree_attributes.reference_at(a_attribute_name)
  20. end
  21. set_processing_instruction (target: STRING; processor: PROCEDURE[TUPLE[STRING]])
  22. require
  23. target /= Void
  24. processor /= Void
  25. local
  26. processors: FAST_ARRAY[PROCEDURE[TUPLE[STRING]]]
  27. do
  28. processors := processing_instructions.reference_at(target)
  29. if processors = Void then
  30. create processors.make(0)
  31. processing_instructions.add(processors, target.twin)
  32. end
  33. processors.add_last(processor)
  34. end
  35. feature {}
  36. attributes: HASHED_DICTIONARY[STRING, STRING]
  37. tree_attributes: like attributes
  38. open_nodes: STACK[XML_NODE]
  39. processing_instructions: HASHED_DICTIONARY[FAST_ARRAY[PROCEDURE[TUPLE[STRING]]], STRING]
  40. once
  41. create Result.make
  42. end
  43. feature {XML_PARSER}
  44. with_attribute (attribute_name: STRING; attribute_value: STRING; line, column: INTEGER)
  45. do
  46. if attribute_value=Void
  47. then attributes.put(Void, attribute_name.twin)
  48. else attributes.put(attribute_value.twin, attribute_name.twin)
  49. end
  50. end
  51. open_node (node_name: STRING; line, column: INTEGER)
  52. local
  53. node: XML_NODE; i: INTEGER
  54. do
  55. node := new_node(node_name.twin, line, column)
  56. from
  57. i := attributes.lower
  58. until
  59. i > attributes.upper
  60. loop
  61. node.set_attribute(attributes.key(i), attributes.item(i))
  62. i := i + 1
  63. end
  64. attributes.clear_count
  65. open_nodes.push(node)
  66. end
  67. close_node (node_name: STRING; line, column: INTEGER)
  68. local
  69. node: XML_NODE
  70. do
  71. node := open_nodes.top
  72. open_nodes.pop
  73. if open_nodes.is_empty then
  74. root := node
  75. else
  76. open_nodes.top.add_child(node)
  77. end
  78. end
  79. open_close_node (node_name: STRING; line, column: INTEGER)
  80. do
  81. open_node(node_name, line, column)
  82. close_node(node_name, line, column)
  83. end
  84. xml_header (line, column: INTEGER)
  85. do
  86. check
  87. tree_attributes.is_empty
  88. end
  89. tree_attributes.copy(attributes)
  90. attributes.clear_count
  91. end
  92. processing_instruction (a_target, a_data: STRING)
  93. local
  94. processors: FAST_ARRAY[PROCEDURE[TUPLE[STRING]]]; i: INTEGER
  95. do
  96. processors := processing_instructions.reference_at(a_target)
  97. if processors /= Void then
  98. from
  99. i := processors.lower
  100. until
  101. i > processors.upper
  102. loop
  103. processors.item(i).call([a_data])
  104. i := i + 1
  105. end
  106. end
  107. end
  108. current_node: STRING
  109. do
  110. if not open_nodes.is_empty then
  111. Result := open_nodes.top.name
  112. end
  113. end
  114. entity (a_entity: STRING; line, column: INTEGER): STRING
  115. do
  116. -- The default tree does not recognize any other entity than XML defaults.
  117. end
  118. data (a_data: STRING; line, column: INTEGER)
  119. do
  120. open_nodes.top.set_data(a_data.twin)
  121. end
  122. parse_error (line, column: INTEGER; message: STRING)
  123. do
  124. at_error := True
  125. if error_handler /= Void then
  126. error_handler.call([line, column, message])
  127. else
  128. std_error.put_string(message)
  129. std_error.put_string(" at line ")
  130. std_error.put_integer(line)
  131. std_error.put_string(", column ")
  132. std_error.put_integer(column)
  133. std_error.put_new_line
  134. die_with_code(1)
  135. end
  136. end
  137. at_error: BOOLEAN
  138. feature {}
  139. error_handler: PROCEDURE[TUPLE[INTEGER, INTEGER, STRING]]
  140. make (in: INPUT_STREAM)
  141. -- read the xml tree in the given input stream
  142. require
  143. in.is_connected
  144. do
  145. create attributes.make
  146. create tree_attributes.make
  147. create open_nodes.make
  148. parser.connect_to(in)
  149. parser.parse(Current)
  150. end
  151. with_error_handler (in: INPUT_STREAM; a_error_handler: like error_handler)
  152. do
  153. error_handler := a_error_handler
  154. make(in)
  155. end
  156. new_node (node_name: STRING; line, column: INTEGER): XML_NODE
  157. do
  158. create Result.make(node_name, line, column)
  159. end
  160. parser: XML_PARSER
  161. once
  162. create Result.make
  163. end
  164. end -- class XML_TREE
  165. --
  166. -- ------------------------------------------------------------------------------------------------------------
  167. -- Copyright notice below. Please read.
  168. --
  169. -- This file is part of the SmartEiffel standard library.
  170. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  171. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  172. --
  173. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  174. --
  175. -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  176. -- documentation files (the "Software"), to deal in the Software without restriction, including without
  177. -- limitation the rights to use, copy, modify, merge, publh, dtribute, sublicense, and/or sell copies of
  178. -- the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
  179. -- conditions:
  180. --
  181. -- The above copyright notice and th permsion notice shall be included in all copies or substantial
  182. -- portions of the Software.
  183. --
  184. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  185. -- LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
  186. -- EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  187. -- AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  188. -- OR OTHER DEALINGS IN THE SOFTWARE.
  189. --
  190. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  191. -- ------------------------------------------------------------------------------------------------------------