/
CoffeeScript.java
49 lines (40 loc) · 1.74 KB
/
CoffeeScript.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.github.searls.jasmine.coffee;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.searls.jasmine.io.IOUtilsWrapper;
import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
public class CoffeeScript {
private static Map<String, String> cache = Collections.synchronizedMap(new WeakHashMap<String, String>());
private final ThreadLocal<HtmlPage> htmlPage = new ThreadLocal<HtmlPage>() {
@Override
protected HtmlPage initialValue() {
MockWebConnection webConnection = new MockWebConnection();
WebClient webClient = new WebClient();
webClient.setWebConnection(webConnection);
try {
HtmlPage page = webClient.getPage(WebClient.URL_ABOUT_BLANK);
page.executeJavaScript(ioUtilsWrapper.toString("/vendor/js/coffee-script.js"));
return page;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
private final IOUtilsWrapper ioUtilsWrapper = new IOUtilsWrapper();
public String compile(String coffee) throws IOException {
String escapedCoffee = StringEscapeUtils.escapeEcmaScript(coffee);
return cache.containsKey(escapedCoffee) ? cache.get(escapedCoffee) : compileAndCache(escapedCoffee);
}
private String compileAndCache(String input) {
ScriptResult scriptResult = htmlPage.get().executeJavaScript(String.format("CoffeeScript.compile(\"%s\");", input));
String result = (String) scriptResult.getJavaScriptResult();
cache.put(input, result);
return result;
}
}