| 1 | /****************************************************************************** |
| 2 | * Copyright (c) 2000-2015 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 exer_uni { |
| 9 | import from ReadXml all; |
| 10 | import from rec { type Neg; function check_match } |
| 11 | |
| 12 | type union UT |
| 13 | { |
| 14 | integer i, |
| 15 | charstring s, |
| 16 | record { |
| 17 | boolean b, |
| 18 | float f |
| 19 | } r |
| 20 | } |
| 21 | with { |
| 22 | variant "useType" |
| 23 | } |
| 24 | |
| 25 | external function encUT(in UT u) return octetstring |
| 26 | with { |
| 27 | extension "prototype(convert) encode(XER:XER_EXTENDED)" |
| 28 | } |
| 29 | |
| 30 | const UT c_ut_i := { |
| 31 | i := 42 |
| 32 | } |
| 33 | |
| 34 | template Nodes t_ut_i := { |
| 35 | { XML_READER_TYPE_ELEMENT , 0, "UT", "", "" }, |
| 36 | { XML_READER_TYPE_TEXT , 1, "#text", "42", "" }, |
| 37 | { XML_READER_TYPE_END_ELEMENT, 0, "UT", "", "" } |
| 38 | } |
| 39 | |
| 40 | const UT c_ut_s := { |
| 41 | s := "Hello, world!" |
| 42 | } |
| 43 | |
| 44 | template Nodes t_ut_s := { |
| 45 | // node_type , depth, name, value, namespace |
| 46 | { XML_READER_TYPE_ELEMENT , 0, "UT", "", "" }, |
| 47 | { XML_READER_TYPE_ATTRIBUTE , 1, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2000/xmlns/" }, |
| 48 | // this is the value for "xmlns:xsi" ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 49 | // this is the namespace for "xsi:type" ------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 50 | { XML_READER_TYPE_ATTRIBUTE , 1, "xsi:type" , "s", "http://www.w3.org/2001/XMLSchema-instance" }, |
| 51 | { XML_READER_TYPE_TEXT , 1, "#text", "Hello, world!", "" }, |
| 52 | { XML_READER_TYPE_END_ELEMENT, 0, "UT", "", "" } |
| 53 | } |
| 54 | |
| 55 | const UT c_ut_rf := { |
| 56 | r := { b := true, f := 3.141592 } |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | <UT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='r'> |
| 61 | <b>true</b> |
| 62 | <f>3.141393</f> |
| 63 | </UT> |
| 64 | */ |
| 65 | template Nodes t_ut_rf := { |
| 66 | // node_type , depth, name, value, namespace |
| 67 | { XML_READER_TYPE_ELEMENT , 0, "UT", "", "" }, |
| 68 | { XML_READER_TYPE_ATTRIBUTE , 1, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2000/xmlns/" }, |
| 69 | { XML_READER_TYPE_ATTRIBUTE , 1, "xsi:type" , "r", "http://www.w3.org/2001/XMLSchema-instance" }, |
| 70 | |
| 71 | { XML_READER_TYPE_ELEMENT , 1, "b", "", "" }, |
| 72 | { XML_READER_TYPE_TEXT , 2, "#text", "true", "" }, |
| 73 | { XML_READER_TYPE_END_ELEMENT, 1, "b", "", "" }, |
| 74 | |
| 75 | { XML_READER_TYPE_ELEMENT , 1, "f", "", "" }, |
| 76 | { XML_READER_TYPE_TEXT , 2, "#text", "3.141592", "" }, |
| 77 | { XML_READER_TYPE_END_ELEMENT, 1, "f", "", "" }, |
| 78 | |
| 79 | { XML_READER_TYPE_END_ELEMENT, 0, "UT", "", "" } |
| 80 | } |
| 81 | |
| 82 | testcase ut_plain() runs on Neg |
| 83 | { |
| 84 | var octetstring o; |
| 85 | var Nodes nodes; |
| 86 | |
| 87 | o := encUT(c_ut_i); |
| 88 | nodes := gather(o, ignore_ws); |
| 89 | check_match(nodes, t_ut_i); |
| 90 | |
| 91 | o := encUT(c_ut_s); |
| 92 | nodes := gather(o, ignore_ws); |
| 93 | check_match(nodes, t_ut_s); |
| 94 | |
| 95 | o := encUT(c_ut_rf); |
| 96 | nodes := gather(o, ignore_ws); |
| 97 | check_match(nodes, t_ut_rf); |
| 98 | } |
| 99 | |
| 100 | // \\ // \\ |
| 101 | |
| 102 | const UT c_ut_i_replace := c_ut_i |
| 103 | with { |
| 104 | erroneous (i) "value := ""fourty-two"" " |
| 105 | } |
| 106 | |
| 107 | template Nodes t_ut_i_replace modifies t_ut_i := { |
| 108 | -, |
| 109 | { XML_READER_TYPE_TEXT , 1, "#text", "fourty-two", "" }, |
| 110 | - |
| 111 | } |
| 112 | |
| 113 | const UT c_ut_s_replace := c_ut_s |
| 114 | with { |
| 115 | erroneous (s) "value := 'DECAFBAD'O" |
| 116 | } // '4548 4c4c 2c4f 5720 524f 444c'O |
| 117 | |
| 118 | template Nodes t_ut_s_replace modifies t_ut_s := { |
| 119 | -,-,-, |
| 120 | { XML_READER_TYPE_TEXT , 1, "#text", "DECAFBAD", "" }, |
| 121 | - |
| 122 | } |
| 123 | |
| 124 | const UT c_ut_s_replace_raw := c_ut_s |
| 125 | with { |
| 126 | erroneous (s) "value(raw) := '48454c4c4f2c20574f524c44'O" |
| 127 | } |
| 128 | |
| 129 | template Nodes t_ut_s_replace_raw modifies t_ut_s := { |
| 130 | -,-,-, |
| 131 | { XML_READER_TYPE_TEXT , 1, "#text", "HELLO, WORLD", "" }, |
| 132 | - |
| 133 | } |
| 134 | |
| 135 | const UT c_ut_rf_replace := c_ut_rf |
| 136 | with { |
| 137 | erroneous (r) "value := UT.r : { b := true, f := 2.71828 }" |
| 138 | } |
| 139 | |
| 140 | template Nodes t_ut_rf_replace modifies t_ut_rf := { |
| 141 | -,-,-, |
| 142 | -,-,-, |
| 143 | -, |
| 144 | { XML_READER_TYPE_TEXT , 2, "#text", "2.718280", "" }, |
| 145 | -,- |
| 146 | } |
| 147 | |
| 148 | testcase ut_replace() runs on Neg |
| 149 | { |
| 150 | var octetstring o; |
| 151 | var Nodes nodes; |
| 152 | |
| 153 | o := encUT(c_ut_i_replace); |
| 154 | nodes := gather(o, ignore_ws); |
| 155 | check_match(nodes, t_ut_i_replace); |
| 156 | |
| 157 | o := encUT(c_ut_s_replace); |
| 158 | nodes := gather(o, ignore_ws); |
| 159 | check_match(nodes, t_ut_s_replace); |
| 160 | |
| 161 | o := encUT(c_ut_s_replace_raw); |
| 162 | nodes := gather(o, ignore_ws); |
| 163 | check_match(nodes, t_ut_s_replace_raw); |
| 164 | |
| 165 | o := encUT(c_ut_rf_replace); |
| 166 | nodes := gather(o, ignore_ws); |
| 167 | check_match(nodes, t_ut_rf_replace); |
| 168 | } |
| 169 | |
| 170 | // -- -- -- -- -- -- -- -- -- -- -- -- USE-TYPE |
| 171 | |
| 172 | type record R { |
| 173 | boolean available |
| 174 | } |
| 175 | with { |
| 176 | variant (available) "attribute" |
| 177 | } |
| 178 | |
| 179 | type union UT2 { |
| 180 | integer i, |
| 181 | charstring s, |
| 182 | R r |
| 183 | } |
| 184 | with { |
| 185 | variant "element"; |
| 186 | variant "useType"; |
| 187 | } |
| 188 | |
| 189 | external function encUT2(in UT2 u) return octetstring |
| 190 | with { extension "prototype(convert) encode(XER:XER_EXTENDED)" } |
| 191 | |
| 192 | const UT2 c_ut_r := { |
| 193 | r := { available := true } |
| 194 | } |
| 195 | |
| 196 | template Nodes t_ut_r := { |
| 197 | // node_type , depth, name, value, namespace |
| 198 | { XML_READER_TYPE_ELEMENT , 0, "UT2", "", "" }, |
| 199 | { XML_READER_TYPE_ATTRIBUTE , 1, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2000/xmlns/" }, |
| 200 | // this is the value for "xmlns:xsi" ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 201 | // this is the namespace for "xsi:type" ------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 202 | { XML_READER_TYPE_ATTRIBUTE , 1, "xsi:type" , "r", "http://www.w3.org/2001/XMLSchema-instance" }, |
| 203 | { XML_READER_TYPE_ATTRIBUTE , 1, "available", "true", "" }, |
| 204 | //{ XML_READER_TYPE_TEXT , 1, "#text", "\n", "" }, |
| 205 | { XML_READER_TYPE_END_ELEMENT, 0, "UT2", "", "" } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | testcase ut_nested_plain() runs on Neg |
| 210 | { |
| 211 | var octetstring o; |
| 212 | var Nodes nodes; |
| 213 | |
| 214 | o := encUT2(c_ut_r); |
| 215 | nodes := gather(o, ignore_ws); |
| 216 | check_match(nodes, t_ut_r); |
| 217 | } |
| 218 | |
| 219 | const UT2 c_ut_r_replace := c_ut_r |
| 220 | with { |
| 221 | erroneous (r) "value := R : { available := false } "; |
| 222 | } |
| 223 | |
| 224 | template Nodes t_ut_r_replace modifies t_ut_r := { |
| 225 | -,-,-, // start element, xmlns:xsi, xsi:type |
| 226 | { XML_READER_TYPE_ATTRIBUTE , 1, "available", "false", "" }, |
| 227 | - |
| 228 | } |
| 229 | |
| 230 | testcase ut_nested_replace() runs on Neg |
| 231 | { |
| 232 | var octetstring o; |
| 233 | var Nodes nodes; |
| 234 | |
| 235 | o := encUT2(c_ut_r_replace); |
| 236 | nodes := gather(o, ignore_ws); |
| 237 | check_match(nodes, t_ut_r_replace); |
| 238 | } |
| 239 | |
| 240 | // -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
| 241 | |
| 242 | // -- -- -- -- -- -- -- -- -- -- -- -- -- -- USE-UNION |
| 243 | |
| 244 | type union UU |
| 245 | { |
| 246 | integer i, |
| 247 | charstring s |
| 248 | } |
| 249 | with { |
| 250 | variant "useUnion"; |
| 251 | } |
| 252 | |
| 253 | external function encUU(in UU u) return octetstring |
| 254 | with { |
| 255 | extension "prototype(convert) encode(XER:XER_EXTENDED)" |
| 256 | } |
| 257 | |
| 258 | const UU c_uu_i := { |
| 259 | i := 42 |
| 260 | } |
| 261 | |
| 262 | template Nodes t_uu_i := { |
| 263 | { XML_READER_TYPE_ELEMENT , 0, "UU", "", "" }, |
| 264 | { XML_READER_TYPE_ATTRIBUTE , 1, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2000/xmlns/" }, |
| 265 | // this is the value for "xmlns:xsi" ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 266 | // this is the namespace for "xsi:type" ------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 267 | { XML_READER_TYPE_ATTRIBUTE , 1, "xsi:type" , "i", "http://www.w3.org/2001/XMLSchema-instance" }, |
| 268 | { XML_READER_TYPE_TEXT , 1, "#text", "42", "" }, |
| 269 | { XML_READER_TYPE_END_ELEMENT, 0, "UU", "", "" } |
| 270 | } |
| 271 | |
| 272 | const UU c_uu_s := { |
| 273 | s := "Hello, world!" |
| 274 | } |
| 275 | |
| 276 | template Nodes t_uu_s := { |
| 277 | // node_type , depth, name, value, namespace |
| 278 | { XML_READER_TYPE_ELEMENT , 0, "UU", "", "" }, |
| 279 | { XML_READER_TYPE_ATTRIBUTE , 1, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2000/xmlns/" }, |
| 280 | // this is the value for "xmlns:xsi" ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 281 | // this is the namespace for "xsi:type" ------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
| 282 | { XML_READER_TYPE_ATTRIBUTE , 1, "xsi:type" , "s", "http://www.w3.org/2001/XMLSchema-instance" }, |
| 283 | { XML_READER_TYPE_TEXT , 1, "#text", "Hello, world!", "" }, |
| 284 | { XML_READER_TYPE_END_ELEMENT, 0, "UU", "", "" } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | testcase uu_plain() runs on Neg |
| 289 | { |
| 290 | var octetstring o; |
| 291 | var Nodes nodes; |
| 292 | |
| 293 | o := encUU(c_uu_i); |
| 294 | nodes := gather(o, ignore_ws); |
| 295 | check_match(nodes, t_uu_i); |
| 296 | |
| 297 | o := encUU(c_uu_s); |
| 298 | nodes := gather(o, ignore_ws); |
| 299 | check_match(nodes, t_uu_s); |
| 300 | } |
| 301 | |
| 302 | } |
| 303 | with { |
| 304 | encode "XML"; |
| 305 | variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'" |
| 306 | } |