Parser
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Classpath: | org.asapframework.data.loader.Parser |
| File last modified: | Thursday, 09 November 2006, 10:57:18 |
Class for parsing Object data, usually from org.asapframework.util.xml.XML2Object.parseXML, into DataValueObject classes.
The class provides static functions for calling IParsable.parseObject on newly created objects of a specified type, either for single data blocks or for an array of similar data.
The Parser removes the tedious for-loop from the location where the XML data is loaded, and moves the parsing of the XML data to the location where it's used for the first time: the DataValueObject class. Your application can use this data, that contains typed variables, without ever caring about the original source of the data.
When the XML structure is changed, only the parsing function in the DataValueObject class has to be changed, thereby facilitating maintenance and development.
The class provides static functions for calling IParsable.parseObject on newly created objects of a specified type, either for single data blocks or for an array of similar data.
The Parser removes the tedious for-loop from the location where the XML data is loaded, and moves the parsing of the XML data to the location where it's used for the first time: the DataValueObject class. Your application can use this data, that contains typed variables, without ever caring about the original source of the data.
When the XML structure is changed, only the parsing function in the DataValueObject class has to be changed, thereby facilitating maintenance and development.
Usage:
- Consider an XML file with the following content:
<?xml version="1.0" encoding="UTF-8"?> <settings> <urls> <url name="addressform" url="../xml/address.xml" /> <url name="entries" url="../xml/entries.xml" /> </urls> </settings>
See the DataLoader for an example of how to load this XML and get it as an Object. Once the XML has been loaded and parsed into an Object, it can be converted into an Array of URLData objects with the following code:// objects of type URLData private var mURLs:Array; // parse an Object containing converted XML // @param o: Object from DataLoader or XML2Object // @return true if parsing went ok, otherwise false private function handleSettingsLoaded (o:Object) : Boolean { mURLs = Parser.parseList(o.settings.urls.url, org.asapframework.data.loader.URLData, false); return (mURLs != null); }
After calling this function, the member variablemURLscontains a list of objects of type URLData, filled with the content of the XML file.
Notes to this code:- The first parameter to parseList is a (can be a) repeating node where each node contains similar data to be parsed into
- Conversion of nodes to an Array is not necessary. If the
<urls>-node in the aforementioned XML file would contain only one<url>-node, the parser still returns an Array, with one object of type URLData. - Since the last parameter to the call to parseList is false, an error in the xml data will result in mURLs being null. The parsing class determines if the data is valid or not, by returning true or false from parseObject().
Summary
Class methods
- parseList (inListObj:Object, f:Function, ignoreError:Boolean) : Array
- Parse an array of objects from XML into an array of the specified class instance by calling its parseObject function
- parseObject (inObject:Object, f:Function, ignoreError:Boolean) : IParsable
- Parse an object from XML into the specified class instance by calling its parseObject function
Class methods
parseList
static function parseList (
inListObj:Object,
f:Function,
ignoreError:Boolean) : Array
Parse an array of objects from XML into an array of the specified class instance by calling its parseObject function
Parameters:
inObject :
object from XML2Object (usually), will be converted to an Array if it isn't already
f :
classname to be instanced
ignoreError:
if true, the return value of parseObject is always added to the array, and the array itself is returned. Otherwise, an error in parsing will return null.
Returns:
- Array of new objects of the specified type, cast to IParsable, or null if parsing returned false
parseObject
Parse an object from XML into the specified class instance by calling its parseObject function
Parameters:
inObject :
object from XML2Object (usually)
f :
classname to be instanced
ignoreError:
if true, the return value of IParsable.parseObject is ignored, and the newly created object is always returned.
Returns:
- a new object of the specified type, cast to IParsable, or null if parsing returned false