forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentLoader.java
More file actions
88 lines (77 loc) · 3.77 KB
/
ContentLoader.java
File metadata and controls
88 lines (77 loc) · 3.77 KB
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
package og;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SequencedMap;
/** Loads content JSON/YAML files and category properties. */
public final class ContentLoader {
static final String CONTENT_DIR = "content";
static final String CATEGORIES_FILE = "html-generators/categories.properties";
static final ObjectMapper JSON_MAPPER = new ObjectMapper();
static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
static final Map<String, ObjectMapper> MAPPERS = Map.of(
"json", JSON_MAPPER, "yaml", YAML_MAPPER, "yml", YAML_MAPPER
);
public static final SequencedMap<String, String> CATEGORY_DISPLAY = loadProperties(CATEGORIES_FILE);
public record Snippet(JsonNode node) {
public String get(String f) { return node.get(f).asText(); }
public String slug() { return get("slug"); }
public String category() { return get("category"); }
public String title() { return get("title"); }
public String jdkVersion() { return get("jdkVersion"); }
public String oldCode() { return get("oldCode"); }
public String modernCode() { return get("modernCode"); }
public String oldApproach() { return get("oldApproach"); }
public String modernApproach() { return get("modernApproach"); }
public String oldLabel() { return get("oldLabel"); }
public String modernLabel() { return get("modernLabel"); }
public String key() { return category() + "/" + slug(); }
public String catDisplay() { return CATEGORY_DISPLAY.get(category()); }
}
public static SequencedMap<String, Snippet> loadAllSnippets() throws IOException {
var snippets = new LinkedHashMap<String, Snippet>();
for (var cat : CATEGORY_DISPLAY.sequencedKeySet()) {
var catDir = Path.of(CONTENT_DIR, cat);
if (!Files.isDirectory(catDir)) continue;
var sorted = new ArrayList<Path>();
for (var ext : MAPPERS.keySet()) {
try (var stream = Files.newDirectoryStream(catDir, "*." + ext)) {
stream.forEach(sorted::add);
}
}
sorted.sort(Path::compareTo);
for (var path : sorted) {
var ext = path.getFileName().toString();
ext = ext.substring(ext.lastIndexOf('.') + 1);
var snippet = new Snippet(MAPPERS.get(ext).readTree(Files.readString(path)));
snippets.put(snippet.key(), snippet);
}
}
return snippets;
}
public static SequencedMap<String, String> loadProperties(String file) {
try {
var map = new LinkedHashMap<String, String>();
for (var line : Files.readAllLines(Path.of(file))) {
line = line.strip();
if (line.isEmpty() || line.startsWith("#")) continue;
var idx = line.indexOf('=');
if (idx > 0) map.put(line.substring(0, idx).strip(), line.substring(idx + 1).strip());
}
return map;
} catch (IOException e) { throw new UncheckedIOException(e); }
}
public static String xmlEscape(String s) {
return s == null ? ""
: s.replace("&", "&").replace("<", "<").replace(">", ">")
.replace("\"", """).replace("'", "'");
}
private ContentLoader() {}
}