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 StringPrefix for argument count in resource key names.private final FileThe target directory where to write the UTF files.private FileThe resource bundle base class being processed.private static final intThe maximal length of comment lines.private intNumber of errors found.private static final StringEncoding of Java source file (not property files).(package private) static final StringExtension for java source files.private static final StringMargin to write before theKEY_MODIFIERS.private static final StringThe Java modifiers applies on the key constants to be generated.private static final StringThe name of the inner class which will contains key values.(package private) static final StringExtension for properties source files.Resource keys and their localized values.private static final StringExtension for resource target files.private final File[]The resources bundle base classes.private final FileThe 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 booleanReturnstrueif the given file is a property file.final intCompares two resource keys.protected voidLogs the given message at theINFOlevel.private voidLoads the existing key values from the source file.private voidloadProperties(File file) Loads all properties from a.propertiesfile.private static PropertiesloadRawProperties(File file) Loads the specified property file.private static StringReturns the path of the given file relative to the given directory.intrun()Runs the resource compiler.private voidScans the package ofbundleClassfor its.propertiesfiles.private static StringtoMessageFormatString(String text) Changes a "normal" text string into a pattern compatible withMessageFormat.private voidLogs the given message at theWARNINGlevel.protected voidLogs the given message at theWARNINGlevel.private voidCreates a source file for resource keys.private voidWrites UTF file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 ofbundleClassfor its.propertiesfiles. The following methods are invoked by this method:- Throws:
IOException- if an input/output operation failed.
-
accept
Returnstrueif the given file is a property file.- Specified by:
acceptin interfaceFilenameFilter- Parameters:
directory- the directory (ignored).name- the file name.- Returns:
trueif 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.propertiesfile. 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 legalMessageFormatpatterns 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. Objecto1ando2are usuallyStringobjects representing resource keys (for example, "MISMATCHED_DIMENSION"), but may also beMap.Entry.- Specified by:
comparein 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 theINFOlevel. The default implementation just sent it to the standard output stream.- Parameters:
message- the message to log.
-
warning
Logs the given message at theWARNINGlevel. The default implementation just sent it to the standard output stream.- Parameters:
message- the message to log.
-
warning
Logs the given message at theWARNINGlevel.- Parameters:
file- file that produced the error, ornullif none.key- resource key that produced the error, ornullif none.message- the message string.exception- an optional exception that is the cause of this warning.
-