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
|
package com.adobe.protocols.dict
{
import com.adobe.protocols.dict.events.*;
import com.adobe.protocols.dict.util.*;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import mx.rpc.http.HTTPService;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import flash.xml.XMLNode;
import mx.utils.StringUtil;
public class Dict
extends EventDispatcher
{
// Event type names.
public static var CONNECTED:String = "connected";
public static var DISCONNECTED:String = "disconnected";
public static var IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static var ERROR:String = "error";
public static var SERVERS:String = "servers";
public static var DATABASES:String = "databases";
public static var MATCH_STRATEGIES:String = "matchStrategies";
public static var DEFINITION:String = "definition";
public static var DEFINITION_HEADER:String = "definitionHeader";
public static var MATCH:String = "match";
public static var NO_MATCH:String = "noMatch";
public static var FIRST_MATCH:uint = 0;
public static var ALL_DATABASES:uint = 1;
private var socket:SocketHelper;
private var dbShortList:Boolean;
public function Dict()
{
this.socket = new SocketHelper();
this.socket.addEventListener(Event.CONNECT, connected);
this.socket.addEventListener(Event.CLOSE, disconnected);
this.socket.addEventListener(SocketHelper.COMPLETE_RESPONSE, incomingData);
this.socket.addEventListener(IOErrorEvent.IO_ERROR, ioError);
this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError);
}
public function connect(server:String, port:uint = 2628):void
{
if (this.socket.connected)
{
this.socket.close();
}
this.socket.connect(server, port);
}
public function connectThroughProxy(proxyServer:String,
proxyPort:int,
server:String,
port:uint = 2628):void
{
if (this.socket.connected)
{
this.socket.close();
}
this.socket.setProxyInfo(proxyServer, proxyPort);
this.socket.connect(server, port);
}
public function disconnect():void
{
this.socket.close();
this.disconnected(null);
}
public function getServers():void
{
var http:HTTPService = new HTTPService();
http.url = "http://luetzschena-stahmeln.de/dictd/xmllist.php";
http.addEventListener(ResultEvent.RESULT, incomingServerXML);
http.addEventListener(FaultEvent.FAULT, httpError);
http.resultFormat = HTTPService.RESULT_FORMAT_E4X;
http.send();
}
public function getDatabases(shortList:Boolean=true):void
{
this.dbShortList = shortList;
this.socket.writeUTFBytes("show db\r\n");
this.socket.flush();
}
public function getMatchStrategies():void
{
this.socket.writeUTFBytes("show strat\r\n");
this.socket.flush();
}
public function match(database:String, term:String, scope:String="prefix"):void
{
this.socket.writeUTFBytes("match " + database + " " + scope + " \"" + term + "\"\r\n");
this.socket.flush();
}
public function define(database:String, term:String):void
{
this.socket.writeUTFBytes("define " + database + " \"" + term + "\"\r\n");
this.socket.flush();
}
public function lookup(term:String, scope:uint):void
{
var flag:String;
if (scope == Dict.ALL_DATABASES)
{
flag = "*";
}
else if (scope == Dict.FIRST_MATCH)
{
flag = "!";
}
this.socket.writeUTFBytes("define " + flag + " \"" + term + "\"\r\n");
this.socket.flush();
}
//// Private functions ////
private function connected(event:Event):void
{
// Wait to dispatch an event until we get the 220 response.
}
private function disconnected(event:Event):void
{
dispatchEvent(new DisconnectedEvent());
}
private function incomingServerXML(event:ResultEvent):void
{
var dictd:Namespace = new Namespace("http://www.luetzschena-stahmeln.de/dictd/");
var result:XML = event.result as XML;
var server:String, description:String;
var servers:Array = new Array();
for each (var serverNode:XML in result.dictd::server)
{
server = serverNode.dictd::dictdurl;
description = serverNode.dictd::description;
if (StringUtil.trim(server).length != 0 &&
StringUtil.trim(description).length != 0)
{
var dServer:DictionaryServer = new DictionaryServer();
dServer.server = server.replace("dict://", "");
dServer.description = description;
servers.push(dServer);
}
}
var dEvent:DictionaryServerEvent = new DictionaryServerEvent();
dEvent.servers = servers;
dispatchEvent(dEvent);
}
private function incomingData(event:CompleteResponseEvent):void
{
var rawResponse:String = event.response;
var response:Response = this.parseRawResponse(rawResponse);
var responseCode:uint = response.code;
if (responseCode == 552) // no matches
{
throwNoMatchEvent(response);
}
else if (responseCode >= 400 && responseCode <= 599) // error
{
throwErrorEvent(response);
}
else if (responseCode == 220) // successful connection
{
dispatchEvent(new ConnectedEvent());
}
else if (responseCode == 110) // databases are being returned
{
throwDatabasesEvent(response);
}
else if (responseCode == 111) // matches strategies
{
throwMatchStrategiesEvent(response);
}
else if (responseCode == 152) // matches
{
throwMatchEvent(response);
}
else if (responseCode == 150)
{
throwDefinitionHeaderEvent(response);
}
else if (responseCode == 151)
{
throwDefinitionEvent(response);
}
}
private function ioError(event:IOErrorEvent):void
{
dispatchEvent(event);
}
private function httpError(event:FaultEvent):void
{
trace("httpError!");
}
private function securityError(event:SecurityErrorEvent):void
{
trace("security error!");
trace(event.text);
}
// Dispatch new events.
private function throwDatabasesEvent(response:Response):void
{
var databases:Array = new Array();
var responseArray:Array = response.body.split("\r\n");
for each (var line:String in responseArray)
{
var name:String = line.substring(0, line.indexOf(" "));
if (name == "--exit--")
{
if (this.dbShortList)
{
break;
}
continue;
}
var description:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
databases.push(new Database(name, description));
}
var event:DatabaseEvent = new DatabaseEvent();
event.databases = databases;
dispatchEvent(event);
}
private function throwMatchStrategiesEvent(response:Response):void
{
var strategies:Array = new Array();
var responseArray:Array = response.body.split("\r\n");
for each (var line:String in responseArray)
{
var name:String = line.substring(0, line.indexOf(" "));
var description:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
strategies.push(new MatchStrategy(name, description));
}
var event:MatchStrategiesEvent = new MatchStrategiesEvent();
event.strategies = strategies;
dispatchEvent(event);
}
private function throwMatchEvent(response:Response):void
{
var matches:Array = new Array();
var responseArray:Array = response.body.split("\r\n");
for each (var line:String in responseArray)
{
var match:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
matches.push(match);
}
var event:MatchEvent = new MatchEvent();
event.matches = matches;
dispatchEvent(event);
}
private function throwErrorEvent(response:Response):void
{
var event:ErrorEvent = new ErrorEvent();
event.code = response.code;
event.message = response.headerText;
dispatchEvent(event);
}
private function throwNoMatchEvent(response:Response):void
{
dispatchEvent(new NoMatchEvent());
}
private function throwDefinitionHeaderEvent(response:Response):void
{
var event:DefinitionHeaderEvent = new DefinitionHeaderEvent();
event.definitionCount = uint(response.headerText.substring(0, response.headerText.indexOf(" ")));
dispatchEvent(event);
}
private function throwDefinitionEvent(response:Response):void
{
var event:DefinitionEvent = new DefinitionEvent();
var def:Definition = new Definition();
var headerText:String = response.headerText;
var tokens:Array = headerText.match(/"[^"]+"/g);
def.term = String(tokens[0]).replace(/"/g, "");
def.database = String(tokens[1]).replace(/"/g, "");
def.definition = response.body;
event.definition = def;
dispatchEvent(event);
}
private function parseRawResponse(rawResponse:String):Response
{
var response:Response = new Response();
var fullHeader:String;
if (rawResponse.indexOf("\r\n") != -1)
{
fullHeader = rawResponse.substring(0, rawResponse.indexOf("\r\n"));
}
else
{
fullHeader = rawResponse;
}
var responseCodeMatch:Array = fullHeader.match(/^\d{3}/);
response.code = uint(responseCodeMatch[0]);
response.headerText = fullHeader.substring(fullHeader.indexOf(" ")+1, fullHeader.length);
var body:String = rawResponse.substring(rawResponse.indexOf("\r\n")+2, rawResponse.length);
body = body.replace(/\r\n\.\./, "\r\n.");
response.body = body;
return response;
}
}
}
|