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