/src/lib/xml/xml_tree.e

http://github.com/tybor/Liberty · Specman e · 224 lines · 163 code · 28 blank · 33 comment · 7 complexity · e397bd1d1fcd6f979667ad9aae24a150 MD5 · raw file

  1. -- See the Copyright notice at the end of this 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_COMPOSITE_NODE
  15. -- The root of the tree
  16. attribute_at (a_attribute_name: UNICODE_STRING): UNICODE_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: UNICODE_STRING; processor: PROCEDURE[TUPLE[UNICODE_STRING]])
  22. require
  23. target /= Void
  24. processor /= Void
  25. local
  26. processors: FAST_ARRAY[PROCEDURE[TUPLE[UNICODE_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[UNICODE_STRING, UNICODE_STRING]
  37. tree_attributes: like attributes
  38. open_nodes: STACK[XML_COMPOSITE_NODE]
  39. processing_instructions: HASHED_DICTIONARY[FAST_ARRAY[PROCEDURE[TUPLE[UNICODE_STRING]]], UNICODE_STRING]
  40. once
  41. create Result.make
  42. end
  43. feature {XML_PARSER}
  44. with_attribute (attribute_name: UNICODE_STRING; attribute_value: UNICODE_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: UNICODE_STRING; line, column: INTEGER)
  52. local
  53. node: XML_COMPOSITE_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: UNICODE_STRING; line, column: INTEGER)
  68. local
  69. node: XML_COMPOSITE_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: UNICODE_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: UNICODE_STRING)
  93. local
  94. processors: FAST_ARRAY[PROCEDURE[TUPLE[UNICODE_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: UNICODE_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: UNICODE_STRING; line, column: INTEGER): UNICODE_STRING
  115. do
  116. -- The default tree does not recognize any other entity than XML defaults.
  117. end
  118. open_entity_url (a_entity: UNICODE_STRING; a_url: URL)
  119. do
  120. -- nothing to do?
  121. end
  122. close_entity_url (a_entity: UNICODE_STRING; a_url: URL)
  123. do
  124. -- nothing to do?
  125. end
  126. data (a_data: UNICODE_STRING; line, column: INTEGER)
  127. local
  128. d: XML_DATA_NODE
  129. do
  130. create d.make(a_data.twin, line, column)
  131. open_nodes.top.add_child(d)
  132. end
  133. parse_error (line, column: INTEGER; message: STRING)
  134. do
  135. at_error := True
  136. if error_handler /= Void then
  137. error_handler.call([line, column, message])
  138. else
  139. std_error.put_string(message)
  140. std_error.put_string(" at line ")
  141. std_error.put_integer(line)
  142. std_error.put_string(", column ")
  143. std_error.put_integer(column)
  144. std_error.put_new_line
  145. die_with_code(1)
  146. end
  147. end
  148. at_error: BOOLEAN
  149. feature {}
  150. error_handler: PROCEDURE[TUPLE[INTEGER, INTEGER, STRING]]
  151. make (url: URL)
  152. -- read the xml tree at the given `url'
  153. require
  154. url.is_connected implies url.read
  155. do
  156. create attributes.make
  157. create tree_attributes.make
  158. create open_nodes.make
  159. parser.connect_to(url)
  160. parser.parse(Current)
  161. parser.disconnect
  162. end
  163. with_error_handler (url: URL; a_error_handler: like error_handler)
  164. do
  165. error_handler := a_error_handler
  166. make(url)
  167. end
  168. new_node (node_name: UNICODE_STRING; line, column: INTEGER): XML_COMPOSITE_NODE
  169. do
  170. create Result.make(node_name, line, column)
  171. end
  172. parser: XML_PARSER
  173. once
  174. create Result.make
  175. end
  176. end -- class XML_TREE
  177. --
  178. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  179. --
  180. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  181. -- of this software and associated documentation files (the "Software"), to deal
  182. -- in the Software without restriction, including without limitation the rights
  183. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  184. -- copies of the Software, and to permit persons to whom the Software is
  185. -- furnished to do so, subject to the following conditions:
  186. --
  187. -- The above copyright notice and this permission notice shall be included in
  188. -- all copies or substantial portions of the Software.
  189. --
  190. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  191. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  192. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  193. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  194. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  195. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  196. -- THE SOFTWARE.