1 20 21 package org.netbeans.libs.freemarker; 22 23 24 import javax.script.*; 25 import java.io.*; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import freemarker.template.*; 29 import org.openide.filesystems.FileObject; 30 31 36 class FreemarkerEngine extends AbstractScriptEngine { 37 38 public static final String STRING_OUTPUT_MODE = "com.sun.script.freemarker.stringOut"; 39 public static final String FREEMARKER_CONFIG = "com.sun.script.freemarker.config"; 40 public static final String FREEMARKER_PROPERTIES = "com.sun.script.freemarker.properties"; 41 public static final String FREEMARKER_TEMPLATE_DIR = "com.sun.script.freemarker.template.dir"; 42 public static final String FREEMARKER_TEMPLATE = "org.openide.filesystems.FileObject"; 43 44 private volatile ScriptEngineFactory factory; 46 private volatile Configuration conf; 47 48 public FreemarkerEngine(ScriptEngineFactory factory) { 49 this.factory = factory; 50 } 51 52 public FreemarkerEngine() { 53 this(null); 54 } 55 56 public Object eval(String str, ScriptContext ctx) 58 throws ScriptException { 59 return eval(new StringReader(str), ctx); 60 } 61 62 public Object eval(Reader reader, ScriptContext ctx) 63 throws ScriptException { 64 ctx.setAttribute("context", ctx, ScriptContext.ENGINE_SCOPE); 65 initFreeMarkerConfiguration(ctx); 66 String fileName = getFilename(ctx); 67 boolean outputAsString = isStringOutputMode(ctx); 68 Writer out; 69 if (outputAsString) { 70 out = new StringWriter(); 71 } else { 72 out = ctx.getWriter(); 73 } 74 Bindings engineScope = ctx.getBindings(ScriptContext.ENGINE_SCOPE); 75 76 try { 77 Template template = new Template(fileName, reader, conf); 78 template.process(engineScope, out); 79 out.flush(); 80 } catch (Exception exp) { 81 throw new ScriptException(exp); 82 } 83 return outputAsString? out.toString() : null; 84 } 85 86 public ScriptEngineFactory getFactory() { 87 if (factory == null) { 88 synchronized (this) { 89 if (factory == null) { 90 factory = new FreemarkerFactory(); 91 } 92 } 93 } 94 return factory; 95 } 96 97 public Bindings createBindings() { 98 return new SimpleBindings(); 99 } 100 101 private static String getFilename(ScriptContext ctx) { 103 Object fileName = ctx.getAttribute(ScriptEngine.FILENAME); 104 return fileName != null? fileName.toString() : "<unknown>"; 105 } 106 107 private static boolean isStringOutputMode(ScriptContext ctx) { 108 Object flag = ctx.getAttribute(STRING_OUTPUT_MODE); 109 if (flag != null) { 110 return flag.equals(Boolean.TRUE); 111 } else { 112 return false; 113 } 114 } 115 116 private void initFreeMarkerConfiguration(ScriptContext ctx) { 117 if (conf == null) { 118 synchronized (this) { 119 if (conf != null) { 120 return; 121 } 122 Object cfg = ctx.getAttribute(FREEMARKER_CONFIG); 123 if (cfg instanceof Configuration) { 124 conf = (Configuration) cfg; 125 return; 126 } 127 128 Object tfo = ctx.getAttribute(FREEMARKER_TEMPLATE); 129 FileObject fo = tfo instanceof FileObject ? (FileObject)tfo : null; 130 131 Configuration tmpConf = new RsrcLoader(fo, ctx); 132 try { 133 initConfProps(tmpConf, ctx); 134 initTemplateDir(tmpConf, fo, ctx); 135 } catch (RuntimeException rexp) { 136 throw rexp; 137 } catch (Exception exp) { 138 throw new RuntimeException (exp); 139 } 140 conf = tmpConf; 141 } 142 } 143 } 144 145 private static void initConfProps(Configuration conf, ScriptContext ctx) { 146 try { 147 Properties props = null; 148 Object tmp = ctx.getAttribute(FREEMARKER_PROPERTIES); 149 if (props instanceof Properties ) { 150 props = (Properties ) tmp; 151 } else { 152 String propsName = System.getProperty(FREEMARKER_PROPERTIES); 153 if (propsName != null) { 154 File propsFile = new File(propsName); 155 if (propsFile.exists() && propsFile.canRead()) { 156 props = new Properties (); 157 props.load(new FileInputStream(propsFile)); 158 } 159 } 160 } 161 if (props != null) { 162 Set <Object > keys = props.keySet(); 163 for (Object obj : keys) { 164 String key; 165 if (obj instanceof String ) { 166 key = (String ) obj; 167 } else { 168 continue; 169 } 170 try { 171 conf.setSetting(key, props.get(key).toString()); 172 } catch (TemplateException te) { 173 } 175 } 176 } 177 } catch (RuntimeException re) { 178 throw re; 179 } catch (Exception exp) { 180 throw new RuntimeException (exp); 181 } 182 } 183 184 private static void initTemplateDir(Configuration conf, FileObject fo, ScriptContext ctx) { 185 try { 186 Object tmp = ctx.getAttribute(FREEMARKER_TEMPLATE_DIR); 187 String dirName; 188 if (tmp != null) { 189 dirName = tmp.toString(); 190 } else { 191 if (fo != null) { 192 return; 193 } 194 tmp = System.getProperty(FREEMARKER_TEMPLATE_DIR); 195 dirName = (tmp == null)? "." : tmp.toString(); 196 } 197 File dir = new File(dirName); 198 if (dir.exists() && dir.isDirectory()) { 199 conf.setDirectoryForTemplateLoading(dir); 200 } 201 } catch (IOException exp) { 202 throw new RuntimeException (exp); 203 } 204 } 205 } 206 | Popular Tags |