• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to check validate content of a resourceBundle ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have code that retrieves one of my property file like this:

messages.properties file looks like :

How could I check the content of messages.properties file and raise an Exception if I would have a specific character ("_" in my case) in the value part, like that :

I don't know at what level/how I can maybe override the behaviour of getBundle (that's one idea) ?
Thanks for your help.
 
Saloon Keeper
Posts: 12143
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? Resource bundles are usually internal to your application. You control them. If there is a mistake in them, that's a bug. Throwing an exception is a mistake.
 
jacques dusieur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes they are internal to my application, that doesn't prevent me from wanting to write a safe-code onto what format we expect the content of a property file.
But if you don't know how to do it, no one will be mad at you, just avoid answering for nothing please.
 
Stephan van Hulst
Saloon Keeper
Posts: 12143
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm answering to help you with good software design.

Code to check that your application is correct should be in unit tests and integration tests, not in the business logic. Do you also write code to check that return values from method calls are correct? Methods guarantee correct results if the input is correct. Resource bundles should guarantee that their contents are correct. They are part of your application, not part of the application input.

Writing code like this leads to bloated, poorly maintainable applications.

Take my advice or leave it, I won't be mad at you.
 
jacques dusieur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I am now interested by the technical aspect of it. Is it possible ? If yes, does anyone has an idea about how to do it?
Regards
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will be appropriate to check the values in your application code. At the place where you are retrieving value for a particular key, we can add a check that if the value contains "_" then throw Invalid property exception. Else there is option of extending ResourceBundle class and override handleGetObject() and getKeys() method to provide your own implementation, but I still prefer first option as this will be easy to handle and debug.
 
Stephan van Hulst
Saloon Keeper
Posts: 12143
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just read all entries from the resource bundle using handleKeySet() and getString(), and using String.contains() to check for illegal characters. Just make sure there aren't actually labels that are supposed to contain these characters.
 
Sheriff
Posts: 4870
317
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resource Bundles exist to externalise the message Strings from your source code to enable you to change them without having to create a new build of your application. If you have a programatic runtime dependency on the value of those message properties, then that String could just as well be hard coded into your application. If you have business logic written around the value of those message properties, then you're misusing them. Resource Bundles should contain Strings for presentation on the UI only. Otherwise what you have is a configuration value, which should be stored in configuration files not resource bundles.
 
Stephan van Hulst
Saloon Keeper
Posts: 12143
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bhushan Bhawsar wrote:I think it will be appropriate to check the values in your application code.



Like I mentioned earlier, it's not appropriate. Appropriate would be to write a unit test like this:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic