1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2007 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package mx.core
13{
14
15import flash.display.Loader;
16import flash.events.Event;
17import flash.events.ErrorEvent;
18import flash.events.ProgressEvent;
19import flash.events.IOErrorEvent;
20import flash.events.SecurityErrorEvent;
21import flash.net.URLRequest;
22import flash.system.LoaderContext;
23import flash.system.ApplicationDomain;
24import flash.system.LoaderContext;
25import flash.system.Security;
26import flash.system.SecurityDomain;
27import flash.utils.ByteArray;
28
29import mx.events.RSLEvent;
30import mx.utils.LoaderUtil;
31
32[ExcludeClass]
33
34/**
35 *  @private
36 *  RSL Item Class
37 *
38 *  Contains properties to describe the RSL and methods to help load the RSL.
39 */
40public class RSLItem
41{
42    include "../core/Version.as";
43
44    //--------------------------------------------------------------------------
45    //
46    //  Properties
47    //
48    //--------------------------------------------------------------------------
49
50    //----------------------------------
51    //  urlRequest
52    //----------------------------------
53
54    /**
55     *  @private
56     *  Only valid after loading has started
57     */
58    public var urlRequest:URLRequest;
59
60    //----------------------------------
61    //  total
62    //----------------------------------
63
64    /**
65     *  @private
66     */
67    public var total:uint = 0;
68
69    //----------------------------------
70    //  loaded
71    //----------------------------------
72
73    /**
74     *  @private
75     */
76    public var loaded:uint = 0;
77
78    //----------------------------------
79    //  rootURL
80    //----------------------------------
81
82    /**
83     *  @private
84     *
85     *  Provides the url used to locate relative RSL urls.
86     */
87    public var rootURL:String;
88
89
90    //--------------------------------------------------------------------------
91    //
92    //  Variables
93    //
94    //--------------------------------------------------------------------------
95
96    /**
97     *  @private
98     */
99    protected var url:String;
100
101    /**
102     *  @private
103     */
104    private var errorText:String;
105
106    /**
107     *  @private
108     */
109    private var completed:Boolean = false;
110
111    /**
112     *  @private
113     *  External handlers so the load can be
114     *  observed by the class calling load().
115     */
116    protected var chainedProgressHandler:Function;
117    protected var chainedCompleteHandler:Function;
118    protected var chainedIOErrorHandler:Function;
119    protected var chainedSecurityErrorHandler:Function;
120    protected var chainedRSLErrorHandler:Function;
121
122
123    //--------------------------------------------------------------------------
124    //
125    //  Constructor
126    //
127    //--------------------------------------------------------------------------
128
129    /**
130     *  Create a RSLItem with a given URL.
131     *
132     *  @param url location of RSL to load
133     *
134     *  @param rootURL provides the url used to locate relative RSL urls.
135     */
136    public function RSLItem(url:String, rootURL:String = null)
137    {
138        super();
139
140        this.url = url;
141        this.rootURL = rootURL;
142    }
143
144    //--------------------------------------------------------------------------
145    //
146    //  Methods
147    //
148    //--------------------------------------------------------------------------
149
150    /**
151     *
152     *  Load an RSL.
153     *
154     *  @param progressHandler Receives ProgressEvent.PROGRESS events.
155     *  May be null.
156     *
157     *  @param completeHandler Receives Event.COMPLETE events.
158     *  May be null.
159     *
160     *  @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events.
161     *  May be null.
162     *
163     *  @param securityErrorHandler
164     *  Receives SecurityErrorEvent.SECURITY_ERROR events.
165     *  May be null.
166     *
167     *  @param rslErrorHandler Receives RSLEvent.RSL_ERROR events.
168     *  May be null.
169     */
170    public function load(progressHandler:Function,
171                         completeHandler:Function,
172                         ioErrorHandler:Function,
173                         securityErrorHandler:Function,
174                         rslErrorHandler:Function):void
175    {
176        chainedProgressHandler = progressHandler;
177        chainedCompleteHandler = completeHandler;
178        chainedIOErrorHandler = ioErrorHandler;
179        chainedSecurityErrorHandler = securityErrorHandler;
180        chainedRSLErrorHandler = rslErrorHandler;
181
182        var loader:Loader = new Loader();
183        var loaderContext:LoaderContext = new LoaderContext();
184        urlRequest = new URLRequest(LoaderUtil.createAbsoluteURL(rootURL, url));
185
186        // The RSLItem needs to listen to certain events.
187
188        loader.contentLoaderInfo.addEventListener(
189            ProgressEvent.PROGRESS, itemProgressHandler);
190
191        loader.contentLoaderInfo.addEventListener(
192            Event.COMPLETE, itemCompleteHandler);
193
194        loader.contentLoaderInfo.addEventListener(
195            IOErrorEvent.IO_ERROR, itemErrorHandler);
196
197        loader.contentLoaderInfo.addEventListener(
198            SecurityErrorEvent.SECURITY_ERROR, itemErrorHandler);
199
200        loaderContext.applicationDomain = ApplicationDomain.currentDomain;
201        loader.load(urlRequest, loaderContext);
202    }
203
204    //--------------------------------------------------------------------------
205    //
206    //  Event handlers
207    //
208    //--------------------------------------------------------------------------
209
210    /**
211     *  @private
212     */
213    public function itemProgressHandler(event:ProgressEvent):void
214    {
215        // Update the loaded and total properties.
216        loaded = event.bytesLoaded;
217        total = event.bytesTotal;
218
219        // Notify an external listener
220        if (chainedProgressHandler != null)
221            chainedProgressHandler(event);
222    }
223
224    /**
225     *  @private
226     */
227    public function itemCompleteHandler(event:Event):void
228    {
229        completed = true;
230
231        // Notify an external listener
232        if (chainedCompleteHandler != null)
233            chainedCompleteHandler(event);
234    }
235
236    /**
237     *  @private
238     */
239    public function itemErrorHandler(event:ErrorEvent):void
240    {
241        errorText = decodeURI(event.text);
242        completed = true;
243        loaded = 0;
244        total = 0;
245
246        trace(errorText);
247
248        // Notify an external listener
249        if (event.type == IOErrorEvent.IO_ERROR &&
250            chainedIOErrorHandler != null)
251        {
252            chainedIOErrorHandler(event);
253        }
254        else if (event.type == SecurityErrorEvent.SECURITY_ERROR &&
255                 chainedSecurityErrorHandler != null)
256        {
257            chainedSecurityErrorHandler(event);
258        }
259        else if (event.type == RSLEvent.RSL_ERROR &&
260                 chainedRSLErrorHandler != null)
261        {
262            chainedRSLErrorHandler(event);
263        }
264
265    }
266}
267
268}
269