KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > libs > freemarker > FreemarkerEngine


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 scripting.dev.java.net. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc.
17  *
18  * Portions Copyrighted 2006 Sun Microsystems, Inc.
19  */

20
21 package org.netbeans.libs.freemarker;
22
23
24 import javax.script.*;
25 import java.io.*;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28 import freemarker.template.*;
29 import org.openide.filesystems.FileObject;
30
31 /* Taken from A. Sundararajan and adopted by Jaroslav Tulach
32  * for NetBeans needs.
33  *
34  * @author A. Sundararajan
35  */

36 class FreemarkerEngine extends AbstractScriptEngine {
37
38     public static final String JavaDoc STRING_OUTPUT_MODE = "com.sun.script.freemarker.stringOut";
39     public static final String JavaDoc FREEMARKER_CONFIG = "com.sun.script.freemarker.config";
40     public static final String JavaDoc FREEMARKER_PROPERTIES = "com.sun.script.freemarker.properties";
41     public static final String JavaDoc FREEMARKER_TEMPLATE_DIR = "com.sun.script.freemarker.template.dir";
42     public static final String JavaDoc FREEMARKER_TEMPLATE = "org.openide.filesystems.FileObject";
43
44     // my factory, may be null
45
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     // ScriptEngine methods
57
public Object JavaDoc eval(String JavaDoc str, ScriptContext ctx)
58                        throws ScriptException {
59         return eval(new StringReader(str), ctx);
60     }
61
62     public Object JavaDoc eval(Reader reader, ScriptContext ctx)
63                        throws ScriptException {
64         ctx.setAttribute("context", ctx, ScriptContext.ENGINE_SCOPE);
65         initFreeMarkerConfiguration(ctx);
66         String JavaDoc 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 JavaDoc 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     // internals only below this point
102
private static String JavaDoc getFilename(ScriptContext ctx) {
103         Object JavaDoc fileName = ctx.getAttribute(ScriptEngine.FILENAME);
104         return fileName != null? fileName.toString() : "<unknown>";
105     }
106
107     private static boolean isStringOutputMode(ScriptContext ctx) {
108         Object JavaDoc 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 JavaDoc cfg = ctx.getAttribute(FREEMARKER_CONFIG);
123                 if (cfg instanceof Configuration) {
124                     conf = (Configuration) cfg;
125                     return;
126                 }
127
128                 Object JavaDoc 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 JavaDoc rexp) {
136                     throw rexp;
137                 } catch (Exception JavaDoc exp) {
138                     throw new RuntimeException JavaDoc(exp);
139                 }
140                 conf = tmpConf;
141             }
142         }
143     }
144
145     private static void initConfProps(Configuration conf, ScriptContext ctx) {
146         try {
147             Properties JavaDoc props = null;
148             Object JavaDoc tmp = ctx.getAttribute(FREEMARKER_PROPERTIES);
149             if (props instanceof Properties JavaDoc) {
150                 props = (Properties JavaDoc) tmp;
151             } else {
152                 String JavaDoc 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 JavaDoc();
157                         props.load(new FileInputStream(propsFile));
158                     }
159                 }
160             }
161             if (props != null) {
162                 Set JavaDoc<Object JavaDoc> keys = props.keySet();
163                 for (Object JavaDoc obj : keys) {
164                     String JavaDoc key;
165                     if (obj instanceof String JavaDoc) {
166                         key = (String JavaDoc) obj;
167                     } else {
168                         continue;
169                     }
170                     try {
171                         conf.setSetting(key, props.get(key).toString());
172                     } catch (TemplateException te) {
173                         // ignore
174
}
175                 }
176             }
177         } catch (RuntimeException JavaDoc re) {
178             throw re;
179         } catch (Exception JavaDoc exp) {
180             throw new RuntimeException JavaDoc(exp);
181         }
182     }
183
184     private static void initTemplateDir(Configuration conf, FileObject fo, ScriptContext ctx) {
185         try {
186             Object JavaDoc tmp = ctx.getAttribute(FREEMARKER_TEMPLATE_DIR);
187             String JavaDoc 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 JavaDoc(exp);
203         }
204     }
205 }
206
Popular Tags