Little.. Application Config, utility

Another one of those utilities I have laying around, that I’m sure someone here can make good use of.

Let’s say your developing an application and you want to be able to configure specifics of this application without editing code… well if you create a config file that is stored on the server that looks like the following…

Saved as, AppConfig.config

<?xml version="1.0"?>

<configuration>
    <appSettings>
        <!-- If you'd like to add a new app setting you simply have to add a new <setting> node..
             always give your setting a name that is intuitive, because this is how you will access it -->
        <setting name="image_dir" value="images/" />
    </appSettings>
    <userSettings>
        <!-- If you'd like to add a new user setting you simply have to add a new <setting> node..
             always give your setting a name that is intuitive, because this is how you will access it -->
        <setting name="allowAll" value="true" />
    </userSettings>
</configuration>

And you save the following class in your default class path as, AppConfigReader.as



class AppConfigReader extends XML
{
    // ignore all white space in the xml file
    private var ignoreWhite:Boolean = true;
    // all listeners listeneing to this object
    private var listeners:Array;
    
    public function AppConfigReader()
    {
        listeners = new Array();
    }
    
    /* Returns the value of the Application Setting you specify */
    public function getAppSetting(name:String) : String
    {
        var startNode:XMLNode = this.firstChild.firstChild;
        for (var i:Number=0; i<startNode.childNodes.length; i++)
        {
            if (startNode.childNodes*.attributes.name == name)
            {
                return startNode.childNodes*.attributes.value;
            }
        }
        
        return undefined;
    }
    
    /* Returns the value of the Application Setting you specify */
    public function getUserSetting(name:String) : String
    {
        var startNode:XMLNode = this.firstChild.childNodes[1];
        for (var i:Number=0; i<startNode.childNodes.length; i++)
        {
            if (startNode.childNodes*.attributes.name == name)
            {
                return startNode.childNodes*.attributes.value;
            }
        }
        
        return undefined;
    }
    
    /* Register a listener to listen for onLoad events from this object */
    public function addListener(listener:Object):Boolean
    {
        for (var i:Number = 0; i<listeners.length; i++)
        {
            if (listeners* == listener) return false;
        }
        
        listeners.push(listener);
        return true;
    }
    /* Unregisters a listener from this object */
    public function removeListener(listener:Object):Boolean
    {
        for (var i:Number = 0; i<listeners.length; i++)
        {
            if (listeners* == listener)
            {
                listeners.splice(i, 1);
                return true;
            }
        }
        
        return false;
    }
    
    private function invokeOnLoad(success : Boolean) : Void
    {
        for (var i:Number = 0; i<listeners.length; i++)
        {
            listeners*.onLoad(success);
        }
    }
    private function onLoad(success:Boolean):Void
    {
        invokeOnLoad(success);
    }
}

You can then access these values in the fla like this…

Fla Code:


var app_configs_rdr = new AppConfigReader();

app_configs_rdr.load("AppConfig.config");

var my_obj = new Object();

my_obj.onLoad = function(success:Boolean):Void
{
    trace(app_configs_rdr.getAppSetting("image_dir"));
    trace(app_configs_rdr.getUserSetting("allowAll"));
}

app_configs_rdr.addListener(my_obj);

If something doesn’t make sense, go ahead and ask and I will explain. Take Carre.

_Michael