/tutorial/xml/dom/example1.e
Specman e | 113 lines | 92 code | 13 blank | 8 comment | 5 complexity | 423843eeaf4c7159a6f0c1b2d5975e56 MD5 | raw file
1class EXAMPLE1 2 -- 3 -- This simple example just uses XML_TREE. 4 -- 5 -- See EXAMPLE2 on how to extend XML_TREE to add validation features. 6 -- 7 8inherit 9 XML_NODE_VISITOR 10 11insert 12 ARGUMENTS 13 14create {} 15 make 16 17feature {} 18 make 19 local 20 in: TEXT_FILE_READ; tree: XML_TREE; version: UNICODE_STRING 21 do 22 if argument_count = 0 then 23 std_error.put_line(once "Usage: #(1) <file.xml>" # command_name) 24 die_with_code(1) 25 end 26 -- first create the stream 27 create in.connect_to(argument(1)) 28 if in.is_connected then 29 -- then create the tree 30 create tree.with_error_handler(in.url, agent error(?, ?)) 31 -- now display the results 32 version := tree.attribute_at(once U"version") 33 if version /= Void then 34 io.put_string(once "XML version: ") 35 io.put_string(version.as_utf8) 36 io.put_new_line 37 end 38 39 check 40 indent = 0 41 end 42 tree.root.accept(Current) 43 end 44 end 45 46 indent: INTEGER 47 48feature {XML_DATA_NODE} 49 visit_data_node (node: XML_DATA_NODE) 50 do -- data not displayed in this example 51 end 52 53feature {XML_COMPOSITE_NODE} 54 visit_composite_node (node: XML_COMPOSITE_NODE) 55 local 56 i, start_indent: INTEGER 57 do 58 from 59 i := 1 60 until 61 i > indent 62 loop 63 io.put_string(once " ") 64 i := i + 1 65 end 66 io.put_string(node.name.as_utf8) 67 if node.attributes_count > 0 then 68 io.put_character('(') 69 from 70 i := 1 71 until 72 i > node.attributes_count 73 loop 74 if i > 1 then 75 io.put_string(once ", ") 76 end 77 io.put_string(node.attribute_name(i).as_utf8) 78 io.put_character('=') 79 io.put_string(node.attribute_value(i).as_utf8) 80 i := i + 1 81 end 82 83 io.put_character(')') 84 end 85 86 io.put_new_line 87 from 88 start_indent := indent 89 indent := start_indent + 1 90 i := 1 91 invariant 92 indent = start_indent + 1 93 until 94 i > node.children_count 95 loop 96 node.child(i).accept(Current) 97 i := i + 1 98 end 99 100 indent := start_indent 101 end 102 103 error (line, column: INTEGER) 104 do 105 std_error.put_string(once "Error at ") 106 std_error.put_integer(line) 107 std_error.put_string(once ", ") 108 std_error.put_integer(column) 109 std_error.put_string(once "!%N") 110 die_with_code(1) 111 end 112 113end -- class EXAMPLE1