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     *  External handlers so the load can be
99     *  observed by the class calling load().
100     */
101    protected var chainedProgressHandler:Function;
102    protected var chainedCompleteHandler:Function;
103    protected var chainedIOErrorHandler:Function;
104    protected var chainedSecurityErrorHandler:Function;
105    protected var chainedRSLErrorHandler:Function;
106
107    /**
108     *  @private
109     */
110    private var completed:Boolean = false;
111
112    /**
113     *  @private
114     */
115    private var errorText:String;
116
117    /**
118     *  @private
119     */
120    protected var moduleFactory:IFlexModuleFactory; // application/module loading this RSL.
121
122    /**
123     *  @private
124     */
125    protected var url:String;
126
127    //--------------------------------------------------------------------------
128    //
129    //  Constructor
130    //
131    //--------------------------------------------------------------------------
132
133    /**
134     *  Create a RSLItem with a given URL.
135     *
136     *  @param url location of RSL to load
137     *  @param rootURL provides the url used to locate relative RSL urls.
138     *  @param moduleFactory The module factory that is loading the RSLs. The
139     *  RSLs will be loaded into the application domain of the given module factory.
140     *  If a module factory is not specified, then the RSLs will be loaded into the
141     *  application domain of where the CrossDomainRSLItem class was first loaded.
142     *
143     *  @langversion 3.0
144     *  @playerversion Flash 9
145     *  @playerversion AIR 1.1
146     *  @productversion Flex 3
147     */
148    public function RSLItem(url:String, rootURL:String = null,
149                            moduleFactory:IFlexModuleFactory = null)
150    {
151        super();
152
153        this.url = url;
154        this.rootURL = rootURL;
155        this.moduleFactory = moduleFactory;
156    }
157
158    //--------------------------------------------------------------------------
159    //
160    //  Methods
161    //
162    //--------------------------------------------------------------------------
163
164    /**
165     *
166     *  Load an RSL.
167     *
168     *  @param progressHandler Receives ProgressEvent.PROGRESS events.
169     *  May be null.
170     *
171     *  @param completeHandler Receives Event.COMPLETE events.
172     *  May be null.
173     *
174     *  @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events.
175     *  May be null.
176     *
177     *  @param securityErrorHandler
178     *  Receives SecurityErrorEvent.SECURITY_ERROR events.
179     *  May be null.
180     *
181     *  @param rslErrorHandler Receives RSLEvent.RSL_ERROR events.
182     *  May be null.
183     *
184     *  @langversion 3.0
185     *  @playerversion Flash 9
186     *  @playerversion AIR 1.1
187     *  @productversion Flex 3
188     */
189    public function load(progressHandler:Function,
190                         completeHandler:Function,
191                         ioErrorHandler:Function,
192                         securityErrorHandler:Function,
193                         rslErrorHandler:Function):void
194    {
195        chainedProgressHandler = progressHandler;
196        chainedCompleteHandler = completeHandler;
197        chainedIOErrorHandler = ioErrorHandler;
198        chainedSecurityErrorHandler = securityErrorHandler;
199        chainedRSLErrorHandler = rslErrorHandler;
200
201        var loader:Loader = new Loader();
202        var loaderContext:LoaderContext = new LoaderContext();
203        urlRequest = new URLRequest(LoaderUtil.createAbsoluteURL(rootURL, url));
204
205        // The RSLItem needs to listen to certain events.
206
207        loader.contentLoaderInfo.addEventListener(
208            ProgressEvent.PROGRESS, itemProgressHandler);
209
210        loader.contentLoaderInfo.addEventListener(
211            Event.COMPLETE, itemCompleteHandler);
212
213        loader.contentLoaderInfo.addEventListener(
214            IOErrorEvent.IO_ERROR, itemErrorHandler);
215
216        loader.contentLoaderInfo.addEventListener(
217            SecurityErrorEvent.SECURITY_ERROR, itemErrorHandler);
218
219        if (moduleFactory != null)
220            loaderContext.applicationDomain = moduleFactory.info()["currentDomain"];
221        else
222            loaderContext.applicationDomain = ApplicationDomain.currentDomain;
223
224        loader.load(urlRequest, loaderContext);
225    }
226
227    //--------------------------------------------------------------------------
228    //
229    //  Event handlers
230    //
231    //--------------------------------------------------------------------------
232
233    /**
234     *  @private
235     */
236    public function itemProgressHandler(event:ProgressEvent):void
237    {
238        // Update the loaded and total properties.
239        loaded = event.bytesLoaded;
240        total = event.bytesTotal;
241
242        // Notify an external listener
243        if (chainedProgressHandler != null)
244            chainedProgressHandler(event);
245    }
246
247    /**
248     *  @private
249     */
250    public function itemCompleteHandler(event:Event):void
251    {
252        completed = true;
253
254        // Notify an external listener
255        if (chainedCompleteHandler != null)
256            chainedCompleteHandler(event);
257    }
258
259    /**
260     *  @private
261     */
262    public function itemErrorHandler(event:ErrorEvent):void
263    {
264        errorText = decodeURI(event.text);
265        completed = true;
266        loaded = 0;
267        total = 0;
268
269        trace(errorText);
270
271        // Notify an external listener
272        if (event.type == IOErrorEvent.IO_ERROR &&
273            chainedIOErrorHandler != null)
274        {
275            chainedIOErrorHandler(event);
276        }
277        else if (event.type == SecurityErrorEvent.SECURITY_ERROR &&
278                 chainedSecurityErrorHandler != null)
279        {
280            chainedSecurityErrorHandler(event);
281        }
282        else if (event.type == RSLEvent.RSL_ERROR &&
283                 chainedRSLErrorHandler != null)
284        {
285            chainedRSLErrorHandler(event);
286        }
287
288    }
289}
290
291}
292