dango_generator 0.0.38
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +16 -0
- data/README.txt +41 -0
- data/dango_generator.rb +99 -0
- data/templates/as3/as3corelib-license.txt +33 -0
- data/templates/as3/as3corelib-readme.txt +5 -0
- data/templates/as3/com/adobe/crypto/MD5.as +256 -0
- data/templates/as3/com/adobe/crypto/SHA1.as +268 -0
- data/templates/as3/com/adobe/crypto/SHA224.as +255 -0
- data/templates/as3/com/adobe/crypto/SHA256.as +260 -0
- data/templates/as3/com/adobe/crypto/WSSEUsernameToken.as +117 -0
- data/templates/as3/com/adobe/errors/IllegalStateError.as +66 -0
- data/templates/as3/com/adobe/images/BitString.as +42 -0
- data/templates/as3/com/adobe/images/JPGEncoder.as +651 -0
- data/templates/as3/com/adobe/images/PNGEncoder.as +144 -0
- data/templates/as3/com/adobe/net/DynamicURLLoader.as +58 -0
- data/templates/as3/com/adobe/net/IURIResolver.as +79 -0
- data/templates/as3/com/adobe/net/URI.as +2469 -0
- data/templates/as3/com/adobe/net/URIEncodingBitmap.as +142 -0
- data/templates/as3/com/adobe/net/proxies/RFC2817Socket.as +204 -0
- data/templates/as3/com/adobe/serialization/json/JSON.as +88 -0
- data/templates/as3/com/adobe/serialization/json/JSONDecoder.as +218 -0
- data/templates/as3/com/adobe/serialization/json/JSONEncoder.as +302 -0
- data/templates/as3/com/adobe/serialization/json/JSONParseError.as +90 -0
- data/templates/as3/com/adobe/serialization/json/JSONToken.as +107 -0
- data/templates/as3/com/adobe/serialization/json/JSONTokenType.as +70 -0
- data/templates/as3/com/adobe/serialization/json/JSONTokenizer.as +550 -0
- data/templates/as3/com/adobe/utils/ArrayUtil.as +190 -0
- data/templates/as3/com/adobe/utils/DateUtil.as +666 -0
- data/templates/as3/com/adobe/utils/DictionaryUtil.as +90 -0
- data/templates/as3/com/adobe/utils/IntUtil.as +69 -0
- data/templates/as3/com/adobe/utils/NumberFormatter.as +77 -0
- data/templates/as3/com/adobe/utils/StringUtil.as +257 -0
- data/templates/as3/com/adobe/utils/XMLUtil.as +171 -0
- data/templates/as3/com/adobe/webapis/ServiceBase.as +51 -0
- data/templates/as3/com/adobe/webapis/URLLoaderBase.as +111 -0
- data/templates/as3/com/adobe/webapis/events/ServiceEvent.as +78 -0
- data/templates/as3/org/rubyforge/dango/DangoClientFramework.as +426 -0
- data/templates/as3/org/rubyforge/dango/DangoError.as +11 -0
- data/templates/as3/org/rubyforge/dango/DangoErrorCode.as +7 -0
- data/templates/as3/org/rubyforge/dango/DangoErrorEvent.as +23 -0
- data/templates/as3/org/rubyforge/dango/DangoReceiveEvent.as +21 -0
- data/templates/as3/org/rubyforge/dango/DangoURLLoader.as +127 -0
- data/templates/as3/org/rubyforge/dango/DangoURLLoaderEvent.as +29 -0
- data/templates/as3/org/rubyforge/dango/DangoUtil.as +84 -0
- data/templates/dango/config/development.yml +28 -0
- data/templates/dango/config/production.yml +3 -0
- data/templates/dango/config/system_message.yml +5 -0
- data/templates/dango/config/test.yml +3 -0
- data/templates/dango/server/99_dango_server.rb +22 -0
- data/templates/lib/dango_monitor_client.rb +1 -0
- data/templates/lib/dango_tester_client.rb +1 -0
- data/templates/script/dango_server +36 -0
- data/templates/tasks/dango.rake +2 -0
- metadata +108 -0
@@ -0,0 +1,142 @@
|
|
1
|
+
/*
|
2
|
+
Adobe Systems Incorporated(r) Source Code License Agreement
|
3
|
+
Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
4
|
+
|
5
|
+
Please read this Source Code License Agreement carefully before using
|
6
|
+
the source code.
|
7
|
+
|
8
|
+
Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
9
|
+
no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
10
|
+
prepare derivative works of, publicly display, publicly perform, and
|
11
|
+
distribute this source code and such derivative works in source or
|
12
|
+
object code form without any attribution requirements.
|
13
|
+
|
14
|
+
The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
15
|
+
derived from the source code without prior written permission.
|
16
|
+
|
17
|
+
You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
18
|
+
against any loss, damage, claims or lawsuits, including attorney's
|
19
|
+
fees that arise or result from your use or distribution of the source
|
20
|
+
code.
|
21
|
+
|
22
|
+
THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
23
|
+
ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
24
|
+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
25
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
26
|
+
NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
27
|
+
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
30
|
+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
31
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
33
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
package com.adobe.net
|
37
|
+
{
|
38
|
+
import flash.utils.ByteArray;
|
39
|
+
|
40
|
+
/**
|
41
|
+
* This class implements an efficient lookup table for URI
|
42
|
+
* character escaping. This class is only needed if you
|
43
|
+
* create a derived class of URI to handle custom URI
|
44
|
+
* syntax. This class is used internally by URI.
|
45
|
+
*
|
46
|
+
* @langversion ActionScript 3.0
|
47
|
+
* @playerversion Flash 9.0*
|
48
|
+
*/
|
49
|
+
public class URIEncodingBitmap extends ByteArray
|
50
|
+
{
|
51
|
+
/**
|
52
|
+
* Constructor. Creates an encoding bitmap using the given
|
53
|
+
* string of characters as the set of characters that need
|
54
|
+
* to be URI escaped.
|
55
|
+
*
|
56
|
+
* @langversion ActionScript 3.0
|
57
|
+
* @playerversion Flash 9.0
|
58
|
+
*/
|
59
|
+
public function URIEncodingBitmap(charsToEscape:String) : void
|
60
|
+
{
|
61
|
+
var i:int;
|
62
|
+
var data:ByteArray = new ByteArray();
|
63
|
+
|
64
|
+
// Initialize our 128 bits (16 bytes) to zero
|
65
|
+
for (i = 0; i < 16; i++)
|
66
|
+
this.writeByte(0);
|
67
|
+
|
68
|
+
data.writeUTFBytes(charsToEscape);
|
69
|
+
data.position = 0;
|
70
|
+
|
71
|
+
while (data.bytesAvailable)
|
72
|
+
{
|
73
|
+
var c:int = data.readByte();
|
74
|
+
|
75
|
+
if (c > 0x7f)
|
76
|
+
continue; // only escape low bytes
|
77
|
+
|
78
|
+
var enc:int;
|
79
|
+
this.position = (c >> 3);
|
80
|
+
enc = this.readByte();
|
81
|
+
enc |= 1 << (c & 0x7);
|
82
|
+
this.position = (c >> 3);
|
83
|
+
this.writeByte(enc);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
/**
|
88
|
+
* Based on the data table contained in this object, check
|
89
|
+
* if the given character should be escaped.
|
90
|
+
*
|
91
|
+
* @param char the character to be escaped. Only the first
|
92
|
+
* character in the string is used. Any other characters
|
93
|
+
* are ignored.
|
94
|
+
*
|
95
|
+
* @return the integer value of the raw UTF8 character. For
|
96
|
+
* example, if '%' is given, the return value is 37 (0x25).
|
97
|
+
* If the character given does not need to be escaped, the
|
98
|
+
* return value is zero.
|
99
|
+
*
|
100
|
+
* @langversion ActionScript 3.0
|
101
|
+
* @playerversion Flash 9.0
|
102
|
+
*/
|
103
|
+
public function ShouldEscape(char:String) : int
|
104
|
+
{
|
105
|
+
var data:ByteArray = new ByteArray();
|
106
|
+
var c:int, mask:int;
|
107
|
+
|
108
|
+
// write the character into a ByteArray so
|
109
|
+
// we can pull it out as a raw byte value.
|
110
|
+
data.writeUTFBytes(char);
|
111
|
+
data.position = 0;
|
112
|
+
c = data.readByte();
|
113
|
+
|
114
|
+
if (c & 0x80)
|
115
|
+
{
|
116
|
+
// don't escape high byte characters. It can make international
|
117
|
+
// URI's unreadable. We just want to escape characters that would
|
118
|
+
// make URI syntax ambiguous.
|
119
|
+
return 0;
|
120
|
+
}
|
121
|
+
else if ((c < 0x1f) || (c == 0x7f))
|
122
|
+
{
|
123
|
+
// control characters must be escaped.
|
124
|
+
return c;
|
125
|
+
}
|
126
|
+
|
127
|
+
this.position = (c >> 3);
|
128
|
+
mask = this.readByte();
|
129
|
+
|
130
|
+
if (mask & (1 << (c & 0x7)))
|
131
|
+
{
|
132
|
+
// we need to escape this, return the numeric value
|
133
|
+
// of the character
|
134
|
+
return c;
|
135
|
+
}
|
136
|
+
else
|
137
|
+
{
|
138
|
+
return 0;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}
|
@@ -0,0 +1,204 @@
|
|
1
|
+
/*
|
2
|
+
Adobe Systems Incorporated(r) Source Code License Agreement
|
3
|
+
Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
4
|
+
|
5
|
+
Please read this Source Code License Agreement carefully before using
|
6
|
+
the source code.
|
7
|
+
|
8
|
+
Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
9
|
+
no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
10
|
+
prepare derivative works of, publicly display, publicly perform, and
|
11
|
+
distribute this source code and such derivative works in source or
|
12
|
+
object code form without any attribution requirements.
|
13
|
+
|
14
|
+
The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
15
|
+
derived from the source code without prior written permission.
|
16
|
+
|
17
|
+
You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
18
|
+
against any loss, damage, claims or lawsuits, including attorney's
|
19
|
+
fees that arise or result from your use or distribution of the source
|
20
|
+
code.
|
21
|
+
|
22
|
+
THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
23
|
+
ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
24
|
+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
25
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
26
|
+
NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
27
|
+
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
30
|
+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
31
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
33
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
package com.adobe.net.proxies
|
36
|
+
{
|
37
|
+
import flash.events.Event;
|
38
|
+
import flash.events.IOErrorEvent;
|
39
|
+
import flash.events.ProgressEvent;
|
40
|
+
import flash.net.Socket;
|
41
|
+
|
42
|
+
/**
|
43
|
+
* This class allows TCP socket connections through HTTP proxies in accordance with
|
44
|
+
* RFC 2817:
|
45
|
+
*
|
46
|
+
* ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt
|
47
|
+
*
|
48
|
+
* It can also be used to make direct connections to a destination, as well. If you
|
49
|
+
* pass the host and port into the constructor, no proxy will be used. You can also
|
50
|
+
* call connect, passing in the host and the port, and if you didn't set the proxy
|
51
|
+
* info, a direct connection will be made. A proxy is only used after you have called
|
52
|
+
* the setProxyInfo function.
|
53
|
+
*
|
54
|
+
* The connection to and negotiation with the proxy is completely hidden. All the
|
55
|
+
* same events are thrown whether you are using a proxy or not, and the data you
|
56
|
+
* receive from the target server will look exact as it would if you were connected
|
57
|
+
* to it directly rather than through a proxy.
|
58
|
+
*
|
59
|
+
* @author Christian Cantrell
|
60
|
+
*
|
61
|
+
**/
|
62
|
+
public class RFC2817Socket
|
63
|
+
extends Socket
|
64
|
+
{
|
65
|
+
private var proxyHost:String = null;
|
66
|
+
private var host:String = null;
|
67
|
+
private var proxyPort:int = 0;
|
68
|
+
private var port:int = 0;
|
69
|
+
private var deferredEventHandlers:Object = new Object();
|
70
|
+
private var buffer:String = new String();
|
71
|
+
|
72
|
+
/**
|
73
|
+
* Construct a new RFC2817Socket object. If you pass in the host and the port,
|
74
|
+
* no proxy will be used. If you want to use a proxy, instantiate with no
|
75
|
+
* arguments, call setProxyInfo, then call connect.
|
76
|
+
**/
|
77
|
+
public function RFC2817Socket(host:String = null, port:int = 0)
|
78
|
+
{
|
79
|
+
if (host != null && port != 0)
|
80
|
+
{
|
81
|
+
super(host, port);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Set the proxy host and port number. Your connection will only proxied if
|
87
|
+
* this function has been called.
|
88
|
+
**/
|
89
|
+
public function setProxyInfo(host:String, port:int):void
|
90
|
+
{
|
91
|
+
this.proxyHost = host;
|
92
|
+
this.proxyPort = port;
|
93
|
+
|
94
|
+
var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
|
95
|
+
var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];
|
96
|
+
|
97
|
+
if (deferredSocketDataHandler != null)
|
98
|
+
{
|
99
|
+
super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);
|
100
|
+
}
|
101
|
+
|
102
|
+
if (deferredConnectHandler != null)
|
103
|
+
{
|
104
|
+
super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Connect to the specified host over the specified port. If you want your
|
110
|
+
* connection proxied, call the setProxyInfo function first.
|
111
|
+
**/
|
112
|
+
public override function connect(host:String, port:int):void
|
113
|
+
{
|
114
|
+
if (this.proxyHost == null)
|
115
|
+
{
|
116
|
+
this.redirectConnectEvent();
|
117
|
+
this.redirectSocketDataEvent();
|
118
|
+
super.connect(host, port);
|
119
|
+
}
|
120
|
+
else
|
121
|
+
{
|
122
|
+
this.host = host;
|
123
|
+
this.port = port;
|
124
|
+
super.addEventListener(Event.CONNECT, this.onConnect);
|
125
|
+
super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
|
126
|
+
super.connect(this.proxyHost, this.proxyPort);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
private function onConnect(event:Event):void
|
131
|
+
{
|
132
|
+
this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");
|
133
|
+
this.flush();
|
134
|
+
this.redirectConnectEvent();
|
135
|
+
}
|
136
|
+
|
137
|
+
private function onSocketData(event:ProgressEvent):void
|
138
|
+
{
|
139
|
+
while (this.bytesAvailable != 0)
|
140
|
+
{
|
141
|
+
this.buffer += this.readUTFBytes(1);
|
142
|
+
if (this.buffer.search(/\r?\n\r?\n$/) != -1)
|
143
|
+
{
|
144
|
+
this.checkResponse(event);
|
145
|
+
break;
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
private function checkResponse(event:ProgressEvent):void
|
151
|
+
{
|
152
|
+
var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);
|
153
|
+
|
154
|
+
if (responseCode.search(/^2/) == -1)
|
155
|
+
{
|
156
|
+
var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);
|
157
|
+
ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer;
|
158
|
+
this.dispatchEvent(ioError);
|
159
|
+
}
|
160
|
+
else
|
161
|
+
{
|
162
|
+
this.redirectSocketDataEvent();
|
163
|
+
this.dispatchEvent(new Event(Event.CONNECT));
|
164
|
+
if (this.bytesAvailable > 0)
|
165
|
+
{
|
166
|
+
this.dispatchEvent(event);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
this.buffer = null;
|
170
|
+
}
|
171
|
+
|
172
|
+
private function redirectConnectEvent():void
|
173
|
+
{
|
174
|
+
super.removeEventListener(Event.CONNECT, onConnect);
|
175
|
+
var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];
|
176
|
+
if (deferredEventHandler != null)
|
177
|
+
{
|
178
|
+
super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
private function redirectSocketDataEvent():void
|
183
|
+
{
|
184
|
+
super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
185
|
+
var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
|
186
|
+
if (deferredEventHandler != null)
|
187
|
+
{
|
188
|
+
super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
|
189
|
+
}
|
190
|
+
}
|
191
|
+
|
192
|
+
public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void
|
193
|
+
{
|
194
|
+
if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)
|
195
|
+
{
|
196
|
+
this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};
|
197
|
+
}
|
198
|
+
else
|
199
|
+
{
|
200
|
+
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
|
201
|
+
}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
/*
|
2
|
+
Adobe Systems Incorporated(r) Source Code License Agreement
|
3
|
+
Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
4
|
+
|
5
|
+
Please read this Source Code License Agreement carefully before using
|
6
|
+
the source code.
|
7
|
+
|
8
|
+
Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
9
|
+
no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
10
|
+
prepare derivative works of, publicly display, publicly perform, and
|
11
|
+
distribute this source code and such derivative works in source or
|
12
|
+
object code form without any attribution requirements.
|
13
|
+
|
14
|
+
The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
15
|
+
derived from the source code without prior written permission.
|
16
|
+
|
17
|
+
You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
18
|
+
against any loss, damage, claims or lawsuits, including attorney's
|
19
|
+
fees that arise or result from your use or distribution of the source
|
20
|
+
code.
|
21
|
+
|
22
|
+
THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
23
|
+
ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
24
|
+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
25
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
26
|
+
NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
27
|
+
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
30
|
+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
31
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
33
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
package com.adobe.serialization.json {
|
37
|
+
|
38
|
+
/**
|
39
|
+
* This class provides encoding and decoding of the JSON format.
|
40
|
+
*
|
41
|
+
* Example usage:
|
42
|
+
* <code>
|
43
|
+
* // create a JSON string from an internal object
|
44
|
+
* JSON.encode( myObject );
|
45
|
+
*
|
46
|
+
* // read a JSON string into an internal object
|
47
|
+
* var myObject:Object = JSON.decode( jsonString );
|
48
|
+
* </code>
|
49
|
+
*/
|
50
|
+
public class JSON {
|
51
|
+
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Encodes a object into a JSON string.
|
55
|
+
*
|
56
|
+
* @param o The object to create a JSON string for
|
57
|
+
* @return the JSON string representing o
|
58
|
+
* @langversion ActionScript 3.0
|
59
|
+
* @playerversion Flash 9.0
|
60
|
+
* @tiptext
|
61
|
+
*/
|
62
|
+
public static function encode( o:Object ):String {
|
63
|
+
|
64
|
+
var encoder:JSONEncoder = new JSONEncoder( o );
|
65
|
+
return encoder.getString();
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Decodes a JSON string into a native object.
|
71
|
+
*
|
72
|
+
* @param s The JSON string representing the object
|
73
|
+
* @return A native object as specified by s
|
74
|
+
* @throw JSONParseError
|
75
|
+
* @langversion ActionScript 3.0
|
76
|
+
* @playerversion Flash 9.0
|
77
|
+
* @tiptext
|
78
|
+
*/
|
79
|
+
public static function decode( s:String ):* {
|
80
|
+
|
81
|
+
var decoder:JSONDecoder = new JSONDecoder( s )
|
82
|
+
return decoder.getValue();
|
83
|
+
|
84
|
+
}
|
85
|
+
|
86
|
+
}
|
87
|
+
|
88
|
+
}
|
@@ -0,0 +1,218 @@
|
|
1
|
+
/*
|
2
|
+
Adobe Systems Incorporated(r) Source Code License Agreement
|
3
|
+
Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
4
|
+
|
5
|
+
Please read this Source Code License Agreement carefully before using
|
6
|
+
the source code.
|
7
|
+
|
8
|
+
Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
9
|
+
no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
10
|
+
prepare derivative works of, publicly display, publicly perform, and
|
11
|
+
distribute this source code and such derivative works in source or
|
12
|
+
object code form without any attribution requirements.
|
13
|
+
|
14
|
+
The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
15
|
+
derived from the source code without prior written permission.
|
16
|
+
|
17
|
+
You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
18
|
+
against any loss, damage, claims or lawsuits, including attorney's
|
19
|
+
fees that arise or result from your use or distribution of the source
|
20
|
+
code.
|
21
|
+
|
22
|
+
THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
23
|
+
ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
24
|
+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
25
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
26
|
+
NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
27
|
+
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
30
|
+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
31
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
33
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
package com.adobe.serialization.json {
|
37
|
+
|
38
|
+
public class JSONDecoder {
|
39
|
+
|
40
|
+
/** The value that will get parsed from the JSON string */
|
41
|
+
private var value:*;
|
42
|
+
|
43
|
+
/** The tokenizer designated to read the JSON string */
|
44
|
+
private var tokenizer:JSONTokenizer;
|
45
|
+
|
46
|
+
/** The current token from the tokenizer */
|
47
|
+
private var token:JSONToken;
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Constructs a new JSONDecoder to parse a JSON string
|
51
|
+
* into a native object.
|
52
|
+
*
|
53
|
+
* @param s The JSON string to be converted
|
54
|
+
* into a native object
|
55
|
+
* @langversion ActionScript 3.0
|
56
|
+
* @playerversion Flash 9.0
|
57
|
+
* @tiptext
|
58
|
+
*/
|
59
|
+
public function JSONDecoder( s:String ) {
|
60
|
+
|
61
|
+
tokenizer = new JSONTokenizer( s );
|
62
|
+
|
63
|
+
nextToken();
|
64
|
+
value = parseValue();
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Gets the internal object that was created by parsing
|
69
|
+
* the JSON string passed to the constructor.
|
70
|
+
*
|
71
|
+
* @return The internal object representation of the JSON
|
72
|
+
* string that was passed to the constructor
|
73
|
+
* @langversion ActionScript 3.0
|
74
|
+
* @playerversion Flash 9.0
|
75
|
+
* @tiptext
|
76
|
+
*/
|
77
|
+
public function getValue():* {
|
78
|
+
return value;
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Returns the next token from the tokenzier reading
|
83
|
+
* the JSON string
|
84
|
+
*/
|
85
|
+
private function nextToken():JSONToken {
|
86
|
+
return token = tokenizer.getNextToken();
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Attempt to parse an array
|
91
|
+
*/
|
92
|
+
private function parseArray():Array {
|
93
|
+
// create an array internally that we're going to attempt
|
94
|
+
// to parse from the tokenizer
|
95
|
+
var a:Array = new Array();
|
96
|
+
|
97
|
+
// grab the next token from the tokenizer to move
|
98
|
+
// past the opening [
|
99
|
+
nextToken();
|
100
|
+
|
101
|
+
// check to see if we have an empty array
|
102
|
+
if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
|
103
|
+
// we're done reading the array, so return it
|
104
|
+
return a;
|
105
|
+
}
|
106
|
+
|
107
|
+
// deal with elements of the array, and use an "infinite"
|
108
|
+
// loop because we could have any amount of elements
|
109
|
+
while ( true ) {
|
110
|
+
// read in the value and add it to the array
|
111
|
+
a.push ( parseValue() );
|
112
|
+
|
113
|
+
// after the value there should be a ] or a ,
|
114
|
+
nextToken();
|
115
|
+
|
116
|
+
if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
|
117
|
+
// we're done reading the array, so return it
|
118
|
+
return a;
|
119
|
+
} else if ( token.type == JSONTokenType.COMMA ) {
|
120
|
+
// move past the comma and read another value
|
121
|
+
nextToken();
|
122
|
+
} else {
|
123
|
+
tokenizer.parseError( "Expecting ] or , but found " + token.value );
|
124
|
+
}
|
125
|
+
}
|
126
|
+
return null;
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* Attempt to parse an object
|
131
|
+
*/
|
132
|
+
private function parseObject():Object {
|
133
|
+
// create the object internally that we're going to
|
134
|
+
// attempt to parse from the tokenizer
|
135
|
+
var o:Object = new Object();
|
136
|
+
|
137
|
+
// store the string part of an object member so
|
138
|
+
// that we can assign it a value in the object
|
139
|
+
var key:String
|
140
|
+
|
141
|
+
// grab the next token from the tokenizer
|
142
|
+
nextToken();
|
143
|
+
|
144
|
+
// check to see if we have an empty object
|
145
|
+
if ( token.type == JSONTokenType.RIGHT_BRACE ) {
|
146
|
+
// we're done reading the object, so return it
|
147
|
+
return o;
|
148
|
+
}
|
149
|
+
|
150
|
+
// deal with members of the object, and use an "infinite"
|
151
|
+
// loop because we could have any amount of members
|
152
|
+
while ( true ) {
|
153
|
+
|
154
|
+
if ( token.type == JSONTokenType.STRING ) {
|
155
|
+
// the string value we read is the key for the object
|
156
|
+
key = String( token.value );
|
157
|
+
|
158
|
+
// move past the string to see what's next
|
159
|
+
nextToken();
|
160
|
+
|
161
|
+
// after the string there should be a :
|
162
|
+
if ( token.type == JSONTokenType.COLON ) {
|
163
|
+
|
164
|
+
// move past the : and read/assign a value for the key
|
165
|
+
nextToken();
|
166
|
+
o[key] = parseValue();
|
167
|
+
|
168
|
+
// move past the value to see what's next
|
169
|
+
nextToken();
|
170
|
+
|
171
|
+
// after the value there's either a } or a ,
|
172
|
+
if ( token.type == JSONTokenType.RIGHT_BRACE ) {
|
173
|
+
// // we're done reading the object, so return it
|
174
|
+
return o;
|
175
|
+
|
176
|
+
} else if ( token.type == JSONTokenType.COMMA ) {
|
177
|
+
// skip past the comma and read another member
|
178
|
+
nextToken();
|
179
|
+
} else {
|
180
|
+
tokenizer.parseError( "Expecting } or , but found " + token.value );
|
181
|
+
}
|
182
|
+
} else {
|
183
|
+
tokenizer.parseError( "Expecting : but found " + token.value );
|
184
|
+
}
|
185
|
+
} else {
|
186
|
+
tokenizer.parseError( "Expecting string but found " + token.value );
|
187
|
+
}
|
188
|
+
}
|
189
|
+
return null;
|
190
|
+
}
|
191
|
+
|
192
|
+
/**
|
193
|
+
* Attempt to parse a value
|
194
|
+
*/
|
195
|
+
private function parseValue():Object {
|
196
|
+
|
197
|
+
switch ( token.type ) {
|
198
|
+
case JSONTokenType.LEFT_BRACE:
|
199
|
+
return parseObject();
|
200
|
+
|
201
|
+
case JSONTokenType.LEFT_BRACKET:
|
202
|
+
return parseArray();
|
203
|
+
|
204
|
+
case JSONTokenType.STRING:
|
205
|
+
case JSONTokenType.NUMBER:
|
206
|
+
case JSONTokenType.TRUE:
|
207
|
+
case JSONTokenType.FALSE:
|
208
|
+
case JSONTokenType.NULL:
|
209
|
+
return token.value;
|
210
|
+
|
211
|
+
default:
|
212
|
+
tokenizer.parseError( "Unexpected " + token.value );
|
213
|
+
|
214
|
+
}
|
215
|
+
return null;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
}
|