| 1 | |
package org.webslinger.ext.quercus.template; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import java.util.Map; |
| 7 | |
|
| 8 | |
import com.caucho.quercus.env.Env; |
| 9 | |
import com.caucho.quercus.env.JavaValue; |
| 10 | |
import com.caucho.quercus.env.Value; |
| 11 | |
import com.caucho.quercus.env.Var; |
| 12 | |
import com.caucho.quercus.module.AbstractQuercusModule; |
| 13 | |
import com.caucho.quercus.program.AbstractFunction; |
| 14 | |
import com.caucho.vfs.StreamPrintWriter; |
| 15 | |
import com.caucho.vfs.WriterStreamImpl; |
| 16 | |
|
| 17 | |
import org.apache.bsf.BSFManager; |
| 18 | |
import org.webslinger.template.TemplateMacro; |
| 19 | |
import org.webslinger.template.TemplateManager; |
| 20 | |
|
| 21 | |
public class WebslingerTemplateModule extends AbstractQuercusModule { |
| 22 | 6 | public WebslingerTemplateModule() { |
| 23 | 6 | } |
| 24 | |
|
| 25 | |
public String[] getLoadedExtensions() { |
| 26 | 3 | return new String[] { "webslinger-template" }; |
| 27 | |
} |
| 28 | |
|
| 29 | |
public void WS_CallMacro(final Env env, String name, Value funcName, Value[] args) { |
| 30 | 0 | BSFManager manager = (BSFManager) ((JavaValue) env.getValue("bsf")).toJavaObject(); |
| 31 | 0 | TemplateMacro macro = (TemplateMacro) manager.lookupBean(name); |
| 32 | 0 | if (macro == null) return; |
| 33 | |
TemplateMacro.Body body; |
| 34 | 0 | final Object source = env.getValue("BSFParent").toJavaObject(); |
| 35 | 0 | if (macro.isBlock()) { |
| 36 | 0 | final AbstractFunction func = env.getFunction(funcName.toString()); |
| 37 | 0 | body = new TemplateMacro.Body() { |
| 38 | |
public Object getSource() { |
| 39 | 0 | return source; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public boolean render(Writer writer, Map<String, Object> context) throws IOException { |
| 43 | 0 | for (Map.Entry<String, Object> entry: context.entrySet()) { |
| 44 | 0 | env.setValue(entry.getKey(), env.wrapJava(entry.getValue())); |
| 45 | |
} |
| 46 | 0 | env.pushOutputBuffer(null, 8192, false); |
| 47 | 0 | WriterStreamImpl writerImpl = new WriterStreamImpl(); |
| 48 | 0 | writerImpl.setWriter(writer); |
| 49 | 0 | env.getOut().init(writerImpl); |
| 50 | |
try { |
| 51 | 0 | func.call(env); |
| 52 | |
} finally { |
| 53 | 0 | env.popOutputBuffer(); |
| 54 | 0 | } |
| 55 | 0 | return false; |
| 56 | |
} |
| 57 | |
}; |
| 58 | 0 | } else { |
| 59 | 0 | body = null; |
| 60 | |
} |
| 61 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
| 62 | 0 | for (Map.Entry<String, Var> entry: env.getEnv().entrySet()) { |
| 63 | 0 | context.put(entry.getKey(), entry.getValue().toJavaObject()); |
| 64 | |
} |
| 65 | 0 | Object[] pList = new Object[args.length]; |
| 66 | 0 | for (int i = 0; i < pList.length; i++) { |
| 67 | 0 | pList[i] = args[i].toJavaObject(); |
| 68 | |
} |
| 69 | |
try { |
| 70 | 0 | macro.render((TemplateManager) manager, source, new StreamPrintWriter(env.getOut()), new TemplateMacro.Args(pList, null), context, body); |
| 71 | 0 | } catch (RuntimeException e) { |
| 72 | 0 | throw e; |
| 73 | 0 | } catch (Exception e) { |
| 74 | 0 | throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); |
| 75 | 0 | } |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public void WS_FunctionBootstrap(Env env) { |
| 79 | 0 | for (Map.Entry<String, Var> entry: env.getGlobalEnv().entrySet()) { |
| 80 | 0 | env.setValue(entry.getKey(), entry.getValue()); |
| 81 | |
} |
| 82 | 0 | } |
| 83 | |
} |