Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Properties ignoring case  RSS feed

 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get properties out of a Properties object using getProperty(..) that ignores the case of the key?

I know I could parse the file manually, but that's a last resort.

Dave
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I can think of doing that would be to use the propertyNames() method to get an Enumeration of all the property keys, then iterate through them comparing each one with the desired key using String's equalsIgnoreCase(String) method until you find the correct one, then call getProperties(String) with the actual key.

This of course would be an expensive operation, especially on a Property instance with a large number of keys. Nonetheless, it is easier then parsing the file manually.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd try something like this:

This converts all keys to lowercase when they are first added. Then when getProperty() is called, only the new target key needs to be converted to lowercase - all the existing keys already are lowercase. Note that I overrode put() rather than setProperty() because I wanted to ahndle what happens during a load() - which calls put(), but not setProperty(). I didn't test this extensively though, so you may need to experiment some more. The Properties class isn't really designed to be overridden, because it doesn't always document the effects of overriding its methods. You might be better off creating a new Map implementation, which simply forwards most of its calls to an enclosed HashMap instance. If you're using JDK 5.0 you can make a Map<String, String> and save yourself some casting. You might also need to write your own load() and store() methods, but that needn't be too difficult.
[ January 11, 2005: Message edited by: Jim Yingst ]
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

like that one, reckon that'll do the job nicely.

Many thanks

Regards

Dave
[ January 12, 2005: Message edited by: Dave Hewy ]
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

that's an interesing thought, the file isn't massive, so that would be feasible.

Thanks

Dave.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to change the properties files - you have to change the code that uses them. The CaselessProperties still has a load() method inherited from Properties. This works just like before, except that when it calls put(), it gets the overriding method in CaselessProperties which ensures that what is stored as the key in memory is lowercase, regardless of what the file said. If you ever call store() later, you will get a new properties file with lower-case keys, otherwise identical to the original. (Well, except that everything will probably be in a different order than it was before, but that's unfortunately a standard problem with the Properties class, because it stores things in hashcode order.)
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jim,

got the wrong end of the stick before.

I have changed my earlier post.

Thanks

Dave
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

Cool idea. Experience comes through once again.

Regards,
Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!