AsBeanGen – Generate value objects for DTD driven XML files
Update:
Download AsBeanGen tool
Download AsBeanGen source
I fixed some of the bugs mentioned in the comments:
- stripping '-' and '_' chars from the bean names
- handeling recursive DTD
- added 'implicit getters' mode
- new option to generate interfaces
.....................................................................................................................................................
AsBeanGen is a class generator application written in java which generates as3 data bean classes from DTD files. Overall it shortens development time a lot, because it rids you of spending a considerable amount of time on writing data bean classes.
Instead AsBeanGen can generate hunderts of classes in less than a second for you and as an added bonus it doesn't just generate the bean classes, but also the classes which transform the content of an XML file (implementing the DTD) to data bean objects. So finally you just have to write two lines of code yourself to get a strongly typed object representation of an XML file.
If you never heard of DTD (Document Type Definition), I suggest you to read this.
As DTD doesn't support type information natively, you also have to annotate the DTD files through simple comments to add the neccessary type information, otherwise AsBeanGen uses 'Object' as the standard type.
To give you a more specific idea how it is used, let's jump straight into an example:
This is the annotatd DTD file
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT test (size,fonts+) > <!-- Sets the types for the properties of the SizeData class TYPE size.width=int TYPE size.height=int --> <!ELEMENT size EMPTY > <!ATTLIST size width CDATA #REQUIRED height CDATA #REQUIRED > <!-- Sets the types for the properties of the FontsData class TYPE fonts.path=String Generates 'getFontDataById(id:String):FontData' method into FontsData class GENERATE font.id Generates 'getFontDataByName(name:String):FontData' method into FontsData class GENERATE font.name --> <!ELEMENT fonts (font+) > <!ATTLIST fonts path CDATA #REQUIRED > <!-- Sets the types for the properties of the FontData class TYPE font.id=String TYPE font.type=String TYPE font.name=String TYPE font.src=String TYPE font.systemfont=Boolean --> <!ELEMENT font EMPTY > <!ATTLIST font id CDATA #REQUIRED type CDATA #REQUIRED name CDATA #REQUIRED src CDATA #REQUIRED systemfont (true|false) #IMPLIED >
This is an xml file based on the DTD
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE test SYSTEM "test.dtd"> <test> <size height="100" width="100"/> <fonts path="fonts/"> <font id="foo" name="Arial" src="arial.swf" type="text"/> <font id="foo2" name="Arial2" src="arial2.swf" type="text2"/> </fonts> </test>
This is the generated FontsData bean class
package data.test { /** * Class generated by AsBeanGen * http://www.splink.org */ import data.test.fonts.FontData; public class FontsData { private var _path : String; private var _fontData : Array; public function FontsData() { } public function getPath() : String { return _path; } public function setPath(value:String) : void { _path = value; } public function getFontDataById(id:String) : FontData { var data:FontData; var r:FontData = null; for (var i:int=0; i<_fontData.length; i++) { data = _fontData[i]; if(data.getId() == id) { r = data; break; } } return r; } public function getFontDataByName(name:String) : FontData { var data:FontData; var r:FontData = null; for (var i:int=0; i<_fontData.length; i++) { data = _fontData[i]; if(data.getName() == name) { r = data; break; } } return r; } /** * @return an array of FontData objects */ public function getFontDataArray() : Array { return _fontData; } /** * @param an array of FontData objects */ public function setFontDataArray(value:Array) : void { _fontData = value; } } }
This is the generated FontsDataReader class for the FontsData bean class
package data.test { /** * Class generated by AsBeanGen * http://www.splink.org */ import data.test.FontsData; import data.test.fonts.FontDataReader; public class FontsDataReader { private var _xml : XML; public function FontsDataReader(xml:XML) { _xml = xml; } public function read() : FontsData { var fontsData:FontsData = new FontsData(); fontsData.setPath(_xml.@path); fontsData.setFontDataArray(readFontData()); return fontsData; } private function readFontData() : Array { var rAr:Array = []; for each(var xml:XML in _xml['font']) { var reader:FontDataReader = new FontDataReader(xml); rAr.push(reader.read()); } return rAr; } } }
This is what the generated structure looks like:
This is how to use the generated classes in your application:
// TestDataReader is the root reader and expects the loaded xml file // as a parameter. var reader:TestDataReader = new TestDataReader(xml); // TestDataReader's read method returns the root object of the // generated tree, filled with values from the given xml. var data:TestData = reader.read();
AsBeanGen can be used as a commandline application (usage shown below), or, if you are eclipse user you can add it as an 'External Tool' which proves to be very convenient (at least to me).
java -jar AsBeanGen.jar test.DTD C:\your\project\src de.your.package.name.test
Note that the "test" in the package name is the name of the DTD file.
How to add AsBeanGen as an 'External Tool' in Eclipse
1) Open your annotated DTD file
2) Open the 'External Tools'

3) Create a 'New Launch Configuration'
4) Fill the form (see screenshot)

4) After configuring AsBeanGen, you will want it to appear in the External Tools favorites. Switch to the 'Common' tab and check 'External Tools' within the 'Display in favorites menu' box.
5) Press 'Apply' button

Now you should be able to launch AsBeanGen from the 'External Tools Favorites Bar'. Just make sure that the DTD from which you wish to generate the actionscript classes is opened and focused.
