50

We are getting properties (that 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.

1

5 Answers 5

184

You can escape every thing in properties file with Java Unicode:

  • \u003d for =
  • \u0020 for whitespace

For example:

foo bar = barefoot

must be:

foo\u0020bar\u0020=\u0020barefoot

So will be:

key: "foo bar "
value: " barefoot"
Sign up to request clarification or add additional context in comments.

5 Comments

This is the "Correct" answer. All other options will end up with non standard solutions. PLEASE use this answer :)
Note that this seems not to work in Grails configuration files (not .groovy but .properties; tested on 2.5.1).
Good solution. Note however that OP writes they "can not influence" the properties they get, so escaping may not be practical. Of course maybe it's possible to have some preprocessing step to do it...
Agree this is the correct answer assuming you can modify the properties file.
If the OP could change the properties file, then he could simply make it foo.bar instead of foo bar avoid the rather messy use of Unicode characters. However, the OP stated that properties could not be altered so this doesn't solve the given issue.
16

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

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

1 Comment

Escaping whitespaces will not work with most editors that trim spaces at the end of lines by default - think of a mainframe parameter that needs trailing spaces. Correct is Veaceslav's solution.
11

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.

3 Comments

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.
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
From the doc: "The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator." So the space is documented as a delimiter, so I don't understand your statement "the delimiter should be =, not space.". The spec says space is a valid delimiter, so why "should" it be =?
4

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 :-).

3 Comments

+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.
Writing your own parser would need to take care of comments block in .properties too.
@OmSao: True. That nicely illustrates the problem that it's important to make sure you understand exactly what the file can or cannot contain.
0
keyValuePair = keyValuePair.substring(0,indexOf("=")).replaceAll("\\s+") + 
               keyValuePair.substring(indexOf("="));  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.