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