Coverage Report - org.vostokframework.domain.loading.states.fileloader.algorithms.FileLoadingAlgorithm
 
Classes in this File Line Coverage Branch Coverage Complexity
FileLoadingAlgorithm
73%
122/166
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.domain.loading.states.fileloader.algorithms
 30  
 {
 31  
         import org.as3collections.IIterator;
 32  
         import org.as3collections.IList;
 33  
         import org.as3collections.lists.ArrayList;
 34  
         import org.as3coreaddendum.errors.ObjectDisposedError;
 35  
         import org.as3coreaddendum.errors.UnsupportedOperationError;
 36  
         import org.vostokframework.domain.loading.IDataParser;
 37  
         import org.vostokframework.domain.loading.LoadError;
 38  
         import org.vostokframework.domain.loading.LoadErrorType;
 39  
         import org.vostokframework.domain.loading.states.fileloader.IDataLoader;
 40  
         import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm;
 41  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent;
 42  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmEvent;
 43  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmMediaEvent;
 44  
 
 45  
         import flash.errors.IOError;
 46  
         import flash.events.AsyncErrorEvent;
 47  
         import flash.events.ErrorEvent;
 48  
         import flash.events.Event;
 49  
         import flash.events.EventDispatcher;
 50  
         import flash.events.HTTPStatusEvent;
 51  
         import flash.events.IOErrorEvent;
 52  
         import flash.events.NetStatusEvent;
 53  
         import flash.events.ProgressEvent;
 54  
         import flash.events.SecurityErrorEvent;
 55  
         import flash.utils.getTimer;
 56  
 
 57  
         /**
 58  
          * description
 59  
          * 
 60  
          * @author Flávio Silva
 61  
          */
 62  
         public class FileLoadingAlgorithm extends EventDispatcher implements IFileLoadingAlgorithm
 63  
         {
 64  
                 /**
 65  
                  * @private
 66  
                   */
 67  
                 private var _disposed:Boolean;
 68  
                 private var _dataLoader:IDataLoader;
 69  
                 private var _parsers:IList;
 70  
                 private var _timeConnectionStarted:int;
 71  
                 
 72  
                 /**
 73  
                  * description
 74  
                  * 
 75  
                  */
 76  
                 public function FileLoadingAlgorithm(dataLoader:IDataLoader)
 77  1
                 {
 78  1
                         if (!dataLoader) throw new ArgumentError("Argument <dataLoader> must not be null.");
 79  
                         
 80  1
                         _dataLoader = dataLoader;
 81  1
                 }
 82  
                 
 83  
                 override public function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void
 84  
                 {
 85  1
                         validateDisposal();
 86  
                         
 87  1
                         if (type == ProgressEvent.PROGRESS)
 88  
                         {
 89  1
                                 _dataLoader.addEventListener(type, listener, useCapture, priority, useWeakReference);
 90  
                         }
 91  
                         else
 92  
                         {
 93  1
                                 super.addEventListener(type, listener, useCapture, priority, useWeakReference);
 94  
                         }
 95  1
                 }
 96  
                 
 97  
                 public function addParsers(parsers:IList):void
 98  
                 {
 99  1
                         validateDisposal();
 100  1
                         _parsers = parsers;
 101  1
                 }
 102  
                 
 103  
                 /**
 104  
                  * description
 105  
                  * 
 106  
                  * @return
 107  
                   */
 108  
                 public function cancel(): void
 109  
                 {
 110  1
                         validateDisposal();
 111  1
                         _dataLoader.cancel();
 112  1
                 }
 113  
                 
 114  
                 override public function dispatchEvent(event : Event) : Boolean
 115  
                 {
 116  1
                         validateDisposal();
 117  
                         
 118  1
                         if (event.type == ProgressEvent.PROGRESS)
 119  
                         {
 120  0
                                 return _dataLoader.dispatchEvent(event);
 121  
                         }
 122  
                         else
 123  
                         {
 124  1
                                 return super.dispatchEvent(event);
 125  
                         }
 126  
                 }
 127  
                 
 128  
                 public function dispose():void
 129  
                 {
 130  1
                         if (_disposed) return;
 131  
                         
 132  1
                         removeDataLoaderListeners();
 133  1
                         _dataLoader.dispose();
 134  
                         
 135  1
                         _dataLoader = null;
 136  1
                         _disposed = true;
 137  
                         //_dataLoader = null;
 138  1
                         _parsers = null;
 139  1
                 }
 140  
                 
 141  
                 override public function hasEventListener(type : String) : Boolean
 142  
                 {
 143  0
                         validateDisposal();
 144  
 
 145  0
                         if (type == ProgressEvent.PROGRESS)
 146  
                         {
 147  0
                                 return _dataLoader.hasEventListener(type);
 148  
                         }
 149  
                         else
 150  
                         {
 151  0
                                 return super.hasEventListener(type);
 152  
                         }
 153  
                 }
 154  
                 
 155  
                 /**
 156  
                  * description
 157  
                  * 
 158  
                  * @return
 159  
                   */
 160  
                 public function load(): void
 161  
                 {
 162  1
                         validateDisposal();
 163  
                         
 164  1
                         addDataLoaderListeners();
 165  1
                         _timeConnectionStarted = getTimer();
 166  
                         
 167  
                         try
 168  
                         {
 169  1
                                 _dataLoader.load();
 170  
                         }
 171  1
                         catch (error:SecurityError)
 172  
                         {
 173  0
                                 securityError(error.message);
 174  
                         }
 175  0
                         catch (error:IOError)
 176  
                         {
 177  0
                                 ioError(error.message);
 178  
                         }
 179  0
                         catch (error:UnsupportedOperationError)
 180  
                         {
 181  0
                                 throw error;
 182  
                         }
 183  0
                         catch (error:Error)
 184  
                         {
 185  0
                                 unknownError(error.message);
 186  
                         }
 187  1
                 }
 188  
                 
 189  
                 override public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void
 190  
                 {
 191  1
                         validateDisposal();
 192  
                         
 193  1
                         if (type == ProgressEvent.PROGRESS)
 194  
                         {
 195  1
                                 _dataLoader.removeEventListener(type, listener, useCapture);
 196  
                         }
 197  
                         else
 198  
                         {
 199  1
                                 super.removeEventListener(type, listener, useCapture);
 200  
                         }
 201  1
                 }
 202  
                 
 203  
                 /**
 204  
                  * description
 205  
                  */
 206  
                 public function stop(): void
 207  
                 {
 208  1
                         validateDisposal();
 209  1
                         _dataLoader.stop();
 210  1
                 }
 211  
                 
 212  
                 override public function willTrigger(type : String) : Boolean
 213  
                 {
 214  0
                         validateDisposal();
 215  
                         
 216  0
                         if (type == ProgressEvent.PROGRESS)
 217  
                         {
 218  0
                                 return _dataLoader.willTrigger(type);
 219  
                         }
 220  
                         else
 221  
                         {
 222  0
                                 return super.willTrigger(type);
 223  
                         }
 224  
                 }
 225  
                 
 226  
                 protected function parseData(rawData:*):*
 227  
                 {
 228  1
                         validateDisposal();
 229  
                         
 230  1
                         if (!rawData) return null;
 231  1
                         if (!_parsers) return rawData;
 232  
                         
 233  0
                         var it:IIterator = _parsers.iterator();
 234  
                         var parser:IDataParser;
 235  0
                         var parsedData:* = rawData;
 236  
                         
 237  0
                         while (it.hasNext())
 238  
                         {
 239  0
                                 parser = it.next();
 240  0
                                 parsedData = parser.parse(parsedData);
 241  
                         }
 242  
                         
 243  0
                         return parsedData;
 244  
                 }
 245  
                 /*
 246  
                 protected function setLoadingDispatcher(dispatcher:IEventDispatcher):void
 247  
                 {
 248  
                         if (!dispatcher) throw new ArgumentError("Argument <dispatcher> must not be null.");
 249  
                         //_dispatcher = dispatcher;
 250  
                 }
 251  
                 */
 252  
                 private function addDataLoaderListeners():void
 253  
                 {
 254  1
                         _dataLoader.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler, false, 0, true);
 255  1
                         _dataLoader.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
 256  1
                         _dataLoader.addEventListener(FileLoadingAlgorithmMediaEvent.CUE_POINT, cuePointHandler, false, 0, true);
 257  1
                         _dataLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
 258  1
                         _dataLoader.addEventListener(Event.INIT, initHandler, false, 0, true);
 259  1
                         _dataLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
 260  1
                         _dataLoader.addEventListener(FileLoadingAlgorithmMediaEvent.META_DATA, metadataHandler, false, 0, true);
 261  1
                         _dataLoader.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
 262  1
                         _dataLoader.addEventListener(Event.OPEN, openHandler, false, 0, true);
 263  1
                         _dataLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
 264  1
                         _dataLoader.addEventListener(ErrorEvent.ERROR, unknownErrorHandler, false, 0, true);
 265  1
                 }
 266  
                 
 267  
                 private function failed(errorType:LoadErrorType, errorMessage:String):void
 268  
                 {
 269  1
                         validateDisposal();
 270  1
                         removeDataLoaderListeners();
 271  
                         
 272  1
                         var errors:IList = new ArrayList();
 273  1
                         var error:LoadError = new LoadError(errorType, errorMessage);
 274  1
                         errors.add(error);
 275  
                         
 276  1
                         dispatchEvent(new FileLoadingAlgorithmErrorEvent(FileLoadingAlgorithmErrorEvent.FAILED, errors));
 277  1
                 }
 278  
                 
 279  
                 private function getData():*
 280  
                 {
 281  1
                         validateDisposal();
 282  1
                         return parseData(_dataLoader.getData());
 283  
                 }
 284  
                 
 285  
                 private function loadingComplete():void
 286  
                 {
 287  1
                         validateDisposal();
 288  1
                         removeDataLoaderListeners();
 289  
                         
 290  
                         try
 291  
                         {
 292  1
                                 var data:* = getData();
 293  1
                                 dispatchEvent(new FileLoadingAlgorithmEvent(FileLoadingAlgorithmEvent.COMPLETE, data));
 294  
                         }
 295  1
                         catch (error:SecurityError)
 296  
                         {
 297  0
                                 securityError(error.message);
 298  
                         }
 299  0
                         catch (error:Error)
 300  
                         {
 301  0
                                 unknownError(error.message);
 302  
                         }
 303  1
                 }
 304  
                 
 305  
                 private function loadingInit():void
 306  
                 {
 307  1
                         validateDisposal();
 308  
                         
 309  
                         try
 310  
                         {
 311  1
                                 var data:* = getData();
 312  1
                                 dispatchEvent(new FileLoadingAlgorithmEvent(FileLoadingAlgorithmEvent.INIT, data));
 313  
                         }
 314  1
                         catch (error:SecurityError)
 315  
                         {
 316  0
                                 securityError(error.message);
 317  
                         }
 318  0
                         catch (error:Error)
 319  
                         {
 320  0
                                 unknownError(error.message);
 321  
                         }
 322  1
                 }
 323  
                 
 324  
                 private function loadingOpen():void
 325  
                 {
 326  1
                         validateDisposal();
 327  
                         
 328  1
                         var latency:int = getTimer() - _timeConnectionStarted;
 329  
                         
 330  
                         try
 331  
                         {
 332  1
                                 var data:* = getData();
 333  1
                                 dispatchEvent(new FileLoadingAlgorithmEvent(FileLoadingAlgorithmEvent.OPEN, data, latency));
 334  
                         }
 335  1
                         catch (error:SecurityError)
 336  
                         {
 337  0
                                 securityError(error.message);
 338  
                         }
 339  0
                         catch (error:Error)
 340  
                         {
 341  0
                                 unknownError(error.message);
 342  
                         }
 343  1
                 }
 344  
                 
 345  
                 private function asyncError(errorMessage:String):void
 346  
                 {
 347  0
                         validateDisposal();
 348  0
                         failed(LoadErrorType.ASYNC_ERROR, errorMessage);
 349  0
                 }
 350  
                 
 351  
                 private function ioError(errorMessage:String):void
 352  
                 {
 353  1
                         validateDisposal();
 354  1
                         failed(LoadErrorType.IO_ERROR, errorMessage);
 355  1
                 }
 356  
                 
 357  
                 private function securityError(errorMessage:String):void
 358  
                 {
 359  1
                         validateDisposal();
 360  1
                         failed(LoadErrorType.SECURITY_ERROR, errorMessage);
 361  1
                 }
 362  
                 
 363  
                 private function unknownError(errorMessage:String):void
 364  
                 {
 365  1
                         validateDisposal();
 366  1
                         failed(LoadErrorType.UNKNOWN_ERROR, errorMessage);
 367  1
                 }
 368  
                 
 369  
                 private function removeDataLoaderListeners():void
 370  
                 {
 371  1
                         _dataLoader.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler, false);
 372  1
                         _dataLoader.removeEventListener(Event.COMPLETE, completeHandler, false);
 373  1
                         _dataLoader.removeEventListener(FileLoadingAlgorithmMediaEvent.CUE_POINT, cuePointHandler, false);
 374  1
                         _dataLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false);
 375  1
                         _dataLoader.removeEventListener(Event.INIT, initHandler, false);
 376  1
                         _dataLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false);
 377  1
                         _dataLoader.removeEventListener(FileLoadingAlgorithmMediaEvent.META_DATA, metadataHandler, false);
 378  1
                         _dataLoader.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false);
 379  1
                         _dataLoader.removeEventListener(Event.OPEN, openHandler, false);
 380  1
                         _dataLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false);
 381  1
                         _dataLoader.removeEventListener(ErrorEvent.ERROR, unknownErrorHandler, false);
 382  1
                 }
 383  
                 
 384  
                 /**
 385  
                  * @private
 386  
                  */
 387  
                 private function validateDisposal():void
 388  
                 {
 389  1
                         if (_disposed) throw new ObjectDisposedError("This object was disposed, therefore no more operations can be performed.");
 390  1
                 }
 391  
                 
 392  
                 ////////////////////////////////////////////////////////////////////////
 393  
                 ///////////////////////// DISPATCHER LISTENERS /////////////////////////
 394  
                 ////////////////////////////////////////////////////////////////////////
 395  
                 
 396  
                 private function asyncErrorHandler(event:AsyncErrorEvent):void
 397  
                 {
 398  0
                         asyncError(event.text);
 399  0
                 }
 400  
                 
 401  
                 private function completeHandler(event:Event):void
 402  
                 {
 403  1
                         loadingComplete();
 404  1
                 }
 405  
                 
 406  
                 private function cuePointHandler(event:FileLoadingAlgorithmMediaEvent):void
 407  
                 {
 408  0
                         dispatchEvent(event);
 409  0
                 }
 410  
                 
 411  
                 private function httpStatusHandler(event:HTTPStatusEvent):void
 412  
                 {
 413  1
                         var $event:FileLoadingAlgorithmEvent = new FileLoadingAlgorithmEvent(FileLoadingAlgorithmEvent.HTTP_STATUS);
 414  1
                         $event.httpStatus = event.status;
 415  
                         
 416  1
                         dispatchEvent($event);
 417  1
                 }
 418  
                 
 419  
                 private function initHandler(event:Event):void
 420  
                 {
 421  1
                         validateDisposal();
 422  1
                         loadingInit();
 423  1
                 }
 424  
                 
 425  
                 private function ioErrorHandler(event:IOErrorEvent):void
 426  
                 {
 427  1
                         ioError(event.text);
 428  1
                 }
 429  
                 
 430  
                 private function metadataHandler(event:FileLoadingAlgorithmMediaEvent):void
 431  
                 {
 432  0
                         dispatchEvent(event);
 433  0
                 }
 434  
                 
 435  
                 private function netStatusHandler(event:NetStatusEvent):void
 436  
                 {
 437  0
                         var $event:FileLoadingAlgorithmMediaEvent = new FileLoadingAlgorithmMediaEvent(FileLoadingAlgorithmMediaEvent.NET_STATUS);
 438  0
                         $event.netStatusInfo = event.info;
 439  
                         
 440  0
                         dispatchEvent($event);
 441  0
                 }
 442  
                 
 443  
                 private function openHandler(event:Event):void
 444  
                 {
 445  1
                         validateDisposal();
 446  1
                         loadingOpen();
 447  1
                 }
 448  
                 
 449  
                 private function securityErrorHandler(event:SecurityErrorEvent):void
 450  
                 {
 451  1
                         securityError(event.text);
 452  1
                 }
 453  
                 
 454  
                 private function unknownErrorHandler(event:ErrorEvent):void
 455  
                 {
 456  1
                         unknownError(event.text);
 457  1
                 }
 458  
 
 459  
         }
 460  
 
 461  
 }