1
0

GeneratePatternClasses.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import com.grack.nanojson.JsonArray;
  2. import com.grack.nanojson.JsonObject;
  3. import com.grack.nanojson.JsonParser;
  4. import com.grack.nanojson.JsonParserException;
  5. import org.schabi.newpipe.extractor.timeago.PatternsHolder;
  6. import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
  7. import java.io.*;
  8. import java.util.*;
  9. public class GeneratePatternClasses {
  10. public static void main(String[] args) throws FileNotFoundException, JsonParserException {
  11. final InputStream resourceAsStream =
  12. new FileInputStream("timeago-parser/raw/unique_patterns.json");
  13. final JsonObject from = JsonParser.object().from(resourceAsStream);
  14. final TreeMap<String, Object> map = new TreeMap<>(from);
  15. for (Map.Entry<String, Object> entry : map.entrySet()) {
  16. final String languageCode = entry.getKey().replace('-', '_');
  17. final Map<String, Object> unitsList = (Map<String, Object>) entry.getValue();
  18. final String wordSeparator = (String) unitsList.get("word_separator");
  19. final JsonArray seconds = (JsonArray) unitsList.get("seconds");
  20. final JsonArray minutes = (JsonArray) unitsList.get("minutes");
  21. final JsonArray hours = (JsonArray) unitsList.get("hours");
  22. final JsonArray days = (JsonArray) unitsList.get("days");
  23. final JsonArray weeks = (JsonArray) unitsList.get("weeks");
  24. final JsonArray months = (JsonArray) unitsList.get("months");
  25. final JsonArray years = (JsonArray) unitsList.get("years");
  26. final StringBuilder specialCasesString = new StringBuilder();
  27. specialCasesConstruct(TimeAgoUnit.SECONDS, seconds, specialCasesString);
  28. specialCasesConstruct(TimeAgoUnit.MINUTES, minutes, specialCasesString);
  29. specialCasesConstruct(TimeAgoUnit.HOURS, hours, specialCasesString);
  30. specialCasesConstruct(TimeAgoUnit.DAYS, days, specialCasesString);
  31. specialCasesConstruct(TimeAgoUnit.WEEKS, weeks, specialCasesString);
  32. specialCasesConstruct(TimeAgoUnit.MONTHS, months, specialCasesString);
  33. specialCasesConstruct(TimeAgoUnit.YEARS, years, specialCasesString);
  34. System.out.println("Generating \"" + languageCode + "\" pattern class...");
  35. try (final FileWriter fileOut = new FileWriter(
  36. "timeago-parser/src/main/java/org/schabi/newpipe/extractor/timeago/patterns/" +
  37. languageCode + ".java")) {
  38. final String test = INFO_CLASS_GENERATED + "\n" +
  39. "\n" +
  40. "package org.schabi.newpipe.extractor.timeago.patterns;\n\n" +
  41. "import org.schabi.newpipe.extractor.timeago.PatternsHolder;\n" +
  42. (specialCasesString.length() > 0 ? "import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;\n" : "") +
  43. "\n" +
  44. "public class " + languageCode + " extends PatternsHolder {\n" +
  45. " private static final String WORD_SEPARATOR = \"" + wordSeparator + "\";\n" +
  46. " private static final String[]\n" +
  47. " SECONDS /**/ = {" + join(seconds) + "},\n" +
  48. " MINUTES /**/ = {" + join(minutes) + "},\n" +
  49. " HOURS /**/ = {" + join(hours) + "},\n" +
  50. " DAYS /**/ = {" + join(days) + "},\n" +
  51. " WEEKS /**/ = {" + join(weeks) + "},\n" +
  52. " MONTHS /**/ = {" + join(months) + "},\n" +
  53. " YEARS /**/ = {" + join(years) + "};\n" +
  54. "\n" +
  55. " private static final " + languageCode + " INSTANCE = new " + languageCode + "();\n" +
  56. "\n" +
  57. " public static " + languageCode + " getInstance() {\n" +
  58. " return INSTANCE;\n" +
  59. " }\n" +
  60. "\n" +
  61. " private " + languageCode + "() {\n" +
  62. " super(WORD_SEPARATOR, SECONDS, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS);\n" +
  63. specialCasesString.toString() +
  64. " }\n" +
  65. "}";
  66. fileOut.write(test);
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72. private static void specialCasesConstruct(TimeAgoUnit unit, JsonArray array, StringBuilder stringBuilder) {
  73. final Iterator<Object> iterator = array.iterator();
  74. while (iterator.hasNext()) {
  75. final Object o = iterator.next();
  76. if (o instanceof JsonObject) {
  77. final JsonObject caseObject = (JsonObject) o;
  78. for (Map.Entry<String, Object> caseEntry : caseObject.entrySet()) {
  79. final int caseAmount = Integer.parseInt(caseEntry.getKey());
  80. final String caseText = (String) caseEntry.getValue();
  81. iterator.remove();
  82. stringBuilder.append(" ")
  83. .append("putSpecialCase(TimeAgoUnit.").append(unit.name())
  84. .append(", \"").append(caseText).append("\"")
  85. .append(", ").append(caseAmount).append(");").append("\n");
  86. }
  87. }
  88. }
  89. }
  90. private static final String INFO_CLASS_GENERATED = "/**/// DO NOT MODIFY THIS FILE MANUALLY\n" +
  91. "/**/// This class was automatically generated by \"GeneratePatternClasses.java\",\n" +
  92. "/**/// modify the \"unique_patterns.json\" and re-generate instead.";
  93. private static String join(List<Object> list) {
  94. final StringBuilder toReturn = new StringBuilder();
  95. for (Object o : list) {
  96. toReturn.append('"').append(o).append('"').append(", ");
  97. }
  98. toReturn.setLength(Math.max(toReturn.length() - 2, 0));
  99. return toReturn.toString();
  100. }
  101. }