PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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