freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.freemarkerdor;
29 import com.sun.jersey.spi.template.TemplateProcessor;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import java.io.OutputStreamWriter;
35 import java.io.Writer;
37 import javax.script.Bindings;
38 import javax.script.ScriptContext;
39 import javax.script.ScriptEngine;
40 import javax.script.ScriptEngineManager;
41 import javax.script.ScriptException;
42 import javax.ws.rs.ext.Provider;
43 import org.openide.filesystems.FileUtil;
47 * @author Jaroslav Tulach <jtulach@netbeans.org>
50 public class FreemarkerProcessor implements TemplateProcessor {
51 private ScriptEngineManager manager;
53 public String resolve(String name) {
57 public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
59 if (manager == null) {
60 manager = new ScriptEngineManager();
63 ScriptEngine eng = manager.getEngineByName("freemarker");
65 throw new IOException("freemarker not found!");
67 Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
68 if (model instanceof Map) {
69 bind.putAll((Map<? extends String, ? extends Object>)model);
71 bind.put("model", model);
73 Writer w = new OutputStreamWriter(out);
75 eng.getContext().setWriter(w);
76 eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
77 // silly workaround for
78 // http://openide.netbeans.org/issues/show_bug.cgi?id=170177
79 eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
81 InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
83 throw new IOException("Not found " + fqn); // NOI18N
85 InputStreamReader r = new InputStreamReader(is, "UTF-8");
88 } catch (ScriptException ex) {
89 System.err.println("dump data: " + model);
91 throw new IOException(ex);