Skip to content

Instantly share code, notes, and snippets.

@jmsktm
Forked from vybs/rhino_dust.java
Created July 14, 2013 08:11
Show Gist options
  • Save jmsktm/5993591 to your computer and use it in GitHub Desktop.
Save jmsktm/5993591 to your computer and use it in GitHub Desktop.
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