freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java
changeset 41 c94f68ddef59
child 48 69e897fe8140
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java	Tue Aug 11 14:26:49 2009 +0200
     1.3 @@ -0,0 +1,95 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.freemarkerdor;
    1.31 +
    1.32 +import com.sun.jersey.spi.template.TemplateProcessor;
    1.33 +import java.io.IOException;
    1.34 +import java.io.InputStream;
    1.35 +import java.io.InputStreamReader;
    1.36 +import java.io.OutputStream;
    1.37 +import java.io.OutputStreamWriter;
    1.38 +import java.io.Writer;
    1.39 +import java.util.Map;
    1.40 +import java.util.logging.Level;
    1.41 +import java.util.logging.Logger;
    1.42 +import javax.script.Bindings;
    1.43 +import javax.script.ScriptContext;
    1.44 +import javax.script.ScriptEngine;
    1.45 +import javax.script.ScriptEngineManager;
    1.46 +import javax.script.ScriptException;
    1.47 +import javax.ws.rs.ext.Provider;
    1.48 +import org.openide.filesystems.FileUtil;
    1.49 +
    1.50 +/**
    1.51 + *
    1.52 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.53 + */
    1.54 +@Provider
    1.55 +public class FreemarkerProcessor implements TemplateProcessor {
    1.56 +    private ScriptEngineManager manager;
    1.57 +
    1.58 +    public String resolve(String name) {
    1.59 +        return name;
    1.60 +    }
    1.61 +
    1.62 +    public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
    1.63 +        synchronized (this) {
    1.64 +            if (manager == null) {
    1.65 +                manager = new ScriptEngineManager();
    1.66 +            }
    1.67 +        }
    1.68 +        ScriptEngine eng = manager.getEngineByName("freemarker");
    1.69 +        if (eng == null) {
    1.70 +            throw new IOException("freemarker not found!");
    1.71 +        }
    1.72 +        Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    1.73 +        if (model instanceof Map) {
    1.74 +            bind.putAll((Map<? extends String, ? extends Object>)model);
    1.75 +        }
    1.76 +        bind.put("model", model);
    1.77 +
    1.78 +        Writer w = new OutputStreamWriter(out);
    1.79 +
    1.80 +        eng.getContext().setWriter(w);
    1.81 +        eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
    1.82 +        // silly workaround for
    1.83 +        // http://openide.netbeans.org/issues/show_bug.cgi?id=170177
    1.84 +        eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
    1.85 +
    1.86 +        InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
    1.87 +        if (is == null) {
    1.88 +            throw new IOException("Not found " + fqn); // NOI18N
    1.89 +        }
    1.90 +        InputStreamReader r = new InputStreamReader(is, "UTF-8");
    1.91 +        try {
    1.92 +            eng.eval(r);
    1.93 +        } catch (ScriptException ex) {
    1.94 +            ex.printStackTrace();
    1.95 +            throw new IOException(ex);
    1.96 +        }
    1.97 +    }
    1.98 +}