Access keys

TextFile2Tree

Kind of class: class
Inherits from: TextFile2Collection < Dispatcher
Version: 20 July 2005
Author: Arthur Clemens
Classpath: data.filedatatransform.TextFile2Tree
File last modified: Tuesday, 03 January 2006, 00:06:10
This simple text parser, inspired by ogdl (see http://ogdl.org), reads in a text file and transforms its content to data.tree.TreeNode nodes (or a subclass of TreeNode, depending on the type of the root node), where each word corresponds to a node name (identifier).


The text file uses tab indenting to indicate node depths:
Home
    GamePass
        Points
        ClaimPrize
            SendToAddress
        PersonalData
    Help
        GamesHelp
Disclaimer
    PrivacyDetails
Zero tabs indicate a node depth of 1 (depth 0 is reserved for the root node - the root node is not written in the text file).

Node properties can be passed as a comma-separated list, with the syntax key=value. In the following example file text, each line starts with the node name, followed by an arbitrary text called text, and a statistics id.
Home, text=Homepage, id=FYOS_0100_flash_home
    GamePass, text=Game Pass, stats_id=FYOS_0200_game_pass
Node properties are stored in the data.tree.TreeNode.properties member as Value Object, and can be accessed with data.tree.TreeNode.getValueForKey. See also data.tree.TreeEnumerator for an example to access TreeNodes.

Two characters are reserved for parsing: the comma (,) and the equals sign (=). If you want to use these characters inside a property text, surround the text by quotes. For instance: GamePass, text="Game Pass, Personal Items", stats_id=QWERTY_0010. The text will be returned as Game Pass, Personal Items, without quotes and with the comma preserved.

Another way is to set a different delimiter or key/value delimiter in the constructor.
Usage:
var transformer:TextFile2Tree = new TextFile2Tree(DATA_URL, this);
transformer.load();
In same class implement the 'finished' event method:
private function onTextFile2TreeFinished (inEvent:TextFile2TreeEvent) : Void
{
    mTree = inEvent.tree;
    mTree.printNodes();
}
Events broadcast to listeners:
TextFile2TreeEvent with type: FINISHED

Summary

Constructor
Instance properties
Instance properties inherited from TextFile2Collection
Instance methods
Instance methods inherited from TextFile2Collection
Event handlers
Event handlers inherited from TextFile2Collection

Constructor

TextFile2Tree

function TextFile2Tree (
inFileUrl:String, inListener:Object, inTree:Tree, inItemDelimiter:String, inKeyValueDelimiter:String)
Creates a new TextFile2Tree instance.
Parameters:
inFileUrl :
url of the text file with data to parse
inListener :
(optional) object that receives a TextFile2TreeEvent.FINISHED after parsing
inCollection :
(optional) existing Tree object to store the data in; if not provided, a new Tree will be created
inItemDelimiter :
(optional) character to split each line in separate property components; default a comma (",")
inKeyValueDelimiter:
(optional) character to split each key/value pair in components; default an equals sign ("=")
Example:
mTree = new Tree("Site tree");
var siteDataUrl:String = "../../www/data/sitedata.txt";
new TextFile2Tree(dataUrl, this, mTree).load(); // sets 'this' as listener
Implement listener method:
private function onTextFile2TreeFinished (inEvent:TextFile2TreeEvent) : Void
{
    var filledTree:Tree = inEvent.tree;
    filledTree.printNodes();
}