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