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;
44 import org.w3c.dom.Document;
48 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 public class FreemarkerProcessor implements TemplateProcessor {
52 private ScriptEngineManager manager;
54 public String resolve(String name) {
58 public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
60 if (manager == null) {
61 manager = new ScriptEngineManager();
64 ScriptEngine eng = manager.getEngineByName("freemarker");
66 throw new IOException("freemarker not found!");
68 Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
69 if (model instanceof Map) {
70 bind.putAll((Map<? extends String, ? extends Object>)model);
72 if (model instanceof Document) {
73 bind.put("doc", model);
75 bind.put("model", model);
78 Writer w = new OutputStreamWriter(out, "UTF-8");
80 eng.getContext().setWriter(w);
81 eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
82 // silly workaround for
83 // http://openide.netbeans.org/issues/show_bug.cgi?id=170177
84 eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
86 InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
88 throw new IOException("Not found " + fqn); // NOI18N
90 InputStreamReader r = new InputStreamReader(is, "UTF-8");
93 } catch (ScriptException ex) {
94 System.err.println("dump data: " + model);
96 throw new IOException(ex);