From 0b869c30a0fbd1b469bf9b6bc3aade89c9044f12 Mon Sep 17 00:00:00 2001 From: 20150024 Date: Wed, 28 Nov 2018 13:24:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=99=95=E7=90=86js=E6=AA=94?= =?UTF-8?q?=E6=A1=88=E9=A1=9E=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LanguageFileCompare.java | 74 ++++++++++++++++---------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/main/java/LanguageFileCompare.java b/src/main/java/LanguageFileCompare.java index 68368c1..b835895 100644 --- a/src/main/java/LanguageFileCompare.java +++ b/src/main/java/LanguageFileCompare.java @@ -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 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 originSet) { if (!targetPath.toFile().exists()) return; - try { - Set targetSet = new HashSet<>(); - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode json = objectMapper.readTree(new String(Files.readAllBytes(targetPath))); - addKeys("", json, targetSet, new ArrayList<>()); - - Set 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 targetSet = new HashSet<>(); + JsonNode json = getJsonNode(targetPath); + if (json == null) return; + + addKeys("", json, targetSet, new ArrayList<>()); + + Set 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); } } -- 2.26.2