Пример #1
0
 private void checkConsistentCasing(
     Category category,
     String path,
     String fullPath,
     String value,
     Options options,
     List<CheckStatus> result) {
   // Avoid NPE
   if (types != null) {
     CasingType ft = CasingType.from(value);
     CasingTypeAndErrFlag typeAndFlagFromCat = types.get(category);
     if (typeAndFlagFromCat == null) {
       typeAndFlagFromCat = CasingTypeAndErrFlag.other_mismatchWarn;
     }
     if (!ft.worksWith(typeAndFlagFromCat.type())) {
       result.add(
           new CheckStatus()
               .setCause(this)
               .setMainType(
                   typeAndFlagFromCat.flag() ? CheckStatus.errorType : CheckStatus.warningType)
               .setSubtype(Subtype.incorrectCasing) // typically warningType or errorType
               .setMessage(
                   CASE_WARNING,
                   value,
                   ft,
                   category,
                   typeAndFlagFromCat.type())); // the message; can be MessageFormat with arguments
     }
   }
 }
Пример #2
0
  /**
   * Calculates casing information using data from the specified CLDRFile.
   *
   * @param resolved the resolved CLDRFile to calculate casing information from
   * @return
   */
  public static Map<Category, CasingType> getSamples(CLDRFile resolved) {
    // Use EnumMap instead of an array for type safety.
    Map<Category, Counter<CasingType>> counters =
        new EnumMap<Category, Counter<CasingType>>(Category.class);

    for (Category category : Category.values()) {
      counters.put(category, new Counter<CasingType>());
    }
    PathStarrer starrer = new PathStarrer();
    boolean isRoot = "root".equals(resolved.getLocaleID());
    Set<String> missing = !DEBUG ? null : new TreeSet<String>();

    for (String path : resolved) {
      if (!isRoot) {
        String locale2 = resolved.getSourceLocaleID(path, null);
        if (locale2.equals("root") || locale2.equals("code-fallback")) {
          continue;
        }
      }
      String winningPath = resolved.getWinningPath(path);
      if (!winningPath.equals(path)) {
        continue;
      }
      Category category = getCategory(path);
      if (category != null) {
        String value = resolved.getStringValue(path);
        if (value == null || value.length() == 0) continue;
        CasingType ft = CasingType.from(value);
        counters.get(category).add(ft, 1);
      } else if (DEBUG) {
        String starred = starrer.set(path);
        missing.add(starred);
      }
    }

    Map<Category, CasingType> info = new EnumMap<Category, CasingType>(Category.class);
    for (Category category : Category.values()) {
      if (category == Category.NOT_USED) continue;
      Counter<CasingType> counter = counters.get(category);
      long countLower = counter.getCount(CasingType.lowercase);
      long countUpper = counter.getCount(CasingType.titlecase);
      long countOther = counter.getCount(CasingType.other);
      CasingType type;
      if (countLower + countUpper == 0) {
        type = CasingType.other;
      } else if (countLower >= countUpper * MIN_FACTOR && countLower >= countOther) {
        type = CasingType.lowercase;
      } else if (countUpper >= countLower * MIN_FACTOR && countUpper >= countOther) {
        type = CasingType.titlecase;
      } else {
        type = CasingType.other;
      }
      info.put(category, type);
    }
    if (DEBUG && missing.size() != 0) {
      System.out.println("Paths skipped:\n" + CollectionUtilities.join(missing, "\n"));
    }
    return info;
  }