Commit 0b869c30 authored by 王品堯's avatar 王品堯

增加處理js檔案類型

parent 6f073c97
......@@ -4,7 +4,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -17,22 +22,40 @@ public class LanguageFileCompare {
private static Logger log = LoggerFactory.getLogger(LanguageFileCompare.class);
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
if (args.length > 0) {
Path originPath = Paths.get(args[0]);
JsonNode twJson = objectMapper.readTree(new String(Files.readAllBytes(originPath)));
if (args.length > 0) {
Path originPath = Paths.get(args[0]);
JsonNode twJson = getJsonNode(originPath);
if (twJson != null) {
Set<String> originSet = new HashSet<>();
addKeys("", twJson, originSet, new ArrayList<>());
IntStream.range(1, args.length)
.forEach(i -> findMissingKeys(Paths.get(args[i]), originSet));
}
}
checkProperties();
}
private static JsonNode getJsonNode(Path path) {
String ext = path.getFileName().toString();
try {
ObjectMapper objectMapper = new ObjectMapper();
if (ext.endsWith(".json")) {
/* CHM、HRB */
return objectMapper.readTree(new String(Files.readAllBytes(path)));
} else if (ext.endsWith(".js")) {
/* CHMB use js file to translate */
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("Nashorn");
engine.eval(new FileReader(path.toFile()));
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
return objectMapper.valueToTree(bindings.get("local"));
}
} catch (Exception e) {
log.error("{} is not exists or not json files.", args[0], e);
log.error("{} is not exists or file not correct.", path.getFileName(), e);
System.exit(1);
}
checkProperties();
return null;
}
private static void addKeys(
......@@ -60,26 +83,23 @@ public class LanguageFileCompare {
private static void findMissingKeys(Path targetPath, Set<String> originSet) {
if (!targetPath.toFile().exists()) return;
try {
Set<String> targetSet = new HashSet<>();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode json = objectMapper.readTree(new String(Files.readAllBytes(targetPath)));
addKeys("", json, targetSet, new ArrayList<>());
Set<String> missingKey =
originSet
.parallelStream()
.filter(key -> !targetSet.contains(key))
.collect(Collectors.toSet());
if (!missingKey.isEmpty()) {
log.error(
"Missing Key on {} \nMissing Keys: {}",
targetPath.getFileName(),
missingKey.stream().sorted().collect(Collectors.toList()));
System.exit(1);
}
} catch (Exception e) {
log.error("{} is not json files.", targetPath, e);
Set<String> targetSet = new HashSet<>();
JsonNode json = getJsonNode(targetPath);
if (json == null) return;
addKeys("", json, targetSet, new ArrayList<>());
Set<String> missingKey =
originSet
.parallelStream()
.filter(key -> !targetSet.contains(key))
.collect(Collectors.toSet());
if (!missingKey.isEmpty()) {
log.error(
"Missing Key on {} \nMissing Keys: {}",
targetPath.getFileName(),
missingKey.stream().sorted().collect(Collectors.toList()));
System.exit(1);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment