Config (class)

Categories:OOP

Overview

Summary

The Config class is an object oriented wrapper for easily saving and loading settings to XML files. It essentially wraps up XML file loading and saving, along with a simple external API to set and retrieve settings.

Contents

Usage

Although you can access the internal XML via the .xml property, the API provides the following benefits over direct modification:

  • the XML file is saved to disk each time a value is modified
  • values are automatically parsed to their correct datatypes using xJSFL's standard xjsfl.utils.parseValue()

Note that because Config saves settings to disk automatically, settings are persistent between both calls and Flash sessions.

API - methods

Config(configPath, xml)

Config class, loads and saves XML from the config folders

Parameters:

  • configPath String The absolute or relative path to the config file
  • xml XML An XML object with which to populate the Config instance

Creating a new Config instance will do one of 2 things:

  1. If the associated XML file doesn't exist, the Config instance will be created, without yet creating an XML file
  2. If the associated XML file does exist, the file will be loaded and the data saved in the file will be available to use straight away

Note that passing a relative file path will attempt to find the file in the cascading file structure, defaulting to user/config/

The following example creates a new empty config file in the user folder:

var config = new Config('test');
trace(config);
[object Config path="xJSFL/user/config/test.xml" nodes=0]

Note that you can save config data to subfolders by specifying a path:

var config = new Config('setttings/test');

There is no need to specify an .xml extension as Config files are XML by default, but if you do so, it will be ignored.

set(path, value)

Sets data on the wrapped XML data

Parameters:

  • path String A dot-notation path to a node or attribute
  • value Value Any value that can be converted to a string

Returns:

  •   Config The current Config node

The following example sets various properties and attributes on a new Config instance:

var config = new Config('settings/test');
config
	.set('app', 'xJSFL')                // root-level node (string)
	.set('@id', 1)                      // root-level attribute (number)
	.set('long.path.to.value', 'Hello') // compound path

trace(config.toString(true));
<settings id="1">
  <app>xJSFL</app>
  <long>
    <path>
      <to>
        <value>Hello</value>
      </to>
    </path>
  </long>
</settings>

The following example attempts to set various datatypes:

var config = new Config('settings/test');
config
    .clear()
    .set('string', '"Hello"')
    .set('illegalstring', '<Hello>')
    .set('number', 1)
    .set('boolean', true)
    .set('xml', <name>Dave</name>)
    .set('xmllist', new XMLList('<icon id="1" /><icon id="2" /><icon id="3" />'))
    .set('date', new Date())
    .set('class', new Config('settings/config'))
    .set('array', [1,2,3,4,5]);

trace(config.toString(true));
<settings>
    <string>"Hello"</string>
    <illegalstring>&lt;Hello&gt;</illegalstring>
    <number>1</number>
    <boolean>true</boolean>
    <xml>
        <name>Dave</name>
    </xml>
    <xmllist>
        <icon id="1"/>
        <icon id="2"/>
        <icon id="3"/>
    </xmllist>
    <date>Thu Jul 21 2011 17:43:05 GMT+0100 (GMT Daylight Time)</date>
    <class>[object Config path="xJSFL/user/config/settings/config.xml" nodes=0]</class>
    <array>1,2,3,4,5</array>
</settings>

get(path, parse)

Gets the value of the specified node path

Parameters:

  • path String A dot-notation String path to a node or attribute
  • parse Boolean A Boolean flag indicating that you want to parse the value to the currect datatype
  • test Boolean A Boolean flag to test the path exists, but trap and return an Error object if not

Returns:

  •   value The value of the node or attribute

The following example gets a node attribute:

var config = new Config('settings/test');
config
	.clear()
	.set('some.setting.@id', 'Hello')
	
trace(config.toString(true));
trace(config.get('some.setting.@id'));
<settings>
  <some>
    <setting id="Hello"/>
  </some>
</settings>
Hello

The following example grabs two attributes and parses them to the correct datatypes:

var config = new Config('settings/test');
config
	.clear()
	.set('five', 5)
	.set('four', 4)
	
trace(config.toString(true));
trace('five times four is: ' + config.get('five') * config.get('four'));
<settings>
  <five>5</five>
  <four>4</four>
</settings>
five times four is: 20

load()

Loads the XML config from disk

Returns:

  •   Config The current Config node

You normally won't need to call this method, as a Config instance's XML is loaded automatically from disk when first instantiated, however, the following example loads (or reloads) the related XML file:

var config = new Config('settings/test');
// perhaps something happened here, like an external modification to config
config.load();
[object Config path="xJSFL/user/config/settings/test.xml" nodes=5]

save()

Saves the internal XML to disk

Returns:

  •   Config The current Config node

The following example saves the current config instance:

config.save();	

clear()

Clears all nodes inside the internal XML

Returns:

  •   Config The current Config node

The following example clears the current config instance:

config.clear();
<settings/>		

toString(asXML)

Returns either a standard String summary of the Config item or the pretty-printed XML contents

Parameters:

  • asXML Boolean An optional flag to return the XML String

Returns:

  •   String The standard String summary of the Config instance
  •   String The pretty-printed XML contents

The following example outputs the current config instance as an XML string:

config.toString(true);

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>