commit | author | age
|
3ff4c5
|
1 |
/* |
U |
2 |
neon-template - Template extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.template; |
|
19 |
|
|
20 |
import com.github.mustachejava.Mustache; |
|
21 |
import com.github.mustachejava.MustacheFactory; |
|
22 |
import com.sun.net.httpserver.HttpExchange; |
|
23 |
import de.uhilger.neon.FileServer; |
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.StringWriter; |
|
27 |
import java.util.Map; |
|
28 |
|
|
29 |
/** |
|
30 |
* Der TemplateWorker rendert den Inhalt einer Map in |
|
31 |
* ein Mustache-Template und gibt das Ergebnis zurueck. |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
* @version 1, 04.07.2021 |
|
35 |
*/ |
|
36 |
public class TemplateWorker { |
|
37 |
|
|
38 |
//public static final String STR_DOT = "."; |
|
39 |
//public static final String STR_SLASH = "/"; |
|
40 |
|
|
41 |
//public static final String ATTR_TEMPLATE = "template"; |
|
42 |
|
|
43 |
//public static final String WELCOME_FILE = "index.htmi"; |
|
44 |
|
|
45 |
/** |
|
46 |
* Eine HTTP-Anfrage bearbeiten |
|
47 |
* |
|
48 |
* Erfordert den Namen des Templates im Attribut TemplateActor.ATTR_TEMPLATE |
|
49 |
* |
|
50 |
* Wenn im HttpContext das Attribut FileHandler.ATTR_FILE_BASE vorhanden ist, |
|
51 |
* wird dessen Inhalt als Basis-Ordner für das zu verwendende Template genutzt. |
|
52 |
* Anderenfalls wird das Template im Classpath gesucht. |
|
53 |
* |
|
54 |
* @param exchange die Anfrage und -Antwort-Infos der HTTP-Anfrage |
|
55 |
* @param data die Map mit den variablen Inhalten des Templates |
|
56 |
* @param template Dateiname der Vorlage relative zum Ablageort laut HttpContext-Attribut fileBase |
|
57 |
* @return das ausgefuellte Template |
|
58 |
* @throws IOException |
|
59 |
*/ |
|
60 |
public String render(HttpExchange exchange, Object data, String template) throws IOException { |
|
61 |
MustacheFactory mf; |
|
62 |
Mustache m; |
|
63 |
//HttpHelper helper = new HttpHelper(); |
|
64 |
Map attributes = exchange.getHttpContext().getAttributes(); |
|
65 |
String fileBase = (String) attributes |
|
66 |
.getOrDefault(FileServer.ATTR_FILE_BASE, FileServer.STR_EMPTY); |
|
67 |
File fileRoot = new File(fileBase); |
|
68 |
//logger.fine("fileRoot: " + fileRoot.getAbsolutePath()); |
|
69 |
//String template = helper.getAttrStr(attributes, ATTR_TEMPLATE, STR_EMPTY); |
|
70 |
File templateFile = new File(fileRoot, template); |
|
71 |
if(templateFile.exists()) { |
|
72 |
//logger.fine("using template " + templateFile.getAbsolutePath()); |
|
73 |
mf = new NeonMustacheFactory(fileRoot); |
|
74 |
m = mf.compile(template); |
|
75 |
} else { |
|
76 |
//logger.fine("template " + templateFile.getAbsolutePath() + " not found"); |
|
77 |
mf = new NeonMustacheFactory(); |
|
78 |
m = mf.compile(template); |
|
79 |
} |
|
80 |
StringWriter writer = new StringWriter(); |
|
81 |
m.execute(writer, data).flush(); |
|
82 |
return writer.toString(); |
|
83 |
} |
|
84 |
|
|
85 |
} |