• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how to check validate content of a resourceBundle ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 5474
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Hug your destiny! And hug this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic