JsonFileCompare.java 4.92 KB
Newer Older
王品堯's avatar
王品堯 committed
1 2 3
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
4 5
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
王品堯's avatar
王品堯 committed
6

王品堯's avatar
王品堯 committed
7 8
import java.io.FileInputStream;
import java.io.IOException;
王品堯's avatar
王品堯 committed
9 10 11 12 13 14 15 16
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class JsonFileCompare {
17
  private static Logger log = LoggerFactory.getLogger(JsonFileCompare.class);
王品堯's avatar
王品堯 committed
18 19 20 21 22 23 24 25 26 27 28

  public static void main(String[] args) {
    try {
      ObjectMapper objectMapper = new ObjectMapper();
      Path originPath = Paths.get(args[0]);
      JsonNode twJson = objectMapper.readTree(new String(Files.readAllBytes(originPath)));
      Set<String> originSet = new HashSet<>();
      addKeys("", twJson, originSet, new ArrayList<>());

      IntStream.range(1, args.length).forEach(i -> findMissingKeys(Paths.get(args[i]), originSet));
    } catch (Exception e) {
29
      log.error("{} is not exists or not json files.", args[0], e);
王品堯's avatar
王品堯 committed
30 31
      System.exit(1);
    }
王品堯's avatar
王品堯 committed
32
    checkProperties();
王品堯's avatar
王品堯 committed
33 34
  }

王品堯's avatar
王品堯 committed
35 36
  private static void addKeys(
      String currentPath, JsonNode jsonNode, Set<String> set, List<Integer> suffix) {
王品堯's avatar
王品堯 committed
37 38 39 40 41 42 43 44 45 46 47
    if (jsonNode.isObject()) {
      ObjectNode objectNode = (ObjectNode) jsonNode;
      Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
      String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "-";

      while (iter.hasNext()) {
        Map.Entry<String, JsonNode> entry = iter.next();
        addKeys(pathPrefix + entry.getKey(), entry.getValue(), set, suffix);
      }

    } else if (jsonNode.isValueNode()) {
48
      StringBuilder sb = new StringBuilder(currentPath);
王品堯's avatar
王品堯 committed
49
      if (currentPath.contains("-")) {
50 51
        for (Integer suffix1 : suffix) {
          sb.append("-").append(suffix1);
王品堯's avatar
王品堯 committed
52 53
        }
      }
54
      set.add(sb.toString());
王品堯's avatar
王品堯 committed
55 56 57
    }
  }

58
  private static void findMissingKeys(Path targetPath, Set<String> originSet) {
王品堯's avatar
王品堯 committed
59 60 61 62 63 64 65 66 67 68 69 70 71
    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()) {
王品堯's avatar
王品堯 committed
72 73 74 75
        log.error(
            "Missing Key on {} \nMissing Keys: {}",
            targetPath.getFileName(),
            missingKey.stream().sorted().collect(Collectors.toList()));
王品堯's avatar
王品堯 committed
76 77 78
        System.exit(1);
      }
    } catch (Exception e) {
79
      log.error("{} is not json files.", targetPath, e);
王品堯's avatar
王品堯 committed
80 81 82
      System.exit(1);
    }
  }
王品堯's avatar
王品堯 committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

  private static void checkProperties() {
    String path = "./src/main/resources/i18n/";
    String extension = ".properties";

    Map<String, List<String>> map =
        Arrays.stream(
                Optional.ofNullable(
                        Paths.get(path).toFile().list((dir, name) -> name.endsWith(extension)))
                    .orElse(new String[0]))
            .map(s -> s.split(extension)[0])
            .collect(
                Collectors.groupingBy(
                    filename -> filename.split("_")[0],
                    Collectors.mapping(l -> path + l + extension, Collectors.toList())));

    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
      List<String> twList =
          entry
              .getValue()
              .stream()
              .filter(fileName -> fileName.contains("zh_TW"))
              .collect(Collectors.toList());
      List<String> otherList =
          entry
              .getValue()
              .stream()
              .filter(fileName -> !fileName.contains("zh_TW"))
              .collect(Collectors.toList());
      if (twList.isEmpty() || otherList.isEmpty()) continue;

      try {
        Set<String> originKeySet = getKeys(twList.get(0));
        for (String target : otherList) {
          Set<String> targetKeySet = getKeys(target);
          Set<String> missingSet =
              originKeySet
                  .parallelStream()
                  .filter(k -> !targetKeySet.contains(k))
                  .collect(Collectors.toSet());
          if (!missingSet.isEmpty()) {
            log.error(
                "Missing Key on {} \nMissing Keys: {}",
                Paths.get(target).getFileName(),
                missingSet);
            System.exit(1);
          }
        }
      } catch (Exception e) {
        log.error("check i18n properties error.", e);
        System.exit(1);
      }
    }
  }

  private static Set<String> getKeys(String fileName) throws IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(fileName));
    return new HashSet(Collections.list(properties.propertyNames()));
  }
王品堯's avatar
王品堯 committed
143
}