| 1 | |
package org.webslinger.template.velocity; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.InputStreamReader; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.io.StringReader; |
| 7 | |
import java.io.Writer; |
| 8 | |
import java.util.Collections; |
| 9 | |
import java.util.Iterator; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.lang.reflect.Constructor; |
| 12 | |
|
| 13 | |
import org.apache.bsf.BSFDeclaredBean; |
| 14 | |
import org.apache.bsf.BSFException; |
| 15 | |
import org.apache.bsf.BSFManager; |
| 16 | |
import org.apache.velocity.Template; |
| 17 | |
import org.apache.velocity.VelocityContext; |
| 18 | |
import org.apache.velocity.exception.ParseErrorException; |
| 19 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
| 20 | |
import org.apache.velocity.runtime.RuntimeConstants; |
| 21 | |
import org.apache.velocity.runtime.RuntimeInstance; |
| 22 | |
|
| 23 | |
import org.codehaus.janino.Scanner; |
| 24 | |
|
| 25 | |
import org.webslinger.bsf.ApplyKey; |
| 26 | |
import org.webslinger.bsf.Compiler; |
| 27 | |
import org.webslinger.bsf.CompilingLanguageEngineImpl; |
| 28 | |
import org.webslinger.bsf.EvalKey; |
| 29 | |
import org.webslinger.bsf.ExecKey; |
| 30 | |
import org.webslinger.bsf.Key; |
| 31 | |
import org.webslinger.bsf.janino.AbstractJaninoCompiler; |
| 32 | |
import org.webslinger.io.Charsets; |
| 33 | |
import org.webslinger.template.TemplateMacro; |
| 34 | |
import org.webslinger.template.TemplateManager; |
| 35 | |
import org.webslinger.vfs.VFSDelegate; |
| 36 | |
|
| 37 | 190 | public class VelocityCompiler extends AbstractJaninoCompiler<LocalVelocityTemplate, Class<LocalVelocityTemplate>> { |
| 38 | |
protected RuntimeInstance ri; |
| 39 | |
protected TemplateManager manager; |
| 40 | |
|
| 41 | |
public VelocityCompiler(VelocityTemplateEngine engine) throws BSFException { |
| 42 | 25 | super(engine); |
| 43 | 25 | } |
| 44 | |
|
| 45 | |
public void initialize(BSFManager manager) throws BSFException { |
| 46 | 25 | this.manager = (TemplateManager) manager; |
| 47 | 25 | ri = new RuntimeInstance(); |
| 48 | 25 | ri.addProperty(VelocityCompiler.class.getName(), this); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 25 | ri.addProperty("directive.set.null.allowed", Boolean.TRUE); |
| 56 | 25 | ri.addProperty("runtime.log.invalid.references", Boolean.FALSE); |
| 57 | 25 | ri.addProperty("velocimacro.permissions.allow.inline", Boolean.TRUE); |
| 58 | 25 | ri.addProperty("velocimacro.permissions.allow.inline.local.scope", Boolean.TRUE); |
| 59 | 25 | ri.addProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute"); |
| 60 | 25 | ri.addProperty("runtime.log.logsystem.log4j.logger", VelocityCompiler.class.getName()); |
| 61 | |
try { |
| 62 | 25 | ri.init(); |
| 63 | 0 | } catch (Error e) { |
| 64 | 0 | throw e; |
| 65 | 0 | } catch (RuntimeException e) { |
| 66 | 0 | throw e; |
| 67 | 0 | } catch (Exception e) { |
| 68 | 0 | throw (BSFException) new BSFException("Invalid argument").initCause(e); |
| 69 | 25 | } |
| 70 | 25 | } |
| 71 | |
|
| 72 | |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
| 73 | 967 | if (bean.bean instanceof TemplateMacro) ri.addDirective(new VelocityDirectiveMacro(manager, (TemplateMacro) bean.bean)); |
| 74 | 967 | super.declareBean(bean); |
| 75 | 967 | } |
| 76 | |
|
| 77 | |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
| 78 | 0 | if (bean.bean instanceof TemplateMacro) ri.removeDirective(bean.name); |
| 79 | 0 | super.undeclareBean(bean); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
protected Map<String, BSFDeclaredBean> getBeans() { |
| 83 | 190 | return Collections.emptyMap(); |
| 84 | |
} |
| 85 | |
|
| 86 | |
protected void compileHelper(JaninoCompilerContext context, StringBuilder code, String className) { |
| 87 | 190 | } |
| 88 | |
|
| 89 | |
protected void compileClassVariables(JaninoCompilerContext context, StringBuilder code, String className) { |
| 90 | 190 | } |
| 91 | |
|
| 92 | |
protected void addExtends(JaninoCompilerContext context, StringBuilder code) { |
| 93 | 190 | code.append(" extends org.webslinger.template.velocity.LocalVelocityTemplate"); |
| 94 | 190 | } |
| 95 | |
|
| 96 | |
protected void compileConstructors(JaninoCompilerContext context, StringBuilder code, String className) { |
| 97 | 190 | code.append("\tpublic ").append(className).append("(String[] names, String path, String text) {\n"); |
| 98 | 190 | code.append("\t\tsuper(names, path, text);\n"); |
| 99 | 190 | code.append("\t}\n"); |
| 100 | 190 | code.append('\n'); |
| 101 | 190 | } |
| 102 | |
|
| 103 | |
protected String removeStart(String text, StringBuilder imports, StringBuilder staticDefs) throws IOException, Scanner.LocatedException { |
| 104 | 190 | return text; |
| 105 | |
} |
| 106 | |
|
| 107 | |
public Class<LocalVelocityTemplate> compileKey(ApplyKey<Class<LocalVelocityTemplate>> key, JaninoCompilerContext context) throws BSFException { |
| 108 | 190 | StringBuilder code = context.code; |
| 109 | 190 | code.append("\tpublic void apply(org.apache.bsf.BSFManager manager, Object body, Object[] args) throws Exception {"); |
| 110 | 190 | code.append("\t\tsuper.apply(manager, body, args);\n"); |
| 111 | 190 | code.append("\t}\n"); |
| 112 | 190 | return null; |
| 113 | |
} |
| 114 | |
|
| 115 | |
protected LocalVelocityTemplate newInstance(JaninoCompilerContext context, Class<LocalVelocityTemplate> clz) throws Throwable { |
| 116 | 190 | Constructor<? extends LocalVelocityTemplate> constructor = clz.getConstructor(new Class[] {String[].class, String.class, String.class}); |
| 117 | 190 | ApplyKey key = (ApplyKey) context.key; |
| 118 | 190 | LocalVelocityTemplate value = constructor.newInstance(new Object[] {key.getNames(), absolutePath(key), context.text}); |
| 119 | 190 | constructor = null; |
| 120 | 190 | return init(value); |
| 121 | |
} |
| 122 | |
|
| 123 | |
public Class<LocalVelocityTemplate> compileKey(EvalKey<Class<LocalVelocityTemplate>> key, JaninoCompilerContext context) throws BSFException { |
| 124 | 0 | if (true) throw new IllegalArgumentException(); |
| 125 | |
return null; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public Class<LocalVelocityTemplate> compileKey(ExecKey<Class<LocalVelocityTemplate>> key, JaninoCompilerContext context) throws BSFException { |
| 129 | 0 | if (true) throw new IllegalArgumentException(); |
| 130 | |
return null; |
| 131 | |
} |
| 132 | |
|
| 133 | |
protected void initHelper(CompilerContext context, Class<LocalVelocityTemplate> compiled) throws Throwable { |
| 134 | 190 | } |
| 135 | |
|
| 136 | |
protected LocalVelocityTemplate init(LocalVelocityTemplate template) throws BSFException { |
| 137 | 190 | template.setRuntimeServices(ri); |
| 138 | |
try { |
| 139 | 190 | template.process(); |
| 140 | 0 | } catch (Error e) { |
| 141 | 0 | throw e; |
| 142 | 0 | } catch (RuntimeException e) { |
| 143 | 0 | throw e; |
| 144 | 0 | } catch (Exception e) { |
| 145 | 0 | throw (BSFException) new BSFException("Invalid argument").initCause(e); |
| 146 | 190 | } |
| 147 | 190 | return template; |
| 148 | |
} |
| 149 | |
} |
| 150 | |
|