views:

512

answers:

3

Hi,

we are getting properties, we can not influence, out of a database and want to access them by a key/value mapping. We are facing the problem, that one of the property keys includes a blank character.

foo bar = barefoot

This is - correctly - interpreted as follows

key: foo
value: bar = barefoot

Is there a way to include the blank in the key so that it's not interpreted as the delimiter? I guess this behaviour is just like intended, but I thought I could give it a try here. :-)

Thank you.

+2  A: 

Maybe you can escape the whitespaces: foo\ bar = barefoot

Edit: Oops, I did not see that you can't change the properties.

Petar Minchev
No maybe about it: http://pastie.org/787892 Produces http://pastie.org/787893
T.J. Crowder
+2  A: 

I assume by "properties", you mean a Java property file (as written/read by java.util.Properties).

Then, as you write yourself,

foo bar = barefoot

must indeed be interpreted as

   key: foo
   value: bar = barefoot

There's no way to configure this using the built-in Properties class. You must either manipulate your input (escape the whitespace, change it to _ and back...), or write your own parser. Writing your own parser is probably better, as obviously your input isn't really a Java properties file to begin with :-).

sleske
+1 "not really a Java properties file to begin with". People see "key=value" and assume it's a properties file. They forget that there are quite a lot of rules around properties file, and if your input doesn't follow **all** of them, then it's not a properties file. Another example: Java properties use Latin1 (a.k.a ISO-8859-1) encoding by default and support Unicode escapes.
Joachim Sauer
+2  A: 

As it seems the delimiter should be =, not space. Hence - keyValuePair.split("=") should do.

If you are loading this from a java .properties file, then you can extend java.util.Properties and override this method

public synchronized void load(InputStream inStream) throws IOException

so that it parses the properties correctly.

Bozho
Hm, overriding methods in `Properties` seems dubios at best. There are probably many hardcoded assumptions in the Properties class, so it's a source of future bugs. If it's not a proper Properties file, i'd rather not use Properties to read it.
sleske
At least a try should be given, because it would save a lot of time. If it doesn't work - create a separate properties parse of course
Bozho