| 1 | |
package org.webslinger.ext.quercus.template; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.OutputStreamWriter; |
| 5 | |
import java.util.HashMap; |
| 6 | |
|
| 7 | |
import org.apache.bsf.BSFDeclaredBean; |
| 8 | |
import org.apache.bsf.BSFException; |
| 9 | |
|
| 10 | |
import org.apache.commons.vfs.FileObject; |
| 11 | |
|
| 12 | |
import com.caucho.quercus.Quercus; |
| 13 | |
import com.caucho.quercus.env.Env; |
| 14 | |
import com.caucho.quercus.env.QuercusClass; |
| 15 | |
import com.caucho.quercus.env.Value; |
| 16 | |
import com.caucho.quercus.page.QuercusPage; |
| 17 | |
import com.caucho.quercus.program.QuercusProgram; |
| 18 | |
import com.caucho.quercus.program.ClassDef; |
| 19 | |
import com.caucho.vfs.Path; |
| 20 | |
import com.caucho.vfs.StringPath; |
| 21 | |
import com.caucho.vfs.Vfs; |
| 22 | |
|
| 23 | |
import org.webslinger.PathContext; |
| 24 | |
import org.webslinger.Webslinger; |
| 25 | |
import org.webslinger.bsf.ApplyKey; |
| 26 | |
import org.webslinger.bsf.Compiler; |
| 27 | |
import org.webslinger.bsf.EvalKey; |
| 28 | |
import org.webslinger.bsf.ExecKey; |
| 29 | |
import org.webslinger.bsf.Key; |
| 30 | |
import org.webslinger.util.GeneratedResult; |
| 31 | |
|
| 32 | |
import org.webslinger.ext.quercus.VFSDelegatePath; |
| 33 | |
import org.webslinger.ext.quercus.LocalEnv; |
| 34 | |
|
| 35 | 6 | public final class QuercusTemplateCompiler extends Compiler<QuercusPage, QuercusPage> { |
| 36 | 1 | private static final HashMap<Class, String> compiledClassNamesMap = new HashMap<Class, String>(); |
| 37 | |
static { |
| 38 | 1 | compiledClassNamesMap.put(ApplyKey.class, "BSFQuercusApply"); |
| 39 | 1 | compiledClassNamesMap.put(EvalKey.class, "BSFQuercusEval"); |
| 40 | 1 | compiledClassNamesMap.put(ExecKey.class, "BSFQuercusExec"); |
| 41 | 1 | } |
| 42 | |
|
| 43 | |
private final QuercusTemplateEngine phpEngine; |
| 44 | |
|
| 45 | |
public QuercusTemplateCompiler(QuercusTemplateEngine engine) { |
| 46 | 3 | super(engine); |
| 47 | 3 | phpEngine = engine; |
| 48 | 3 | } |
| 49 | |
|
| 50 | |
public Quercus getQuercus() { |
| 51 | 3 | return phpEngine.getQuercus(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
| 55 | 107 | } |
| 56 | |
|
| 57 | |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
protected CompilerContext newContext(Key<QuercusPage> key) throws BSFException { |
| 61 | 3 | return new CompilerContext(key); |
| 62 | |
} |
| 63 | |
|
| 64 | |
public QuercusPage compile(CompilerContext context) throws Throwable { |
| 65 | 3 | Quercus php = getQuercus(); |
| 66 | 3 | synchronized (php) { |
| 67 | 3 | return php.parse(getPath(context.key.getId())); |
| 68 | 0 | } |
| 69 | |
} |
| 70 | |
|
| 71 | |
protected QuercusPage init(CompilerContext context, QuercusPage compiled) { |
| 72 | 3 | return compiled; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public Env getGlobalEnv() { |
| 76 | 21 | return phpEngine.getGlobalEnv(); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public LocalEnv newEnv() throws Throwable { |
| 80 | 0 | LocalEnv env = new LocalEnv(getQuercus(), null, null, null); |
| 81 | 0 | env.getWriterStreamImpl().setWriter(new OutputStreamWriter(System.err)); |
| 82 | 0 | return env; |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected Path getPath(Object body) throws Exception { |
| 86 | 3 | return new VFSDelegatePath(phpEngine.getVFSDelegate(), body); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public QuercusPage compileKey(ApplyKey<QuercusPage> key, CompilerContext context) throws BSFException { |
| 90 | 0 | throw new InternalError(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
public QuercusPage compileKey(EvalKey<QuercusPage> key, CompilerContext context) throws BSFException { |
| 94 | 0 | throw new InternalError(); |
| 95 | |
} |
| 96 | |
|
| 97 | |
public QuercusPage compileKey(ExecKey<QuercusPage> key, CompilerContext context) throws BSFException { |
| 98 | 0 | throw new InternalError(); |
| 99 | |
} |
| 100 | |
|
| 101 | |
public Object wrapForArgs(int i, Object object, Class type) { |
| 102 | 15 | if (i < 2) return object; |
| 103 | 9 | return wrap(object, type); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public Object wrap(Object object, Class type) { |
| 107 | 21 | return getGlobalEnv().wrapJava(object); |
| 108 | |
} |
| 109 | |
|
| 110 | |
public Object unwrap(Object object) { |
| 111 | 3 | return ((Value) object).toJavaObject(); |
| 112 | |
} |
| 113 | |
} |