-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMXDemo.java
103 lines (92 loc) · 3.43 KB
/
HTMXDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import com.github.forax.htmlcomponent.Component;
import com.github.forax.htmlcomponent.Renderer;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import static java.lang.System.out;
import static java.nio.charset.StandardCharsets.UTF_8;
// cd src/test/java
// java --enable-preview --source 22 --class-path ../../../target/*.jar HTMXDemo.java
public class HTMXDemo {
record University(String name, String headquarter, String creation, String wikiurl) {
University {
Objects.requireNonNull(name);
Objects.requireNonNull(headquarter);
Objects.requireNonNull(creation);
Objects.requireNonNull(wikiurl);
}
}
private static final Pattern UNICODE_ESCAPE = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
private static String cleanData(String text) {
return UNICODE_ESCAPE.matcher(text)
.replaceAll(r -> new String(Character.toChars(Integer.parseInt(r.group(1), 16))));
}
private static List<University> extractData(Path path) throws IOException {
@SuppressWarnings("unchecked")
var json = (List<Map<String, Object>>) JExpress.ToyJSONParser.parse(Files.readString(path));
//out.println(json);
return json.stream()
.filter(data -> data.get("implantation_lib") != null)
.map(data -> new University(
cleanData((String) data.get("implantation_lib")).toLowerCase(Locale.ROOT),
cleanData((String) data.get("siege_lib")),
(String) data.get("date_ouverture"),
(String) data.get("element_wikidata")
))
.toList();
}
record UniversityView(String name, String headquarter, String creation, String wikiurl) implements Component {
public Renderer render() {
return $."""
<tr>
<td><a href="\{wikiurl}">\{name}</a></td>
<td>\{headquarter}</td>
<td>\{creation}</td>
</tr>
""";
}
}
record UniversityListView(List<University> universities, String nameFilter) implements Component {
public Renderer render() {
return $."""
<table>
<tr>
<th>name</th><th>headquarter</th><th>creation date</th>
</tr>
\{
Renderer.from(universities.stream()
.filter(u -> u.name.contains(nameFilter))
.map(u -> new UniversityView(u.name, u.headquarter, u.creation, u.wikiurl)))
}
</table>
""";
}
}
void main() throws URISyntaxException, IOException {
var root = Path.of(HTMXDemo.class.getResource(".").toURI());
var resources = root.resolve("../resources");
if (!Files.isDirectory(resources)) {
resources = root;
}
var universities = extractData(resources.resolve("university.json"));
var app = JExpress.express();
app.use(JExpress.staticFiles(resources));
app.post("/api/university", (request, response) -> {
var formData = URLDecoder.decode(request.bodyText(), UTF_8);
var nameFilter = formData.substring(formData.indexOf('=') + 1);
var view = new UniversityListView(universities, nameFilter);
response
.type("text/xml")
.send(view.render().asString());
});
app.listen(8080);
out.println("application started on port 8080");
}
}