Escaping quotes to the nth degree

I'm having big troubles with escaping quotes.

Here's what I'm trying to do:

In my Java properties file, there may be a value that contains a URL with quotes. In this case it's a JavaScript function that opens a URL in a new window. Within those quotes, the actual URL should be inserted, for instance:

# Open the story in a new window

link.story = javascript:open_window('{0}', '{1}', 546, 400, false);

Now, I tried MessageFormat.format, and it works, *but* it also deletes my quotes, leaving me with something like

javascript:open_window(http://java.sun.com, myNewWindow, 546, 400, false);

I then tried escaping all quotes with String.replaceAll, however nothing I do works. If I use

string.replaceAll("\'", "\\\\'");

it escapes quotes (I'm trying just about every number of backslashes, too!), resulting in

javascript:open_window(\'{0}\', \'{1}\', 546, 400, false);

However, after MessageFormat.format, I get

javascript:open_window({0}, {1}, 546, 400, false);

So, the big question is: What exactly do I need to be able to write quotes "naturally" in the properties file and then get what I need, namely

javascript:open_window('http://java.sun.com', 'myNewWindow', 546, 400, false);

Any help would be greatly appreciated!

Regards,

Eric

[1364 byte] By [eric_lewis] at [2007-9-27 21:42:45]
# 1
Just use double quotes for the whole string and it won't omit the single quotes.javascript:open_window(" ' http://java.sun.com' "," 'myNewWindow' ", 546, 400, false);
JustLee at 2007-7-7 3:41:15 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Sorry, that doesn't work...

If I write the following String into the properties file:

targetWindow.sendStory = javascript:open_win("'{0}'", 'SendStory', 546, 400, false);

I get the following output after using messageFormat.format:

javascript:open_win("{0}", SendStory, 546, 400, false);

eric_lewis at 2007-7-7 3:41:15 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

Try to double all your single quotes. I had a similar problem and if I correctly remember this was the solution.

# Open the story in a new window

link.story = javascript:open_window(''{0}'', ''{1}'', 546, 400, false);

Hope this helps,

Pierre

PS: Note that in the example above I use two single quotes (''), not a double-quote (").

pierrepost at 2007-7-7 3:41:15 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

Ok, with the help of a colleague, I found a much simpler way: I won't use single quotes, but double quotes. JavaScript doesn't care about the type of quote.

So now I'm using:

targetWindow.sendStory = javascript:open_win("{0}", "SendStory", 546, 400, false);

This solves my problem, but not the issue with single quotes as such...

eric_lewis at 2007-7-7 3:41:15 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

Ok, my solution should definitively work. I have found the following explanation in the Java API documentation of the MessageFormat class:

In strings, single quotes can be used to quote the "{" (curly brace) if necessary. A real single quote is represented by ''.

Only wanted to add that. However, your solution is ok too.

pierrepost at 2007-7-7 3:41:15 > top of Java-index,Archived Forums,New To Java Technology Archive...