freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:37:47 +0200
changeset 48 69e897fe8140
parent 41 c94f68ddef59
child 54 f041b6570ff9
permissions -rw-r--r--
Spliting Game into GameId and Game with full info about the state of the game
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     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]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    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;
    36 import java.util.Map;
    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 
    45 /**
    46  *
    47  * @author Jaroslav Tulach <jtulach@netbeans.org>
    48  */
    49 @Provider
    50 public class FreemarkerProcessor implements TemplateProcessor {
    51     private ScriptEngineManager manager;
    52 
    53     public String resolve(String name) {
    54         return name;
    55     }
    56 
    57     public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
    58         synchronized (this) {
    59             if (manager == null) {
    60                 manager = new ScriptEngineManager();
    61             }
    62         }
    63         ScriptEngine eng = manager.getEngineByName("freemarker");
    64         if (eng == null) {
    65             throw new IOException("freemarker not found!");
    66         }
    67         Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    68         if (model instanceof Map) {
    69             bind.putAll((Map<? extends String, ? extends Object>)model);
    70         }
    71         bind.put("model", model);
    72 
    73         Writer w = new OutputStreamWriter(out);
    74 
    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);
    80 
    81         InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
    82         if (is == null) {
    83             throw new IOException("Not found " + fqn); // NOI18N
    84         }
    85         InputStreamReader r = new InputStreamReader(is, "UTF-8");
    86         try {
    87             eng.eval(r);
    88         } catch (ScriptException ex) {
    89             System.err.println("dump data: " + model);
    90             ex.printStackTrace();
    91             throw new IOException(ex);
    92         }
    93     }
    94 }