freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 31 Aug 2009 22:44:47 +0200
changeset 54 f041b6570ff9
parent 48 69e897fe8140
child 84 824d66048bac
permissions -rw-r--r--
Removing anything related to JSON from the HTML layer, using natural freemarker's W3C DOM processing capabilities
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@54
    44
import org.w3c.dom.Document;
jtulach@41
    45
jtulach@41
    46
/**
jtulach@41
    47
 *
jtulach@41
    48
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    49
 */
jtulach@41
    50
@Provider
jtulach@41
    51
public class FreemarkerProcessor implements TemplateProcessor {
jtulach@41
    52
    private ScriptEngineManager manager;
jtulach@41
    53
jtulach@41
    54
    public String resolve(String name) {
jtulach@41
    55
        return name;
jtulach@41
    56
    }
jtulach@41
    57
jtulach@41
    58
    public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
jtulach@41
    59
        synchronized (this) {
jtulach@41
    60
            if (manager == null) {
jtulach@41
    61
                manager = new ScriptEngineManager();
jtulach@41
    62
            }
jtulach@41
    63
        }
jtulach@41
    64
        ScriptEngine eng = manager.getEngineByName("freemarker");
jtulach@41
    65
        if (eng == null) {
jtulach@41
    66
            throw new IOException("freemarker not found!");
jtulach@41
    67
        }
jtulach@41
    68
        Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
jtulach@41
    69
        if (model instanceof Map) {
jtulach@41
    70
            bind.putAll((Map<? extends String, ? extends Object>)model);
jtulach@41
    71
        }
jtulach@54
    72
        if (model instanceof Document) {
jtulach@54
    73
            bind.put("doc", model);
jtulach@54
    74
        } else {
jtulach@54
    75
            bind.put("model", model);
jtulach@54
    76
        }
jtulach@41
    77
jtulach@41
    78
        Writer w = new OutputStreamWriter(out);
jtulach@41
    79
jtulach@41
    80
        eng.getContext().setWriter(w);
jtulach@41
    81
        eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
jtulach@41
    82
        // silly workaround for
jtulach@41
    83
        // http://openide.netbeans.org/issues/show_bug.cgi?id=170177
jtulach@41
    84
        eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
jtulach@41
    85
jtulach@41
    86
        InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
jtulach@41
    87
        if (is == null) {
jtulach@41
    88
            throw new IOException("Not found " + fqn); // NOI18N
jtulach@41
    89
        }
jtulach@41
    90
        InputStreamReader r = new InputStreamReader(is, "UTF-8");
jtulach@41
    91
        try {
jtulach@41
    92
            eng.eval(r);
jtulach@41
    93
        } catch (ScriptException ex) {
jaroslav@48
    94
            System.err.println("dump data: " + model);
jtulach@41
    95
            ex.printStackTrace();
jtulach@41
    96
            throw new IOException(ex);
jtulach@41
    97
        }
jtulach@41
    98
    }
jtulach@41
    99
}