import mx.utils.*;
import mx.events.*;
import com.xfactorstudio.xml.xpath.*;
import com.peterjoel.xml.*;
/**
* Loads and parses simple Configuration parameters from an XML file.
*
* Config is a singleton, accessed via Config.getInstance().
*
* Properties are all strings and available using the getResource, once the xml has been loaded
*
*/
class com.peterjoel.utils.Config {
/**
* Triggered when the config file has loaded
*/
[Event("load")]
public static var EVENT_LOAD:String = "load";
private static var configResourceCache:Array;
private static var configParamCache:Array;
private var configXML:XML;
private var loadDelegate:Function;
private var defaultConfigURL:String;
private var configURLSuffix:String = "_config.xml";
private var __url:String;
private static var __instance:Config;
public function get loaded():Boolean{
return configXML.loaded;
}
/**
* Constructor method; creates an instance of Config
*/
public static function getInstance():Config{
if(!__instance){
__instance = new Config();
}
return __instance;
}
private function Config(){
loadDelegate = Delegate.create(this, configLoaded);
configXML = new XML();
configXML.ignoreWhite = true;
configXML.onLoad = loadDelegate;
}
/**
* Load the xml. The URL is determined by searching in _level0 for configURL, which may be passed there via FlashVars or
* url query string. Otherwise a default - "_config.xml" - is used
*/
public function load(swf:MovieClip, url:String){
if(url == null){
url = _level0.configURL==null ? getDefaultConfigURL(swf==null ? _level0 : swf) : _level0.configURL;
}
__url = url;
configXML.load(url);
}
function configLoaded(){
if(configXML.status){
dispatchEvent({type:"error", code:configXML.status});
}else{
dispatchEvent({type:EVENT_LOAD});
}
}
/**
* Retrieves a parameter value from the loaded data. name corresponds to the name attribute of a element
* in the xml. The return value is the corresponding value attribute.
*/
public function getParam(name:String):Object{
if(configParamCache == null){
configParamCache = XPath.selectNodes(configXML, "//config/parameters/paramGroup/param");
}
var params:Array = configParamCache;
var n = params.length;
var value:String;
var node:XMLNode;
for(var i=0; i element
* in the xml. The return value is the corresponding value attribute.
*/
public function getResource (name:String):String{
if(configResourceCache == null){
configResourceCache = XPath.selectNodes(configXML, "//config/resources/resGroup/res");
}
var res:Array = configResourceCache;
var n = res.length;
for(var i=0; i