-
-
Save wenbing/8256305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.linkedin.dust.renderer; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.io.Writer; | |
import org.mozilla.javascript.Context; | |
import org.mozilla.javascript.JavaScriptException; | |
import org.mozilla.javascript.Scriptable; | |
public class DustEngine | |
{ | |
public static final String MODULE = DustEngine.class.getName(); | |
private static Scriptable globalScope; | |
public DustEngine(InputStream dustStream) | |
{ | |
try | |
{ | |
Reader dustReader = new InputStreamReader(dustStream, "UTF-8"); | |
Context dustEngineContext = Context.enter(); | |
dustEngineContext.setOptimizationLevel(9); | |
try | |
{ | |
globalScope = dustEngineContext.initStandardObjects(); | |
dustEngineContext.evaluateReader(globalScope, | |
dustReader, | |
"dust-compile.js", | |
0, | |
null); | |
} | |
finally | |
{ | |
Context.exit(); | |
} | |
} | |
catch (IOException ex) | |
{ | |
throw new RuntimeException(" ERROR : Unable to load dust engine resource: ", ex); | |
} | |
} | |
public static String compileTemplate(String name, String rawSource) | |
{ | |
Context dustContext = Context.enter(); | |
try | |
{ | |
Scriptable compileScope = dustContext.newObject(globalScope); | |
compileScope.setParentScope(globalScope); | |
compileScope.put("rawSource", compileScope, rawSource); | |
compileScope.put("name", compileScope, name); | |
try | |
{ | |
return (String) dustContext.evaluateString(compileScope, | |
"(dust.compile(rawSource, name))", | |
"JDustCompiler", | |
0, | |
null); | |
} | |
catch (JavaScriptException e) | |
{ | |
// Fail hard on any compile time error for dust templates | |
throw new RuntimeException(e); | |
} | |
} | |
finally | |
{ | |
Context.exit(); | |
} | |
} | |
public static void loadTemplate(String name, String rawSource) | |
{ | |
Context dustContext = Context.enter(); | |
try | |
{ | |
Scriptable compileScope = dustContext.newObject(globalScope); | |
compileScope.setParentScope(globalScope); | |
compileScope.put("rawSource", compileScope, rawSource); | |
compileScope.put("name", compileScope, name); | |
try | |
{ | |
dustContext.evaluateString(compileScope, | |
"(dust.loadSource(dust.compile(rawSource, name)))", | |
"JDustCompiler", | |
0, | |
null); | |
} | |
catch (JavaScriptException e) | |
{ | |
// Fail hard on any compile time error for dust templates | |
throw new RuntimeException(e); | |
} | |
} | |
finally | |
{ | |
Context.exit(); | |
} | |
} | |
public static void render(String name, String json, Writer writer) | |
{ | |
Context dustContext = Context.enter(); | |
Scriptable renderScope = dustContext.newObject(globalScope); | |
renderScope.setParentScope(globalScope); | |
String renderScript = | |
("{ dust.render( name, JSON.parse(json) , function( err, data) { if(err) { writer.write(err);} else { writer.write( data );} } ); }"); | |
try | |
{ | |
renderScope.put("writer", renderScope, writer); | |
renderScope.put("json", renderScope, json); | |
renderScope.put("name", renderScope, name); | |
dustContext.evaluateString(renderScope, | |
renderScript, | |
"JDustCompiler", | |
0, | |
null); | |
} | |
catch (JavaScriptException e) | |
{ | |
// Fail hard on any render time error for dust templates | |
throw new RuntimeException(e); | |
} | |
finally | |
{ | |
Context.exit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment