| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
package org.webmacro.util; |
| 6 | |
|
| 7 | |
import org.webmacro.Context; |
| 8 | |
import org.webmacro.Template; |
| 9 | |
import org.webmacro.WM; |
| 10 | |
import org.webmacro.WebMacro; |
| 11 | |
import org.webmacro.engine.StringTemplate; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class WMStringEval |
| 19 | |
{ |
| 20 | |
|
| 21 | |
private Context context; |
| 22 | |
private WebMacro wm; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
public WMStringEval() |
| 28 | 0 | { |
| 29 | |
|
| 30 | |
try |
| 31 | |
{ |
| 32 | 0 | wm = new WM(); |
| 33 | 0 | context = wm.getContext(); |
| 34 | |
} |
| 35 | 0 | catch (Exception e) |
| 36 | |
{ |
| 37 | 0 | e.printStackTrace(System.err); |
| 38 | 0 | throw new IllegalStateException(e.toString()); |
| 39 | 0 | } |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public Context getCurrentContext() |
| 47 | |
{ |
| 48 | 0 | return context; |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public Context getNewContext() |
| 56 | |
{ |
| 57 | 0 | context = wm.getContext(); |
| 58 | 0 | return context; |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void addContextReference(String name, Object value) |
| 67 | |
{ |
| 68 | 0 | context.put(name, value); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public String eval(String template) throws Exception |
| 78 | |
{ |
| 79 | 0 | Template t = new StringTemplate(wm.getBroker(), template, "anonymous"); |
| 80 | 0 | return t.evaluateAsString(context); |
| 81 | |
} |
| 82 | |
|
| 83 | |
} |