Class IndexedResourceCompiler

java.lang.Object
org.apache.sis.util.resources.IndexedResourceCompiler
All Implemented Interfaces:
FilenameFilter, Comparator<Object>
Direct Known Subclasses:
ResourceCompilerMojo.Compiler

class IndexedResourceCompiler extends Object implements FilenameFilter, Comparator<Object>
Reads a given list of .properties files and copies their content to .utf files using UTF-8 encoding. This class also checks for key validity and checks values for MessageFormat compatibility. Finally, it writes the key values in the Java source files.

This class is independent of any Mojo and could be executed from the command-line. For now we keep it package-private, but we could consider to enable execution from the command-line in a future version if this happen to be useful.

Since:
0.3
Version:
0.8
  • Field Details

    • JAVA_EXT

      static final String JAVA_EXT
      Extension for java source files.
      See Also:
    • PROPERTIES_EXT

      static final String PROPERTIES_EXT
      Extension for properties source files.
      See Also:
    • RESOURCES_EXT

      private static final String RESOURCES_EXT
      Extension for resource target files.
      See Also:
    • ARGUMENT_COUNT_PREFIX

      private static final String ARGUMENT_COUNT_PREFIX
      Prefix for argument count in resource key names. For example, a resource expecting one argument may have a key name like "HelloWorld_1".
      See Also:
    • COMMENT_LENGTH

      private static final int COMMENT_LENGTH
      The maximal length of comment lines.
      See Also:
    • KEYS_INNER_CLASS

      private static final String KEYS_INNER_CLASS
      The name of the inner class which will contains key values.
      See Also:
    • JAVA_ENCODING

      private static final String JAVA_ENCODING
      Encoding of Java source file (not property files).
      See Also:
    • KEY_MODIFIERS

      private static final String KEY_MODIFIERS
      The Java modifiers applies on the key constants to be generated.
      See Also:
    • KEY_MARGIN

      private static final String KEY_MARGIN
      Margin to write before the KEY_MODIFIERS.
      See Also:
    • sourceDirectory

      private final File sourceDirectory
      The source directory of the java and properties files.
    • buildDirectory

      private final File buildDirectory
      The target directory where to write the UTF files.
    • bundleClass

      private File bundleClass
      The resource bundle base class being processed. Example: org/apache/sis/util/resources/Vocabulary.java.
    • allocatedIDs

      private final Map<Integer,String> allocatedIDs
      Integer IDs allocated to resource keys. This map will be shared for all languages of a given resource bundle.
    • resources

      private final Map<Object,Object> resources
      Resource keys and their localized values. This map will be cleared for each language in a resource bundle.
    • resourcesToProcess

      private final File[] resourcesToProcess
      The resources bundle base classes.
    • errors

      private int errors
      Number of errors found.
  • Constructor Details

    • IndexedResourceCompiler

      IndexedResourceCompiler(File sourceDirectory, File buildDirectory, File[] resourcesToProcess)
      Constructs a new IndexedResourceCompiler.
      Parameters:
      sourceDirectory - the source directory.
      buildDirectory - the target directory where to write UTF files.
      resourcesToProcess - the resource bundle base classes (e.g. org/apache/sis/util/resources/Vocabulary.java).
  • Method Details

    • run

      public int run() throws ResourceCompilerException
      Runs the resource compiler.
      Returns:
      the number of errors found.
      Throws:
      ResourceCompilerException - if an error occurred.
    • loadKeyValues

      private void loadKeyValues() throws IOException
      Loads the existing key values from the source file. This is used in order to avoid the need to recompile the whole application when new entries are added.
      Throws:
      IOException - if an error occurred while reading the source file.
    • relative

      private static String relative(File directory, File file) throws IOException
      Returns the path of the given file relative to the given directory.
      Throws:
      IOException
    • scanForResources

      private void scanForResources() throws IOException
      Scans the package of bundleClass for its .properties files. The following methods are invoked by this method:
      Throws:
      IOException - if an input/output operation failed.
    • accept

      public final boolean accept(File directory, String name)
      Returns true if the given file is a property file.
      Specified by:
      accept in interface FilenameFilter
      Parameters:
      directory - the directory (ignored).
      name - the file name.
      Returns:
      true if the given file is a property file.
    • loadRawProperties

      private static Properties loadRawProperties(File file) throws IOException
      Loads the specified property file. No processing are performed on them.
      Parameters:
      file - the property file to load.
      Returns:
      the properties.
      Throws:
      IOException - if the file cannot be read.
    • loadProperties

      private void loadProperties(File file) throws IOException
      Loads all properties from a .properties file. Resource keys are checked for naming conventions (i.e. resources expecting some arguments must have a key name ending with "_$n" where "n" is the number of arguments). This method transforms resource values into legal MessageFormat patterns when necessary.

      The following methods must be invoked before this one:

      Parameters:
      file - the properties file to read.
      Throws:
      IOException - if an input/output operation failed.
    • toMessageFormatString

      private static String toMessageFormatString(String text)
      Changes a "normal" text string into a pattern compatible with MessageFormat. The main operation consists of changing ' for '', except for '{' and '}' strings.
    • writeUTF

      private void writeUTF(File file) throws IOException
      Writes UTF file. The following methods must be invoked before this one:
      Parameters:
      file - the destination file.
      Throws:
      IOException - if an input/output operation failed.
    • writeJavaSource

      private void writeJavaSource() throws IOException
      Creates a source file for resource keys. The following methods must be invoked before this one:
      Throws:
      IOException - if an input/output operation failed.
    • compare

      public final int compare(Object o1, Object o2)
      Compares two resource keys. Object o1 and o2 are usually String objects representing resource keys (for example, "MISMATCHED_DIMENSION"), but may also be Map.Entry.
      Specified by:
      compare in interface Comparator<Object>
      Parameters:
      o1 - the resource key to compare.
      o2 - the second resource key to compare.
      Returns:
      -1, 0 or +1 based on the alphabetic order of resource keys.
    • info

      protected void info(String message)
      Logs the given message at the INFO level. The default implementation just sent it to the standard output stream.
      Parameters:
      message - the message to log.
    • warning

      protected void warning(String message)
      Logs the given message at the WARNING level. The default implementation just sent it to the standard output stream.
      Parameters:
      message - the message to log.
    • warning

      private void warning(File file, String key, String message, Exception exception)
      Logs the given message at the WARNING level.
      Parameters:
      file - file that produced the error, or null if none.
      key - resource key that produced the error, or null if none.
      message - the message string.
      exception - an optional exception that is the cause of this warning.