annotate bindings/as3/ext/com/adobe/protocols/dict/Dict.as @ 770:c54bc2ffbf92 tip

update tags
author convert-repo
date Fri, 16 Dec 2011 11:34:01 +0000
parents 3a0b9700b3d2
children
rev   line source
mas01mj@732 1 /*
mas01mj@732 2 Copyright (c) 2009, Adobe Systems Incorporated
mas01mj@732 3 All rights reserved.
mas01mj@732 4
mas01mj@732 5 Redistribution and use in source and binary forms, with or without
mas01mj@732 6 modification, are permitted provided that the following conditions are
mas01mj@732 7 met:
mas01mj@732 8
mas01mj@732 9 * Redistributions of source code must retain the above copyright notice,
mas01mj@732 10 this list of conditions and the following disclaimer.
mas01mj@732 11
mas01mj@732 12 * Redistributions in binary form must reproduce the above copyright
mas01mj@732 13 notice, this list of conditions and the following disclaimer in the
mas01mj@732 14 documentation and/or other materials provided with the distribution.
mas01mj@732 15
mas01mj@732 16 * Neither the name of Adobe Systems Incorporated nor the names of its
mas01mj@732 17 contributors may be used to endorse or promote products derived from
mas01mj@732 18 this software without specific prior written permission.
mas01mj@732 19
mas01mj@732 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
mas01mj@732 21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mas01mj@732 23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
mas01mj@732 24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mas01mj@732 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
mas01mj@732 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
mas01mj@732 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
mas01mj@732 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mas01mj@732 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mas01mj@732 31 */
mas01mj@732 32
mas01mj@732 33 package com.adobe.protocols.dict
mas01mj@732 34 {
mas01mj@732 35 import com.adobe.protocols.dict.events.*;
mas01mj@732 36 import com.adobe.protocols.dict.util.*;
mas01mj@732 37
mas01mj@732 38 import flash.events.Event;
mas01mj@732 39 import flash.events.EventDispatcher;
mas01mj@732 40 import flash.events.IOErrorEvent;
mas01mj@732 41 import flash.events.ProgressEvent;
mas01mj@732 42 import flash.events.SecurityErrorEvent;
mas01mj@732 43 import flash.net.Socket;
mas01mj@732 44 import mx.rpc.http.HTTPService;
mas01mj@732 45 import mx.rpc.events.ResultEvent;
mas01mj@732 46 import mx.rpc.events.FaultEvent;
mas01mj@732 47 import flash.xml.XMLNode;
mas01mj@732 48 import mx.utils.StringUtil;
mas01mj@732 49
mas01mj@732 50 public class Dict
mas01mj@732 51 extends EventDispatcher
mas01mj@732 52 {
mas01mj@732 53 // Event type names.
mas01mj@732 54 //public static var CONNECTED:String = "connected";
mas01mj@732 55 //public static var DISCONNECTED:String = "disconnected";
mas01mj@732 56 public static var IO_ERROR:String = IOErrorEvent.IO_ERROR;
mas01mj@732 57 //public static var ERROR:String = "error";
mas01mj@732 58 //public static var SERVERS:String = "servers";
mas01mj@732 59 //public static var DATABASES:String = "databases";
mas01mj@732 60 //public static var MATCH_STRATEGIES:String = "matchStrategies";
mas01mj@732 61 //public static var DEFINITION:String = "definition";
mas01mj@732 62 //public static var DEFINITION_HEADER:String = "definitionHeader";
mas01mj@732 63 //public static var MATCH:String = "match";
mas01mj@732 64 //public static var NO_MATCH:String = "noMatch";
mas01mj@732 65
mas01mj@732 66 public static var FIRST_MATCH:uint = 0;
mas01mj@732 67 public static var ALL_DATABASES:uint = 1;
mas01mj@732 68
mas01mj@732 69 private var socket:SocketHelper;
mas01mj@732 70
mas01mj@732 71 private var dbShortList:Boolean;
mas01mj@732 72
mas01mj@732 73 public function Dict()
mas01mj@732 74 {
mas01mj@732 75 this.socket = new SocketHelper();
mas01mj@732 76 this.socket.addEventListener(Event.CONNECT, connected);
mas01mj@732 77 this.socket.addEventListener(Event.CLOSE, disconnected);
mas01mj@732 78 this.socket.addEventListener(SocketHelper.COMPLETE_RESPONSE, incomingData);
mas01mj@732 79 this.socket.addEventListener(IOErrorEvent.IO_ERROR, ioError);
mas01mj@732 80 this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError);
mas01mj@732 81 }
mas01mj@732 82
mas01mj@732 83 public function connect(server:String, port:uint = 2628):void
mas01mj@732 84 {
mas01mj@732 85 if (this.socket.connected)
mas01mj@732 86 {
mas01mj@732 87 this.socket.close();
mas01mj@732 88 }
mas01mj@732 89 this.socket.connect(server, port);
mas01mj@732 90 }
mas01mj@732 91
mas01mj@732 92 public function connectThroughProxy(proxyServer:String,
mas01mj@732 93 proxyPort:int,
mas01mj@732 94 server:String,
mas01mj@732 95 port:uint = 2628):void
mas01mj@732 96 {
mas01mj@732 97 if (this.socket.connected)
mas01mj@732 98 {
mas01mj@732 99 this.socket.close();
mas01mj@732 100 }
mas01mj@732 101 this.socket.setProxyInfo(proxyServer, proxyPort);
mas01mj@732 102 this.socket.connect(server, port);
mas01mj@732 103 }
mas01mj@732 104
mas01mj@732 105 public function disconnect():void
mas01mj@732 106 {
mas01mj@732 107 this.socket.close();
mas01mj@732 108 this.disconnected(null);
mas01mj@732 109 }
mas01mj@732 110
mas01mj@732 111 public function getServers():void
mas01mj@732 112 {
mas01mj@732 113 var http:HTTPService = new HTTPService();
mas01mj@732 114 http.url = "http://luetzschena-stahmeln.de/dictd/xmllist.php";
mas01mj@732 115 http.addEventListener(ResultEvent.RESULT, incomingServerXML);
mas01mj@732 116 http.addEventListener(FaultEvent.FAULT, httpError);
mas01mj@732 117 http.resultFormat = HTTPService.RESULT_FORMAT_E4X;
mas01mj@732 118 http.send();
mas01mj@732 119 }
mas01mj@732 120
mas01mj@732 121 public function getDatabases(shortList:Boolean=true):void
mas01mj@732 122 {
mas01mj@732 123 this.dbShortList = shortList;
mas01mj@732 124 this.socket.writeUTFBytes("show db\r\n");
mas01mj@732 125 this.socket.flush();
mas01mj@732 126 }
mas01mj@732 127
mas01mj@732 128 public function getMatchStrategies():void
mas01mj@732 129 {
mas01mj@732 130 this.socket.writeUTFBytes("show strat\r\n");
mas01mj@732 131 this.socket.flush();
mas01mj@732 132 }
mas01mj@732 133
mas01mj@732 134 public function match(database:String, term:String, scope:String="prefix"):void
mas01mj@732 135 {
mas01mj@732 136 this.socket.writeUTFBytes("match " + database + " " + scope + " \"" + term + "\"\r\n");
mas01mj@732 137 this.socket.flush();
mas01mj@732 138 }
mas01mj@732 139
mas01mj@732 140 public function define(database:String, term:String):void
mas01mj@732 141 {
mas01mj@732 142 this.socket.writeUTFBytes("define " + database + " \"" + term + "\"\r\n");
mas01mj@732 143 this.socket.flush();
mas01mj@732 144 }
mas01mj@732 145
mas01mj@732 146 public function lookup(term:String, scope:uint):void
mas01mj@732 147 {
mas01mj@732 148 var flag:String;
mas01mj@732 149 if (scope == Dict.ALL_DATABASES)
mas01mj@732 150 {
mas01mj@732 151 flag = "*";
mas01mj@732 152 }
mas01mj@732 153 else if (scope == Dict.FIRST_MATCH)
mas01mj@732 154 {
mas01mj@732 155 flag = "!";
mas01mj@732 156 }
mas01mj@732 157 this.socket.writeUTFBytes("define " + flag + " \"" + term + "\"\r\n");
mas01mj@732 158 this.socket.flush();
mas01mj@732 159 }
mas01mj@732 160
mas01mj@732 161 //// Private functions ////
mas01mj@732 162
mas01mj@732 163 private function connected(event:Event):void
mas01mj@732 164 {
mas01mj@732 165 // Wait to dispatch an event until we get the 220 response.
mas01mj@732 166 }
mas01mj@732 167
mas01mj@732 168 private function disconnected(event:Event):void
mas01mj@732 169 {
mas01mj@732 170 dispatchEvent(new DisconnectedEvent(DisconnectedEvent.DISCONNECTED));
mas01mj@732 171 }
mas01mj@732 172
mas01mj@732 173 private function incomingServerXML(event:ResultEvent):void
mas01mj@732 174 {
mas01mj@732 175 var dictd:Namespace = new Namespace("http://www.luetzschena-stahmeln.de/dictd/");
mas01mj@732 176 var result:XML = event.result as XML;
mas01mj@732 177 var server:String, description:String;
mas01mj@732 178 var servers:Array = new Array();
mas01mj@732 179 for each (var serverNode:XML in result.dictd::server)
mas01mj@732 180 {
mas01mj@732 181 server = serverNode.dictd::dictdurl;
mas01mj@732 182 description = serverNode.dictd::description;
mas01mj@732 183 if (StringUtil.trim(server).length != 0 &&
mas01mj@732 184 StringUtil.trim(description).length != 0)
mas01mj@732 185 {
mas01mj@732 186 var dServer:DictionaryServer = new DictionaryServer();
mas01mj@732 187 dServer.server = server.replace("dict://", "");
mas01mj@732 188 dServer.description = description;
mas01mj@732 189 servers.push(dServer);
mas01mj@732 190 }
mas01mj@732 191 }
mas01mj@732 192 var dEvent:DictionaryServerEvent = new DictionaryServerEvent(DictionaryServerEvent.SERVERS);
mas01mj@732 193 dEvent.servers = servers;
mas01mj@732 194 dispatchEvent(dEvent);
mas01mj@732 195 }
mas01mj@732 196
mas01mj@732 197 private function incomingData(event:CompleteResponseEvent):void
mas01mj@732 198 {
mas01mj@732 199 var rawResponse:String = event.response;
mas01mj@732 200 var response:Response = this.parseRawResponse(rawResponse);
mas01mj@732 201 var responseCode:uint = response.code;
mas01mj@732 202 if (responseCode == 552) // no matches
mas01mj@732 203 {
mas01mj@732 204 throwNoMatchEvent(response);
mas01mj@732 205 }
mas01mj@732 206 else if (responseCode >= 400 && responseCode <= 599) // error
mas01mj@732 207 {
mas01mj@732 208 throwErrorEvent(response);
mas01mj@732 209 }
mas01mj@732 210 else if (responseCode == 220) // successful connection
mas01mj@732 211 {
mas01mj@732 212 dispatchEvent(new ConnectedEvent(ConnectedEvent.CONNECTED));
mas01mj@732 213 }
mas01mj@732 214 else if (responseCode == 110) // databases are being returned
mas01mj@732 215 {
mas01mj@732 216 throwDatabasesEvent(response);
mas01mj@732 217 }
mas01mj@732 218 else if (responseCode == 111) // matches strategies
mas01mj@732 219 {
mas01mj@732 220 throwMatchStrategiesEvent(response);
mas01mj@732 221 }
mas01mj@732 222 else if (responseCode == 152) // matches
mas01mj@732 223 {
mas01mj@732 224 throwMatchEvent(response);
mas01mj@732 225 }
mas01mj@732 226 else if (responseCode == 150)
mas01mj@732 227 {
mas01mj@732 228 throwDefinitionHeaderEvent(response);
mas01mj@732 229 }
mas01mj@732 230 else if (responseCode == 151)
mas01mj@732 231 {
mas01mj@732 232 throwDefinitionEvent(response);
mas01mj@732 233 }
mas01mj@732 234 }
mas01mj@732 235
mas01mj@732 236 private function ioError(event:IOErrorEvent):void
mas01mj@732 237 {
mas01mj@732 238 dispatchEvent(event);
mas01mj@732 239 }
mas01mj@732 240
mas01mj@732 241 private function httpError(event:FaultEvent):void
mas01mj@732 242 {
mas01mj@732 243 trace("httpError!");
mas01mj@732 244 }
mas01mj@732 245
mas01mj@732 246 private function securityError(event:SecurityErrorEvent):void
mas01mj@732 247 {
mas01mj@732 248 trace("security error!");
mas01mj@732 249 trace(event.text);
mas01mj@732 250 }
mas01mj@732 251
mas01mj@732 252 // Dispatch new events.
mas01mj@732 253
mas01mj@732 254 private function throwDatabasesEvent(response:Response):void
mas01mj@732 255 {
mas01mj@732 256 var databases:Array = new Array();
mas01mj@732 257 var responseArray:Array = response.body.split("\r\n");
mas01mj@732 258 for each (var line:String in responseArray)
mas01mj@732 259 {
mas01mj@732 260 var name:String = line.substring(0, line.indexOf(" "));
mas01mj@732 261 if (name == "--exit--")
mas01mj@732 262 {
mas01mj@732 263 if (this.dbShortList)
mas01mj@732 264 {
mas01mj@732 265 break;
mas01mj@732 266 }
mas01mj@732 267 continue;
mas01mj@732 268 }
mas01mj@732 269 var description:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
mas01mj@732 270 databases.push(new Database(name, description));
mas01mj@732 271 }
mas01mj@732 272 var event:DatabaseEvent = new DatabaseEvent(DatabaseEvent.DATABASES);
mas01mj@732 273 event.databases = databases;
mas01mj@732 274 dispatchEvent(event);
mas01mj@732 275 }
mas01mj@732 276
mas01mj@732 277 private function throwMatchStrategiesEvent(response:Response):void
mas01mj@732 278 {
mas01mj@732 279 var strategies:Array = new Array();
mas01mj@732 280 var responseArray:Array = response.body.split("\r\n");
mas01mj@732 281 for each (var line:String in responseArray)
mas01mj@732 282 {
mas01mj@732 283 var name:String = line.substring(0, line.indexOf(" "));
mas01mj@732 284 var description:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
mas01mj@732 285 strategies.push(new MatchStrategy(name, description));
mas01mj@732 286 }
mas01mj@732 287 var event:MatchStrategiesEvent = new MatchStrategiesEvent(MatchStrategiesEvent.MATCH_STRATEGIES);
mas01mj@732 288 event.strategies = strategies;
mas01mj@732 289 dispatchEvent(event);
mas01mj@732 290 }
mas01mj@732 291
mas01mj@732 292 private function throwMatchEvent(response:Response):void
mas01mj@732 293 {
mas01mj@732 294 var matches:Array = new Array();
mas01mj@732 295 var responseArray:Array = response.body.split("\r\n");
mas01mj@732 296 for each (var line:String in responseArray)
mas01mj@732 297 {
mas01mj@732 298 var match:String = line.substring(line.indexOf(" ")+1, line.length).replace(/\"/g,"");
mas01mj@732 299 matches.push(match);
mas01mj@732 300 }
mas01mj@732 301 var event:MatchEvent = new MatchEvent(MatchEvent.MATCH);
mas01mj@732 302 event.matches = matches;
mas01mj@732 303 dispatchEvent(event);
mas01mj@732 304 }
mas01mj@732 305
mas01mj@732 306 private function throwErrorEvent(response:Response):void
mas01mj@732 307 {
mas01mj@732 308 var event:ErrorEvent = new ErrorEvent(ErrorEvent.ERROR);
mas01mj@732 309 event.code = response.code;
mas01mj@732 310 event.message = response.headerText;
mas01mj@732 311 dispatchEvent(event);
mas01mj@732 312 }
mas01mj@732 313
mas01mj@732 314 private function throwNoMatchEvent(response:Response):void
mas01mj@732 315 {
mas01mj@732 316 dispatchEvent(new NoMatchEvent(NoMatchEvent.NO_MATCH));
mas01mj@732 317 }
mas01mj@732 318
mas01mj@732 319 private function throwDefinitionHeaderEvent(response:Response):void
mas01mj@732 320 {
mas01mj@732 321 var event:DefinitionHeaderEvent = new DefinitionHeaderEvent(DefinitionHeaderEvent.DEFINITION_HEADER);
mas01mj@732 322 event.definitionCount = uint(response.headerText.substring(0, response.headerText.indexOf(" ")));
mas01mj@732 323 dispatchEvent(event);
mas01mj@732 324 }
mas01mj@732 325
mas01mj@732 326 private function throwDefinitionEvent(response:Response):void
mas01mj@732 327 {
mas01mj@732 328 var event:DefinitionEvent = new DefinitionEvent(DefinitionEvent.DEFINITION);
mas01mj@732 329 var def:Definition = new Definition();
mas01mj@732 330 var headerText:String = response.headerText;
mas01mj@732 331 var tokens:Array = headerText.match(/"[^"]+"/g);
mas01mj@732 332 def.term = String(tokens[0]).replace(/"/g, "");
mas01mj@732 333 def.database = String(tokens[1]).replace(/"/g, "");
mas01mj@732 334 def.definition = response.body;
mas01mj@732 335 event.definition = def;
mas01mj@732 336 dispatchEvent(event);
mas01mj@732 337 }
mas01mj@732 338
mas01mj@732 339 private function parseRawResponse(rawResponse:String):Response
mas01mj@732 340 {
mas01mj@732 341 var response:Response = new Response();
mas01mj@732 342 var fullHeader:String;
mas01mj@732 343 if (rawResponse.indexOf("\r\n") != -1)
mas01mj@732 344 {
mas01mj@732 345 fullHeader = rawResponse.substring(0, rawResponse.indexOf("\r\n"));
mas01mj@732 346 }
mas01mj@732 347 else
mas01mj@732 348 {
mas01mj@732 349 fullHeader = rawResponse;
mas01mj@732 350 }
mas01mj@732 351 var responseCodeMatch:Array = fullHeader.match(/^\d{3}/);
mas01mj@732 352 response.code = uint(responseCodeMatch[0]);
mas01mj@732 353 response.headerText = fullHeader.substring(fullHeader.indexOf(" ")+1, fullHeader.length);
mas01mj@732 354 var body:String = rawResponse.substring(rawResponse.indexOf("\r\n")+2, rawResponse.length);
mas01mj@732 355 body = body.replace(/\r\n\.\./, "\r\n.");
mas01mj@732 356 response.body = body;
mas01mj@732 357 return response;
mas01mj@732 358 }
mas01mj@732 359 }
mas01mj@732 360 }