Created
November 24, 2012 14:52
-
-
Save KAYLukas/4139978 to your computer and use it in GitHub Desktop.
original code
This file contains 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package org.docpresso.templating; | |
import org.docpresso.util.Tuples.Pair; | |
import org.mozilla.javascript.Context; | |
import org.mozilla.javascript.Function; | |
import org.mozilla.javascript.Scriptable; | |
import org.mozilla.javascript.ScriptableObject; | |
import org.mozilla.javascript.annotations.JSFunction; | |
/** | |
* | |
* @author Kay | |
*/ | |
public class TemplateScope extends ScriptableObject { | |
private StringBuilder log = new StringBuilder(); | |
private StringBuilder out = new StringBuilder(); | |
private TemplateScope() { | |
//context.initStandardObjects(this); | |
} | |
public static TemplateScope init(Context context) { | |
TemplateScope t = new TemplateScope(); | |
context.initStandardObjects(t); | |
JSFunctions.init(t); | |
return t; | |
} | |
@Override | |
public String getClassName() { | |
return "TemplateScope"; | |
} | |
public Pair<String, String> pullOutput() { | |
String out_str = out.toString(); | |
out.setLength(0); | |
String log_str = log.toString(); | |
log.setLength(0); | |
return new Pair(out_str, log_str); | |
} | |
/** | |
* Defines the JSfunction which modify the TemplateScope | |
*/ | |
private static class JSFunctions { | |
public static final String[] METHODS = | |
new String[]{"print", "log", "printLine", "logLine"}; | |
public static void init(TemplateScope ts) { | |
int modifiers = ScriptableObject.READONLY | ScriptableObject.PERMANENT | ScriptableObject.CONST; | |
ts.defineFunctionProperties(JSFunctions.METHODS, JSFunctions.class, modifiers); | |
} | |
public static void print(Context cx, Scriptable thisObj, Object[] args, | |
Function funObj) { | |
TemplateScope self = (TemplateScope) thisObj; | |
for (Object arg : args) { | |
self.out.append(arg.toString()); | |
} | |
} | |
public static void printLine(Context cx, Scriptable thisObj, Object[] args, | |
Function funObj) { | |
TemplateScope self = (TemplateScope) thisObj; | |
for (Object arg : args) { | |
self.out.append(arg.toString()); | |
} | |
self.out.append("\n"); | |
} | |
public static void log(Context cx, Scriptable thisObj, Object[] args, | |
Function funObj) { | |
TemplateScope self = (TemplateScope) thisObj; | |
for (Object arg : args) { | |
self.log.append(arg.toString()); | |
} | |
} | |
public static void logLine(Context cx, Scriptable thisObj, Object[] args, | |
Function funObj) { | |
TemplateScope self = (TemplateScope) thisObj; | |
for (Object arg : args) { | |
self.log.append(arg.toString()); | |
} | |
self.log.append("\n"); | |
} | |
} | |
public static void main(String... args) { | |
Context context; | |
try { | |
context = Context.enter(); | |
TemplateScope scope = TemplateScope.init(context); | |
context.evaluateString(scope, "print(\"hallo\",' ' ,'wereld!'); log(\"tralala\");", "test source", 1, null); | |
Pair<String, String> result = scope.pullOutput(); | |
System.out.println("out is: " + result.a); | |
System.out.println("log is: " + result.b); | |
result = scope.pullOutput(); | |
System.out.println("out is: " + result.a); | |
System.out.println("log is: " + result.b); | |
context.evaluateString(scope, "log(\"test\" + \"\\n\");", "test source", 1, null); | |
result = scope.pullOutput(); | |
System.out.println("out is: " + result.a); | |
System.out.println("log is: " + result.b); | |
} finally { | |
Context.exit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment