-
Notifications
You must be signed in to change notification settings - Fork 0
Home
It started because of the quoting and escaping hell that can happen when you’re trying to write functional tests in Selenium RC from the Java driver. This is particularly useful when using CSS or XPath locators that include text. See Escape the literal prison for more background.
Taking the idea of using a different encoding scheme further, I thought about interpolation and wondered about prepared statements in SQL. Some quick research turned nothing up, but while chatting with Ola Bini he also mentioned prepared statements, so this convinced me to knock something together that might be useful.
The result is a way to generate quoted, escaped strings for JavaScript from Java. Of course, the resultant strings can also be used in other C-like languages (including Java).
Instead of typing
System.out.println("Hello \"stranger\"!");
you can type (using a static import)
System.out.println(prepared("Hello ?!", "stranger"));
The result is
Hello "stranger"!
String lyric = prepared("Well it's a ? for the ?, ? for the ?", 1, "money", 2, "show");
System.out.println(lyric);
results in
Well it's a 1 for the "money", 2 for the "show"
And let’s say you needed to generate some JavaScript that had strings with double-quotes and backslashes inside them too. You could use single-quotes to help with the craziness, or use the encoding technique described in the blog post mentioned above. You could use PreparedEval to help, or a combination.
You want this displayed in an alert box
Greetings "earthlings", we are from planet Qu"otoid\Backslash. Have you heard of it? Take us to your "leader".
so you need this JavaScript
alert("Greetings \"earthlings\", we are from planet Qu\"otoid\\Backslash. Have you heard of it? Take us to your \"leader\".")
and you can generate it with this Java
String template = "Greetings ?, we are from planet Qu\"otoid\\Backslash. Have you heard of it\\? Take us to your ?.";
String greeting = prepared(template, "earthlings", "leader");
String script = prepared("alert(?)", greeting);
otherwise you would have to use this Java
String script = "alert(\"Greetings \\\"earthlings\\\", we are from planet Qu\\\"otoid\\\\Backslash. Have you heard of it? Take us to your \\\"leader\\\".\")";
The Quoter class is used to escape double-quotes and backslashes in a string, and also to surround the string in double-quotes.
The Preparer class is used to substitute question-marks in a string with the value of given arguments. Preparer uses Quoter for string argument values to escape and, by default, to surround in double-quotes. Non-string arguments are neither escaped nor surrounded in double-quotes, but their toString() method is used for substituting question-marks. Escaped question-marks are not substituted.
Instances of both classes are immutable, and both classes have static convenience methods.
No dependencies
JUnit4 (not included)
The Rhino JavaScript library JAR is included in the source distribution.