-
Notifications
You must be signed in to change notification settings - Fork 94
/
webservice.sl
167 lines (140 loc) · 3.81 KB
/
webservice.sl
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
# Sleep Webservice Utilities
#
# To use this:
# include('webservice.sl');
#
# debug(7 | 34) highly recommended
#
# Functions:
#
# $ post("url", %hash)
# connects to "url", posts the data in the hash, returns a handle for reading
#
# % buildDataStructure("<xml>...</xml>");
# creates a sleep hash tree from the XML document. each node in the hash is:
# name => string : tagname
# text => string : raw content of the tag (may be a bunch of spaces)
# attributes => hash : attributes for the tag
# children => array : an array of nodes (with this same structure)
#
# childrenToHash(%element)
# constructs a hash out of an element's children... uses the tagnames for the
# keys and each element for the values.
#
# walkData(%data, %functions)
# walks through %data, calls the function held by %functions["tagName"] when
# a tag is encountered. Argument $1 to the function is the current node.
# uses a postorder traversal; children left to right, parent
#
# printNice(%data);
# pretty prints a sleep hash tree
#
# Dependencies:
#
# This code assumes the following jars are in your Java classpath:
#
# commons-codec-1.2.jar - http://archive.apache.org/dist/commons/codec/
# commons-httpclient-3.1.jar - http://hc.apache.org/downloads.cgi
# commons-logging-api.jar - http://archive.apache.org/dist/commons/logging/binaries/
# commons-logging.jar - http://archive.apache.org/dist/commons/logging/binaries/
# jdom.jar - http://www.jdom.org
#
# Contact:
#
# Raphael Mudge ([email protected])
#
# Release 15 Jul 08
#
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import sleep.bridges.io.IOObject;
import org.jdom.*;
import org.jdom.input.*;
sub post
{
local('$client $method $key $value $code');
$client = [new HttpClient];
$method = [new PostMethod: $1];
foreach $key => $value ($2)
{
[$method addParameter: $key, $value];
}
$code = [$client executeMethod: $method];
if ($code != [HttpStatus SC_OK])
{
throw "Method failed: " . [$method getStatusLine];
}
return [SleepUtils getIOHandle: [$method getResponseBodyAsStream], $null];
}
sub buildDataStructure
{
local('$builder $doc $buffer');
$buffer = allocate(strlen($1));
writeb($buffer, $1);
closef($buffer);
$builder = [new SAXBuilder];
$doc = [$builder build: [$buffer getInputStream]];
closef($buffer);
return [{
local('$child %result');
%result["name"] = [$1 getName];
%result["text"] = [$1 getText];
%result["attributes"] = [{
local('$attr %result');
foreach $attr ([SleepUtils getArrayWrapper: $1])
{
%result[[$attr getName]] = [$attr getValue];
}
return %result;
}: [$1 getAttributes]];
%result["children"] = @();
foreach $child ([SleepUtils getArrayWrapper: [$1 getChildren]])
{
push(%result["children"], [$this: $child]);
}
return %result;
}: [$doc getRootElement]];
}
sub walkData
{
local('$child');
foreach $child ($1["children"])
{
walkData($child, $2);
}
invoke($2, @($1), $1["name"]);
}
sub childrenToHash
{
local('$child %results');
foreach $child ($1["children"])
{
%results[$child["name"]] = $child["text"];
}
return %results;
}
sub printNice
{
local('$k $v $2');
if (-isarray $1)
{
foreach $k => $v ($1)
{
println("$2 $+ $k $+ :");
printNice($v, "$2 ");
}
}
else if (-ishash $1)
{
foreach $k => $v ($1)
{
println("$2 $+ $k $+ :");
printNice($v, "$2 ");
}
}
else
{
println("$2 $+ $1");
}
}