1 20 21 package org.netbeans.modules.templates; 22 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Reader ; 27 import java.io.Writer ; 28 import java.util.Map ; 29 import java.util.logging.Logger ; 30 import javax.script.Bindings; 31 import javax.script.ScriptContext; 32 import javax.script.ScriptEngine; 33 import javax.script.ScriptEngineManager; 34 import javax.script.ScriptException; 35 import javax.swing.text.PlainDocument ; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.loaders.CreateFromTemplateHandler; 39 import org.openide.text.IndentEngine; 40 41 42 47 public class ScriptingCreateFromTemplateHandler extends CreateFromTemplateHandler { 48 private static ScriptEngineManager manager; 49 private static final Logger LOG = Logger.getLogger(ScriptingCreateFromTemplateHandler.class.getName()); 50 51 protected boolean accept(FileObject orig) { 52 return engine(orig) != null; 53 } 54 55 protected FileObject createFromTemplate(FileObject template, FileObject f, 56 String name, 57 Map <String , Object > values) throws IOException { 58 59 ScriptEngine eng = engine(template); 60 Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); 61 bind.putAll(values); 62 for (Map.Entry <String , Object > entry : values.entrySet()) { 63 eng.getContext().setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE); 64 } 65 66 String nameUniq = FileUtil.findFreeFileName(f, name, template.getExt()); 67 FileObject output = FileUtil.createData(f, nameUniq + '.' + template.getExt()); 68 69 Writer w = null; 70 Reader is = null; 71 try { 72 w = new OutputStreamWriter (output.getOutputStream()); 73 74 IndentEngine format = IndentEngine.find(template.getMIMEType()); 75 if (format != null) { 76 PlainDocument doc = new PlainDocument (); 77 doc.putProperty(PlainDocument.StreamDescriptionProperty, template); 78 w = format.createWriter(doc, 0, w); 79 } 80 81 82 eng.getContext().setWriter(w); 83 eng.getContext().setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE); 85 eng.getContext().setAttribute(ScriptEngine.FILENAME, template.getNameExt(), ScriptContext.ENGINE_SCOPE); 86 is = new InputStreamReader (template.getInputStream()); 87 eng.eval(is); 88 }catch (ScriptException ex) { 89 IOException io = new IOException (ex.getMessage()); 90 io.initCause(ex); 91 throw io; 92 } finally { 93 if (w != null) w.close(); 94 if (is != null) is.close(); 95 } 96 return output; 97 } 98 99 private static ScriptEngine engine(FileObject fo) { 100 Object obj = fo.getAttribute("javax.script.ScriptEngine"); if (obj instanceof ScriptEngine) { 102 return (ScriptEngine)obj; 103 } 104 if (obj instanceof String ) { 105 synchronized (ScriptingCreateFromTemplateHandler.class) { 106 if (manager == null) { 107 manager = new ScriptEngineManager(); 108 } 109 } 110 return manager.getEngineByName((String )obj); 111 } 112 return null; 113 } 114 } 115 | Popular Tags |