Titan Core Initial Contribution
[deliverable/titan.core.git] / regression_test / XML / NegativeTest / ReadXml.ttcn
1 /******************************************************************************
2 * Copyright (c) 2000-2014 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 module ReadXml
9 {
10
11 external function FromMemory(in octetstring data) return integer;
12
13 //not supported by XmlReaderWrap
14 //external function FromFile(in charstring filename) return integer;
15
16 external function Cleanup();
17
18 // return 1 on success, 0 if no more nodes, -1 on error
19 external function XmlRead() return integer;
20 // Gotcha! Can't call it "Read" due to macros in XmlReader.hh
21
22 type enumerated xmlReaderTypes // copy-paste from libxml2/xmlreader.h
23 {
24 XML_READER_TYPE_NONE (0),
25 XML_READER_TYPE_ELEMENT (1),
26 XML_READER_TYPE_ATTRIBUTE (2),
27 XML_READER_TYPE_TEXT (3),
28 XML_READER_TYPE_CDATA (4),
29 XML_READER_TYPE_ENTITY_REFERENCE (5),
30 XML_READER_TYPE_ENTITY (6),
31 XML_READER_TYPE_PROCESSING_INSTRUCTION (7),
32 XML_READER_TYPE_COMMENT (8),
33 XML_READER_TYPE_DOCUMENT (9),
34 XML_READER_TYPE_DOCUMENT_TYPE (10),
35 XML_READER_TYPE_DOCUMENT_FRAGMENT (11),
36 XML_READER_TYPE_NOTATION (12),
37 XML_READER_TYPE_WHITESPACE (13),
38 XML_READER_TYPE_SIGNIFICANT_WHITESPACE (14),
39 XML_READER_TYPE_END_ELEMENT (15),
40 XML_READER_TYPE_END_ENTITY (16),
41 XML_READER_TYPE_XML_DECLARATION (17)
42 } ;
43
44 external function NodeType() return xmlReaderTypes;
45
46 external function Name() return charstring;
47
48 external function Value() return charstring;
49
50 external function NsUri() return charstring;
51
52 external function Depth() return integer;
53
54 external function FirstAttribute() return integer;
55 // ^^-- same problem as Read --VV
56 external function NextAttribute() return integer;
57
58 /* * * * * * * * * * * * * * * * */
59
60 type record XmlNode {
61 xmlReaderTypes node_type,
62 integer depth,
63 universal charstring node_name,
64 universal charstring node_value,
65 universal charstring ns_uri
66 }
67
68 type record of XmlNode Nodes;
69
70 type enumerated Ignore_ws { no, ignore_ws }
71 type enumerated Ignore_txt{ no, ignore_text }
72
73 function gather(in octetstring data, in Ignore_ws ws := no, in Ignore_txt txt := no) return Nodes
74 {
75 var integer ret;
76 var integer numnodes := 0;
77 var Nodes nodes;
78 ret := FromMemory(data);
79 // TODO check return value
80 for (ret := XmlRead(); ret > 0; ret := XmlRead()) {
81 var xmlReaderTypes t := NodeType();
82 if ((ws != no and ((t == XML_READER_TYPE_WHITESPACE) or (t == XML_READER_TYPE_SIGNIFICANT_WHITESPACE)))
83 or (txt!= no and (t == XML_READER_TYPE_TEXT))) {
84 //log("skipped a ", t);
85 }
86 else { // collect it
87 var XmlNode this_node;
88 this_node.node_type := t;
89 this_node.depth := Depth();
90 this_node.node_name := oct2unichar(char2oct(Name()));
91 this_node.node_value:= oct2unichar(char2oct(Value()));
92 this_node.ns_uri := oct2unichar(char2oct(NsUri()));
93
94 nodes[numnodes] := this_node;
95 numnodes := numnodes + 1;
96
97 if (t == XML_READER_TYPE_ELEMENT) {
98 for (ret := FirstAttribute(); ret > 0; ret := NextAttribute()) {
99 var XmlNode this_attribute := {
100 node_type := NodeType(), // usually XML_READER_TYPE_ATTRIBUTE,
101 depth := Depth(),
102 node_name := oct2unichar(char2oct(Name())),
103 node_value := oct2unichar(char2oct(Value())),
104 ns_uri := oct2unichar(char2oct(NsUri()))
105 }
106
107 nodes[numnodes] := this_attribute;
108 numnodes := numnodes + 1;
109 }
110 }
111 }
112 }
113 Cleanup();
114 return nodes;
115 }
116
117 } // end of module
This page took 0.033579 seconds and 6 git commands to generate.