jtulach@41
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@41
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jtulach@41
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jtulach@41
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jtulach@41
|
17 |
*/
|
jtulach@41
|
18 |
|
jtulach@41
|
19 |
package cz.xelfi.quoridor.freemarkerdor;
|
jtulach@41
|
20 |
|
jtulach@41
|
21 |
import com.sun.jersey.spi.template.TemplateProcessor;
|
jtulach@41
|
22 |
import java.io.IOException;
|
jtulach@41
|
23 |
import java.io.InputStream;
|
jtulach@41
|
24 |
import java.io.InputStreamReader;
|
jtulach@41
|
25 |
import java.io.OutputStream;
|
jtulach@41
|
26 |
import java.io.OutputStreamWriter;
|
jtulach@41
|
27 |
import java.io.Writer;
|
jtulach@41
|
28 |
import java.util.Map;
|
jtulach@41
|
29 |
import javax.script.Bindings;
|
jtulach@41
|
30 |
import javax.script.ScriptContext;
|
jtulach@41
|
31 |
import javax.script.ScriptEngine;
|
jtulach@41
|
32 |
import javax.script.ScriptEngineManager;
|
jtulach@41
|
33 |
import javax.script.ScriptException;
|
jtulach@41
|
34 |
import javax.ws.rs.ext.Provider;
|
jtulach@41
|
35 |
import org.openide.filesystems.FileUtil;
|
jtulach@54
|
36 |
import org.w3c.dom.Document;
|
jtulach@41
|
37 |
|
jtulach@41
|
38 |
/**
|
jtulach@41
|
39 |
*
|
jtulach@41
|
40 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@41
|
41 |
*/
|
jtulach@41
|
42 |
@Provider
|
jtulach@41
|
43 |
public class FreemarkerProcessor implements TemplateProcessor {
|
jtulach@41
|
44 |
private ScriptEngineManager manager;
|
jtulach@41
|
45 |
|
jtulach@41
|
46 |
public String resolve(String name) {
|
jtulach@41
|
47 |
return name;
|
jtulach@41
|
48 |
}
|
jtulach@41
|
49 |
|
jtulach@41
|
50 |
public void writeTo(String fqn, Object model, OutputStream out) throws IOException {
|
jtulach@41
|
51 |
synchronized (this) {
|
jtulach@41
|
52 |
if (manager == null) {
|
jtulach@41
|
53 |
manager = new ScriptEngineManager();
|
jtulach@41
|
54 |
}
|
jtulach@41
|
55 |
}
|
jtulach@41
|
56 |
ScriptEngine eng = manager.getEngineByName("freemarker");
|
jtulach@41
|
57 |
if (eng == null) {
|
jtulach@41
|
58 |
throw new IOException("freemarker not found!");
|
jtulach@41
|
59 |
}
|
jtulach@41
|
60 |
Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
|
jtulach@41
|
61 |
if (model instanceof Map) {
|
jtulach@41
|
62 |
bind.putAll((Map<? extends String, ? extends Object>)model);
|
jtulach@41
|
63 |
}
|
jtulach@54
|
64 |
if (model instanceof Document) {
|
jtulach@54
|
65 |
bind.put("doc", model);
|
jtulach@54
|
66 |
} else {
|
jtulach@54
|
67 |
bind.put("model", model);
|
jtulach@54
|
68 |
}
|
jtulach@41
|
69 |
|
jaroslav@84
|
70 |
Writer w = new OutputStreamWriter(out, "UTF-8");
|
jtulach@41
|
71 |
|
jtulach@41
|
72 |
eng.getContext().setWriter(w);
|
jtulach@41
|
73 |
eng.getContext().setAttribute(ScriptEngine.FILENAME, fqn, ScriptContext.ENGINE_SCOPE);
|
jtulach@41
|
74 |
// silly workaround for
|
jtulach@41
|
75 |
// http://openide.netbeans.org/issues/show_bug.cgi?id=170177
|
jtulach@41
|
76 |
eng.getContext().setAttribute("org.openide.filesystems.FileObject", FileUtil.createData(FileUtil.getConfigRoot(), fqn), ScriptContext.ENGINE_SCOPE);
|
jtulach@41
|
77 |
|
jtulach@41
|
78 |
InputStream is = FreemarkerProcessor.class.getResourceAsStream(fqn);
|
jtulach@41
|
79 |
if (is == null) {
|
jtulach@41
|
80 |
throw new IOException("Not found " + fqn); // NOI18N
|
jtulach@41
|
81 |
}
|
jtulach@41
|
82 |
InputStreamReader r = new InputStreamReader(is, "UTF-8");
|
jtulach@41
|
83 |
try {
|
jtulach@41
|
84 |
eng.eval(r);
|
jtulach@41
|
85 |
} catch (ScriptException ex) {
|
jaroslav@48
|
86 |
System.err.println("dump data: " + model);
|
jtulach@41
|
87 |
ex.printStackTrace();
|
jtulach@41
|
88 |
throw new IOException(ex);
|
jtulach@41
|
89 |
}
|
jtulach@41
|
90 |
}
|
jtulach@41
|
91 |
}
|