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