KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > templates > ScriptingCreateFromTemplateHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc.
17  *
18  * Portions Copyrighted 2007 Sun Microsystems, Inc.
19  */

20
21 package org.netbeans.modules.templates;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.logging.Logger JavaDoc;
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 JavaDoc;
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 /** Processeses templates that have associated attribute
43 * with name of the scripting engine.
44 *
45 * @author Jaroslav Tulach
46 */

47 public class ScriptingCreateFromTemplateHandler extends CreateFromTemplateHandler {
48     private static ScriptEngineManager manager;
49     private static final Logger JavaDoc 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 JavaDoc name,
57                                             Map JavaDoc<String JavaDoc, Object JavaDoc> values) throws IOException JavaDoc {
58         
59         ScriptEngine eng = engine(template);
60         Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
61         bind.putAll(values);
62         for (Map.Entry JavaDoc<String JavaDoc, Object JavaDoc> entry : values.entrySet()) {
63             eng.getContext().setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
64         }
65         
66         String JavaDoc nameUniq = FileUtil.findFreeFileName(f, name, template.getExt());
67         FileObject output = FileUtil.createData(f, nameUniq + '.' + template.getExt());
68
69         Writer JavaDoc w = null;
70         Reader JavaDoc is = null;
71         try {
72             w = new OutputStreamWriter JavaDoc(output.getOutputStream());
73             
74             IndentEngine format = IndentEngine.find(template.getMIMEType());
75             if (format != null) {
76                 PlainDocument JavaDoc doc = new PlainDocument JavaDoc();
77                 doc.putProperty(PlainDocument.StreamDescriptionProperty, template);
78                 w = format.createWriter(doc, 0, w);
79             }
80             
81             
82             eng.getContext().setWriter(w);
83             //eng.getContext().setBindings(bind, ScriptContext.ENGINE_SCOPE);
84
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 JavaDoc(template.getInputStream());
87             eng.eval(is);
88         }catch (ScriptException ex) {
89             IOException JavaDoc io = new IOException JavaDoc(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 JavaDoc obj = fo.getAttribute("javax.script.ScriptEngine"); // NOI18N
101
if (obj instanceof ScriptEngine) {
102             return (ScriptEngine)obj;
103         }
104         if (obj instanceof String JavaDoc) {
105             synchronized (ScriptingCreateFromTemplateHandler.class) {
106                 if (manager == null) {
107                     manager = new ScriptEngineManager();
108                 }
109             }
110             return manager.getEngineByName((String JavaDoc)obj);
111         }
112         return null;
113     }
114 }
115
Popular Tags