예제 #1
0
 /**
  * (Re)generate the target {@linkplain Properties properties} file.
  *
  * @throws IOException If an exceptional situation arises while reading properties.
  */
 public void generate() throws IOException {
   // Force correct initialization order... sigh.
   AvailRuntime.nextHash();
   final File fileName =
       new File(
           String.format(
               "src/%s_%s.properties", baseName.replace('.', '/'), locale.getLanguage()));
   System.out.println(fileName.getAbsolutePath());
   final String[] components = baseName.split("\\.");
   final File tempFileName =
       new File(
           String.format(
               "%s/%s_%s.propertiesTEMP",
               System.getProperty("java.io.tmpdir"),
               components[components.length - 1],
               locale.getLanguage()));
   assert fileName.getPath().endsWith(".properties");
   final Properties properties = new Properties();
   try (final FileInputStream inputStream = new FileInputStream(fileName)) {
     try (final InputStreamReader reader =
         new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
       properties.load(reader);
     }
   } catch (final FileNotFoundException e) {
     // Ignore. It's okay if the file doesn't already exist.
   }
   final PrintWriter writer = new PrintWriter(tempFileName, "UTF-8");
   generatePreamble(writer);
   generateProperties(properties, writer);
   writer.close();
   // Now switch the new file in.  In the rare event of failure between
   // these steps, the complete content will still be available in the
   // corresponding *.propertiesTEMP file.
   boolean worked = fileName.delete();
   if (!worked) {
     System.err.println(
         String.format("deleting the original properties file failed: %s", fileName));
   }
   worked = tempFileName.renameTo(fileName);
   if (!worked) {
     throw new RuntimeException(
         String.format(
             "moving the temporary properties file failed: %s -> %s", tempFileName, fileName));
   }
 }
예제 #2
0
 /**
  * Generate the preamble for the properties file. This includes the copyright and machine
  * generation warnings.
  *
  * @param writer The {@linkplain PrintWriter output stream}.
  */
 protected void generatePreamble(final PrintWriter writer) {
   writer.println(
       MessageFormat.format(
           preambleBundle.getString(propertiesCopyright.name()),
           localName(baseName) + "_" + locale.getLanguage(),
           new Date()));
   writer.println(
       MessageFormat.format(
           preambleBundle.getString(generatedPropertiesNotice.name()),
           getClass().getName(),
           new Date()));
 }