root/trunk/as3/com/jeroenwijering/utils/Configger.as @ 1

Revision 1, 2.5 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

Line 
1/**
2* Loads application configuration data (from xml, cookies and flashvars).
3**/
4
5
6package com.jeroenwijering.utils {
7
8
9import flash.events.Event;
10import flash.events.EventDispatcher;
11import flash.display.Sprite;
12import flash.net.SharedObject;
13import flash.net.URLRequest;
14import flash.net.URLLoader;
15import com.jeroenwijering.utils.Strings;
16
17
18public class Configger extends EventDispatcher {
19
20
21        /** Reference to a display object to get flashvars from. **/
22        private var reference:Sprite;
23        /** Reference to the config object. **/
24        public var config:Object;
25        /** XML loading object reference **/
26        private var loader:URLLoader;
27        /** Cookie object. **/
28        private static var cookie:SharedObject;
29
30
31        /** Constructor; nothing fancy. **/
32        public function Configger(ref:Sprite) {
33                reference = ref;
34        };
35
36
37        /**
38        * Start the loading process.
39        *
40        * @param def    The config object to overwrite new data in.
41        **/
42        public function load(def:Object) {
43                config = def;
44                var xml = reference.root.loaderInfo.parameters['config'];
45                if(xml) {
46                        loadXML(Strings.decode(xml));
47                } else {
48                        loadCookies();
49                }
50        };
51
52
53        /** Load configuration data from external XML file. **/
54        private function loadXML(url:String) {
55                loader = new URLLoader();
56                loader.addEventListener(Event.COMPLETE,xmlHandler);
57                try {
58                        loader.load(new URLRequest(url));
59                } catch (err:Error) { throw err; }
60        };
61
62
63        /** Parse the XML list **/
64        private function xmlHandler(evt:Event) {
65                var dat = XML(evt.currentTarget.data);
66                var obj = new Object();
67                for each (var prp in dat.children()) {
68                        obj[prp.name()] = prp.text();
69                }
70                compareWrite(obj)
71                loadCookies();
72        };
73
74
75        /** Load configuration data from flashcookie. **/
76        private function loadCookies() {
77                Configger.cookie = SharedObject.getLocal('com.jeroenwijering.utils','/');
78                compareWrite(Configger.cookie.data);
79                loadFlashvars();
80        };
81
82
83        /** Set config variables or load them from flashvars. **/
84        private function loadFlashvars() {
85                compareWrite(reference.root.loaderInfo.parameters);
86                dispatchEvent(new Event(Event.COMPLETE));
87        };
88
89
90        /** Compare and save new items in config, preserving datatype. **/
91        private function compareWrite(obj:Object) {
92                for(var cfv in config) {
93                        var lfv = cfv.toLowerCase();
94                        if(obj[lfv] != undefined) {
95                                config[lfv] = Strings.serialize(obj[lfv],config[cfv]);
96                        }
97                }
98        };
99
100
101        /**
102        * Save config parameter to cookie.
103        *
104        * @param prm    The parameter name.
105        * @param val    The parameter value.
106        **/
107        public static function saveCookie(prm:String,val:Object) {
108                Configger.cookie.data[prm] = val;
109                Configger.cookie.flush();
110        };
111
112
113}
114
115
116}
Note: See TracBrowser for help on using the browser.