public
Created

original code

  • Download Gist
gistfile1.txt
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/*
* 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();
}
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.