CXM_xml_rpc_parser.anubis
13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Created by PyramIDE.
* User: Totoro
* Date: 06/07/2013
* Time: 01:13
*
*/
read calexium_lib/web/CXM_xml_rpc_types.anubis
read tools/streams.anubis
read tools/basis.anubis
read system/string.anubis
read system/convert.anubis
type XML_RPC_Token:
none,
token(String token).
define Maybe(XML_RPC_value) read_value(Stream stream).
define XML_RPC_Token
_next_xml_token
(
Stream stream,
List(Word8) so_far,
Bool in_token
)=
if read_byte(stream) is
{
failure then none, //can't read on stream !!
success(b) then
//println("["+implode([b])+"]");
if in_token then
if b = '>' then //just found the end of bracket, so we return the token in LOWER case
with tok = to_lower(implode(reverse(so_far))),
//println("found tag "+tok);
token(tok)
else
_next_xml_token(stream, [b . so_far], in_token)
else
if b = '<' then //just found the begin of token
_next_xml_token(stream, [], true)
else
_next_xml_token(stream, so_far, in_token)
}
.
define XML_RPC_Token
next_xml_token
(
Stream stream
)= _next_xml_token(stream, [], false).
define Maybe(String)
_xml_tag_content
(
Stream stream,
String tag, //tag to match
List(Word8) so_far,
List(Word8) content,
Bool in_first_token,
Bool in_content,
Bool in_last_token
)=
if read_byte(stream) is
{
failure then failure, //can't read on stream !!
success(b) then
if in_first_token then
if b = '>' then //just found the end of bracket, so we return the token in LOWER case
if to_lower(implode(reverse(so_far))) = tag then
_xml_tag_content(stream, tag, [], [], false, true, false)
else
failure
else
_xml_tag_content(stream, tag, [b . so_far], content, in_first_token, in_content, in_last_token)
else if in_content then
if b = '<' then //just found the begin bracket,
if read_byte(stream) is
{
failure then failure, //can't read on stream !!
success(_b) then
if _b = '/' then //can't find / => syntax error
_xml_tag_content(stream, tag, [], content, false, false, true)
else
failure
}
else
_xml_tag_content(stream, tag, [], [b . content], false, true, false)
else if in_last_token then
if b = '>' then //just found the end of bracket, so we return the token in LOWER case
if to_lower(implode(reverse(so_far))) = tag then
with _content = implode(reverse(content)),
println("Tag ["+tag+"] content found ["+_content+"]");
success(_content)
else
failure
else
_xml_tag_content(stream, tag, [b . so_far], content, false, false, true)
else
if b = '<' then //just found the begin of token
_xml_tag_content(stream, tag, [], [], true, false, false)
else
_xml_tag_content(stream, tag, [], [], false, false, false)
}
.
define Maybe(String)
xml_pair_tag_content
(
Stream stream,
String tag
)= _xml_tag_content( stream, tag, [], [], false, false, false).
define Maybe(String)
xml_tag_content
(
Stream stream,
String tag
)= _xml_tag_content( stream, tag, [], [], false, true, false).
/***** ARRAY functions ******/
define Maybe(List(XML_RPC_value))
read_values
(
Stream stream,
List(XML_RPC_value) so_far
)=
if read_value(stream) is
{
failure then failure,
success(value) then
if next_xml_token(stream) is
{
none then failure,
token(token) then
if token = "value" then //there is another value we read it
read_values(stream, [value . so_far])
else
unput_string("<"+token+">", stream);
success(reverse([value . so_far]))
}
}.
define Maybe(List(XML_RPC_value))
read_data
(
Stream stream
)=
if next_xml_token(stream) is
{
none then failure,
token(token) then
if token = "data" then
if next_xml_token(stream) is
{
none then failure,
token(token) then
if token = "value" then
if read_values(stream, []) is
{
failure then failure
success(values) then
if next_xml_token(stream) is
{
none then failure,
token(token) then
if token = "/data" then
success(values)
else
failure
}
}
// <data></data> mean empty array
else if token = "/data" then
success([])
else
failure
}
else
failure
}.
define Maybe(XML_RPC_value)
read_array
(
Stream stream
)=
if read_data(stream) is
{
failure then failure
success(values) then
if next_xml_token(stream) is
{
none then failure,
token(token) then
if token = "/array" then
success(array(array(values)))
else
failure
}
}.
/***** STRUCT functions ******/
define Maybe(List(XML_RPC_struct_member))
read_members
(
Stream stream,
List(XML_RPC_struct_member) so_far
)=
if xml_pair_tag_content(stream, "name") is
{
failure then failure,
success(member_name) then
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "value" then
if read_value(stream) is
{
failure then failure,
success(value) then
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "/member" then
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "member" then //there is another member in structure, we read it
read_members(stream, [member(member_name, value) . so_far])
else if tok = "/struct" then //End of structrue found
//println("End struct");
success(reverse([member(member_name, value) . so_far])) //return all members in right order
else
failure //unexpected token
}
else
failure
}
}
else
failure
}
} .
define Maybe(XML_RPC_value)
read_struct
(
Stream stream
)=
if next_xml_token(stream) is
{
none then failure,
token(token) then
//there is member hence read it
if token = "member" then
if read_members(stream, []) is
{
failure then failure
success(members_list) then success(struct(members(members_list)))
}
//the immediate following token is </struct>. Hence this is an empty struct
else if token = "/struct" then
success(struct(members([])))
else
failure
}.
define Maybe(XML_RPC_value)
read_value
(
Stream stream
)=
if next_xml_token(stream) is
{
none then failure,
token(token) then
with value = if token = "string" then
if xml_tag_content(stream, "string") is
{
failure then failure,
success(v) then success(string(v))
}
else if token = "int" then
if xml_tag_content(stream, "int") is
{
failure then failure,
success(v) then
if decimal_scan(v) is
{
failure then failure,
success(int_v) then success(int(truncate_to_Word32(int_v)))
}
}
else if token = "i4" then
if xml_tag_content(stream, "i4") is
{
failure then failure,
success(v) then
if decimal_scan(v) is
{
failure then failure,
success(int_v) then success(int(truncate_to_Word32(int_v)))
}
}
else if token = "boolean" then
if xml_tag_content(stream, "boolean") is
{
failure then failure,
success(v) then success(bool(to_Bool(v)))
}
else if token = "double" then
if xml_tag_content(stream, "string") is
{
failure then failure,
success(v) then success(double(0.0))
}
else if token = "datetime" then
if xml_tag_content(stream, "string") is
{
failure then failure,
success(v) then success(datetime(v))
}
else if token = "base64" then
if xml_tag_content(stream, "base64") is
{
failure then failure,
success(b64) then success(base64(b64))
}
else if token = "struct" then read_struct(stream)
else if token = "array" then read_array(stream)
else if token = "nil/" then success(nil)
else
println("Unknown <value> token ["+token+"]");
failure,
if next_xml_token(stream) is
{
none then failure
token(token) then
if token = "/value" then
value
else
failure
}
}.
define Maybe(XML_RPC_parameter)
in_value
(
Stream stream,
List(XML_RPC_value) so_far
)=
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "value" then
if read_value(stream) is
{
failure then failure,
success(value) then in_value(stream, [ value. so_far])
}
else if tok = "/param" then
success(parameter(reverse(so_far)))
else
failure
}.
define Maybe(XML_RPC_value)
in_fault
(
Stream stream,
)=
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "value" then
if read_value(stream) is
{
failure then failure,
success(value) then
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "/fault" then
success(value)
else
failure
}
}
else
failure
}.
define Maybe(XML_RPC_parameters)
in_param
(
Stream stream,
List(XML_RPC_parameter) so_far
)=
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "param" then
if in_value(stream, []) is
{
failure then failure,
success(param) then in_param(stream, [ param . so_far])
}
else if tok = "/params" then
success(parameters(reverse(so_far)))
else
failure
}
.
define Maybe(XML_RPC_response)
in_params
(
Stream stream
)=
if next_xml_token(stream) is
{
none then failure,
token(tok) then
if tok = "params" then
if in_param(stream, []) is
{
failure then failure,
success(resp) then success(ok(resp))
}
else if tok = "fault" then
if in_fault(stream) is
{
failure then failure,
success(resp) then success(fault(resp))
}
else
failure
}
.
public define Maybe(XML_RPC_response)
xml_rpc_get_response
(
String response
)=
with stream = make_stream(response),
if next_xml_token(stream) is
{
none then failure
token(tok) then
if tok = "?xml version='1.0'?" then
if next_xml_token(stream) is
{
none then failure
token(_tok) then
if _tok = "methodresponse" then
in_params(stream)
else
failure
}
else
failure
}.