freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 11 Aug 2009 14:26:49 +0200
changeset 41 c94f68ddef59
child 48 69e897fe8140
permissions -rw-r--r--
Simple, freemarker based textual UI
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 java.util.logging.Level;
jtulach@41
    38
import java.util.logging.Logger;
jtulach@41
    39
import javax.script.Bindings;
jtulach@41
    40
import javax.script.ScriptContext;
jtulach@41
    41
import javax.script.ScriptEngine;
jtulach@41
    42
import javax.script.ScriptEngineManager;
jtulach@41
    43
import javax.script.ScriptException;
jtulach@41
    44
import javax.ws.rs.ext.Provider;
jtulach@41
    45
import org.openide.filesystems.FileUtil;
jtulach@41
    46
jtulach@41
    47
/**
jtulach@41
    48
 *
jtulach@41
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    50
 */
jtulach@41
    51
@Provider
jtulach@41
    52
public class FreemarkerProcessor implements TemplateProcessor {
jtulach@41
    53
    private ScriptEngineManager manager;
jtulach@41
    54
jtulach@41
    55
    public String resolve(String name) {
jtulach@41
    56
        return name;
jtulach@41
    57
    }
jtulach@41
    58
jtulach@41
    59
    public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
jtulach@41
    60
        synchronized (this) {
jtulach@41
    61
            if (manager == null) {
jtulach@41
    62
                manager = new ScriptEngineManager();
jtulach@41
    63
            }
jtulach@41
    64
        }
jtulach@41
    65
        ScriptEngine eng = manager.getEngineByName("freemarker");
jtulach@41
    66
        if (eng == null) {
jtulach@41
    67
            throw new IOException("freemarker not found!");
jtulach@41
    68
        }
jtulach@41
    69
        Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
jtulach@41
    70
        if (model instanceof Map) {
jtulach@41
    71
            bind.putAll((Map<? extends String, ? extends Object>)model);
jtulach@41
    72
        }
jtulach@41
    73
        bind.put("model", model);
jtulach@41
    74
jtulach@41
    75
        Writer w = new OutputStreamWriter(out);
jtulach@41
    76
jtulach@41
    77
        eng.getContext().setWriter(w);
jtulach@41
    78
        eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
jtulach@41
    79
        // silly workaround for
jtulach@41
    80
        // http://openide.netbeans.org/issues/show_bug.cgi?id=170177
jtulach@41
    81
        eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
jtulach@41
    82
jtulach@41
    83
        InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
jtulach@41
    84
        if (is == null) {
jtulach@41
    85
            throw new IOException("Not found " + fqn); // NOI18N
jtulach@41
    86
        }
jtulach@41
    87
        InputStreamReader r = new InputStreamReader(is, "UTF-8");
jtulach@41
    88
        try {
jtulach@41
    89
            eng.eval(r);
jtulach@41
    90
        } catch (ScriptException ex) {
jtulach@41
    91
            ex.printStackTrace();
jtulach@41
    92
            throw new IOException(ex);
jtulach@41
    93
        }
jtulach@41
    94
    }
jtulach@41
    95
}