Alternatives to java.util.Properties?

I wrote a MVC application that creates web reports from a database. I designed it in a way that allows me to add new reports simply by adding lines to a pair of properties files; one file has the title for each report, and one has the SQL statement for each report (matching keys). I find that this works well, except for one thing. Because the Properties class is implemented as a hash, I have no control over the order when extracting the keys. I don't simply want to sort them, but would rather access them by order of appearance in the files. Any good implementation suggestions for this? I suspect that an XML implementation would be a good approach, except that it seems like a lot of work (not to mention overkill) just to load a few configuration lines. There are currently no more than seven or eight key/value pairs per file, and this might grow to a dozen or so. There is also a properties file for my database connection parameters, but this works fine with the standard Properties implementation. Thanks! PCS

There are probably a number of other implementations of this sort of thing floating around, but here's one I made which uses JDK 1.4's LinkedHashMap and XML parsing:

The main() method shows an example of use. A sample properties file looks like this: Modify as you wish...

"I'm not back." - Bill Harding, Twister

Philip Shanks

Ranch Hand

Posts: 189

posted 16 years ago

Thanks for the hint to use a LinkedHashMap. I've decided to created my own OrderedProperties class based upon this data structure, but that will use the properties files I already have (plain text, "key=value" format) for backwards compatibility. I am also going to incorporate Joshua Bloch's advice to "Favor composition over inheritance," which is Item 14 in "Effective Java." I will be able to swap it right into my current project with minimal disruption. PCS [ November 07, 2002: Message edited by: Philip Shanks ]

The Preferences approach looks interesting, but seems to be geared ever so slightly more toward client-side applications. My interest in this thread was to overcome the Hashtable behavior of the Properties class with minimal re-work to my existing applications that use the current Properties class. Primarily, I did not like the way that the ordering of the Enumeration from propertyNames() method is dictated by the hashing function. While it would be an easy thing to sort the keys alphabetically, I wanted to get the keys in their order of appearance in the properties file. To this end, I have re-implemented the Properties class, based on the java.util.LinkedHashMap data structure. It is a work in progress, though, so I haven't yet implemented all of the methods, rules and behavior found in the original java.util.Properties class. For instance, I haven't yet implemented any capability for reading values that span multiple lines using continuation characters. Among the liberties I am taking:

Given the greater flexibility that XML-based configuration files can afford, I will probably cut development of this class short. It is more of a coding exersize for me anyway, but it does meet a current need as well. For the curious, here is the code. Of course, this is limited to Java 1.4 platforms, but that is not a problem for me. I have tested this code, and so far it works just as expected.

Here is a very simple solution to getting you properties in the order they appear in the properties file, by giving them an order, instead of just implying there is order. You could easily do the following in your properties file key1=value a key2=value b key3=value c Then you could just iterate through the key names in order using a counter. int x=1; while(myProperties.containsKey("key" + x) { something = myProperties.getProperty("key" + x);

x++; }

Jim Yingst

Wanderer

Posts: 18671

posted 16 years ago

Properties? How 20th century! Thanks, Thomas. I knew I'd seen something about a more modern XML-based approach, but forgot where it was. Lots of cool new toys were in 1.4 - I didn't get around to playing with them all. My code was something I'd cooked up previously, and later modifed to use LinkedHashMap rather than HashMap. Aaron - I suppose that would work, if you give up the ability to give the keys meaningful names that tell users what a given property actually does .

Philip and Jim, That's a pretty good thought. However, does this OrderProperties stuff overcome the limitation of multiple values for a single property in multiple lines constraint? For example, I have a file named "config.ini" that has some values like this --- [xml_handlers] handler = handleByTagName handler = handleByAttribute handler = handleByElelement handler = handleByNode I find java properties are not the way to deal this scenario and so going for a custimized collection consisting of a Map and a List to store all keys in Map and values in List. But if this could be dealt in your way then I would be very glad. What do you think?