Package org.apache.sis.util.resources
Class IndexedResourceCompiler
java.lang.Object
org.apache.sis.util.resources.IndexedResourceCompiler
- All Implemented Interfaces:
FilenameFilter
,Comparator<Object>
- Direct Known Subclasses:
ResourceCompilerMojo.Compiler
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 Summary
FieldsModifier and TypeFieldDescriptionInteger IDs allocated to resource keys.private static final String
Prefix for argument count in resource key names.private final File
The target directory where to write the UTF files.private File
The resource bundle base class being processed.private static final int
The maximal length of comment lines.private int
Number of errors found.private static final String
Encoding of Java source file (not property files).(package private) static final String
Extension for java source files.private static final String
Margin to write before theKEY_MODIFIERS
.private static final String
The Java modifiers applies on the key constants to be generated.private static final String
The name of the inner class which will contains key values.(package private) static final String
Extension for properties source files.Resource keys and their localized values.private static final String
Extension for resource target files.private final File[]
The resources bundle base classes.private final File
The source directory of the java and properties files. -
Constructor Summary
ConstructorsConstructorDescriptionIndexedResourceCompiler
(File sourceDirectory, File buildDirectory, File[] resourcesToProcess) Constructs a newIndexedResourceCompiler
. -
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
Returnstrue
if the given file is a property file.final int
Compares two resource keys.protected void
Logs the given message at theINFO
level.private void
Loads the existing key values from the source file.private void
loadProperties
(File file) Loads all properties from a.properties
file.private static Properties
loadRawProperties
(File file) Loads the specified property file.private static String
Returns the path of the given file relative to the given directory.int
run()
Runs the resource compiler.private void
Scans the package ofbundleClass
for its.properties
files.private static String
toMessageFormatString
(String text) Changes a "normal" text string into a pattern compatible withMessageFormat
.private void
Logs the given message at theWARNING
level.protected void
Logs the given message at theWARNING
level.private void
Creates a source file for resource keys.private void
Writes UTF file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
JAVA_EXT
Extension for java source files.- See Also:
-
PROPERTIES_EXT
Extension for properties source files.- See Also:
-
RESOURCES_EXT
Extension for resource target files.- See Also:
-
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_LENGTHThe maximal length of comment lines.- See Also:
-
KEYS_INNER_CLASS
The name of the inner class which will contains key values.- See Also:
-
JAVA_ENCODING
Encoding of Java source file (not property files).- See Also:
-
KEY_MODIFIERS
The Java modifiers applies on the key constants to be generated.- See Also:
-
KEY_MARGIN
Margin to write before theKEY_MODIFIERS
.- See Also:
-
sourceDirectory
The source directory of the java and properties files. -
buildDirectory
The target directory where to write the UTF files. -
bundleClass
The resource bundle base class being processed. Example:org/apache/sis/util/resources/Vocabulary.java
. -
allocatedIDs
Integer IDs allocated to resource keys. This map will be shared for all languages of a given resource bundle. -
resources
Resource keys and their localized values. This map will be cleared for each language in a resource bundle. -
resourcesToProcess
The resources bundle base classes. -
errors
private int errorsNumber of errors found.
-
-
Constructor Details
-
IndexedResourceCompiler
Constructs a newIndexedResourceCompiler
.- 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
Runs the resource compiler.- Returns:
- the number of errors found.
- Throws:
ResourceCompilerException
- if an error occurred.
-
loadKeyValues
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
Returns the path of the given file relative to the given directory.- Throws:
IOException
-
scanForResources
Scans the package ofbundleClass
for its.properties
files. The following methods are invoked by this method:- Throws:
IOException
- if an input/output operation failed.
-
accept
Returnstrue
if the given file is a property file.- Specified by:
accept
in interfaceFilenameFilter
- Parameters:
directory
- the directory (ignored).name
- the file name.- Returns:
true
if the given file is a property file.
-
loadRawProperties
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
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 legalMessageFormat
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
Changes a "normal" text string into a pattern compatible withMessageFormat
. The main operation consists of changing ' for '', except for '{' and '}' strings. -
writeUTF
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
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
Compares two resource keys. Objecto1
ando2
are usuallyString
objects representing resource keys (for example, "MISMATCHED_DIMENSION
"), but may also beMap.Entry
.- Specified by:
compare
in interfaceComparator<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
Logs the given message at theINFO
level. The default implementation just sent it to the standard output stream.- Parameters:
message
- the message to log.
-
warning
Logs the given message at theWARNING
level. The default implementation just sent it to the standard output stream.- Parameters:
message
- the message to log.
-
warning
Logs the given message at theWARNING
level.- Parameters:
file
- file that produced the error, ornull
if none.key
- resource key that produced the error, ornull
if none.message
- the message string.exception
- an optional exception that is the cause of this warning.
-