コード例 #1
0
  public void generateResourceBundle(String baseName, Path bundleDir)
      throws GenerationException, IOException {
    HashMap<String, Properties> bundles = generateResourceBundle(baseName);

    for (String bKey : bundles.keySet()) {
      final Properties bundle = bundles.get(bKey);

      final Path file = bundleDir.resolve(bKey + ".properties");
      try (Writer w = Files.newBufferedWriter(file, Charset.forName("utf8"))) {
        bundle.store(
            w,
            String.format(
                "ResourceBundle (%s) for %s, generated by %s v%s",
                bKey,
                baseName,
                "com.github.tkurz.sesame:vocab-builder",
                MavenUtil.loadVersion(
                    "com.github.tkurz.sesame", "vocab-builder", "0.0.0-DEVELOP")));
      } catch (IOException e) {
        log.error("Could not write Bundle {} to {}: {}", bKey, file, e);
        throw e;
      }
    }
  }
コード例 #2
0
  public HashMap<String, Properties> generateResourceBundle(String baseName)
      throws GenerationException {
    // be sure to have at least the default URI constant settings
    if (uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
    }
    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    HashMap<String, URI> splitUris = new HashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          splitUris.put(k, (URI) nextSubject);
        }
      }
    }

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);

    HashMap<String, Properties> bundles = new HashMap<>();
    // Default we have for sure
    bundles.put(baseName, new Properties());
    for (String key : keys) {
      final URI resource = splitUris.get(key);
      //
      String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));

      for (URI p : LABEL_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".label")) {
              bundle.put(nextKey + ".label", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }

      for (URI p : COMMENT_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".comment")) {
              bundle.put(nextKey + ".comment", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }
    }

    if (getPreferredLanguage() != null) {
      log.debug("completing default Bundle with preferred language {}", getPreferredLanguage());
      final Properties defaultBundle = bundles.get(baseName);
      final Properties prefBundle = bundles.get(baseName + "_" + getPreferredLanguage());
      if (prefBundle != null) {
        for (Entry<Object, Object> key : prefBundle.entrySet()) {
          String nextKey = (String) key.getKey();
          if (!defaultBundle.containsKey(nextKey)) {
            log.trace("copying {} from {} to default Bundle", nextKey, getPreferredLanguage());
            defaultBundle.setProperty(nextKey, (String) key.getValue());
          }
        }
      } else {
        log.warn("No Bundle data found for preferred language {}", getPreferredLanguage());
      }
    }
    return bundles;
  }