On Friday, I decided I was going to extend dumb to be able to read or write XML documents as data. Instead, I figured, with configuration, I should be able to write any document as I see fit, without worrying about whether or not dumb can read it. I decided to start a new project, and soon after, I finished it! It's along the lines of Jakarta Commons Configuration, although definitely not nearly as complex. It's just for XML configuration files following any scheme. Basically, I tell what a bunch of XML tags mean, and what class to load them as, and it reads my config files and loads objects from the xml, and makes them available by the xml tag name that are in my config files. This takes typically one line of XML for each type of object. It's simple.

I never read Jakarta Commons' Configuration code, so I didn't have a place to start. I only knew that I didn't want to read and parse XML using DOM. That type of processing, rather, the code for each type of XML document I can have, can be rather long. I had stuff written to read my latest XML documents, but I deleted it after I determined that my little configuration reader tool would do everything I need it to.

This falls under the laziness of a computer scientist, fully documented in The Philosophy, Part I. If I can write something to do something for me automatically, then by God I'm gonna do it. This one is particularly big, since I hate writing XML code every time. Really. I hate it, even in Java! This was the last time I have to do it for static configuration files. And when I update dumb to read and write XML databases, then I won't have to ever do it again. I can't wait, but that's a huge undertaking. I won't be doing it for at least a few months, or as need dictates.

Here's some sample xml (it reads an xml file to configure itself!! I know, it's ironic):

<obj type="map" spec="attributes" class="aproject.domain.attribute.AttributeMap" key="name" />

This says "When you come across the <attributes> tag, create a new AttributeMap and load that S$#@% in it". However, the "attributes" tag will have tags below it that also define objects or lists. So, each tag is defined in my config file, with a class, and can be loaded in, have properties set on it, and everything else. An object can have lists or properties, those objects can have lists or properties, so it's capable of some pretty complex stuff. My "ConfigParser" class, the one that reads all of the configuration files for your project, is only 128 lines though! A line in computer science is really nothing. It's one instruction. This is four of them:

public ConfigParser(NodeMap objs){
this.objs = objs;
map = new ObjectMap();

That's the constructor. Figure in blank lines, "import" declarations, and short lines like that, 128 lines is really nothing at all. And it WORKS!! Many thanks to recursion.

P.S. First game coming soon! Well, a LOT sooner than before I wrote this!

blog comments powered by Disqus