org.deegree_impl.model.resources
Class ResourceCompiler

java.lang.Object
  extended byorg.deegree_impl.model.resources.ResourceCompiler
All Implemented Interfaces:
java.util.Comparator

final class ResourceCompiler
extends java.lang.Object
implements java.util.Comparator

Resources compiler. This class is run from the command-line at compile-time only. ResourceCompiler scan for .properties files and copy their content to .utf files using UTF8 encoding. It also check for key validity (making sure that the same set of keys is defined in every language) and change values for MessageFormat compatibility. Lastly, it create a ResourceKeys.java source file declaring resource keys as integer constants.

ResourceCompiler and all ResourceKeys classes don't need to be included in the final JAR file. They are used at compile-time only and no other classes should keep reference to them.

Version:
1.0
Author:
Martin Desruisseaux

Field Summary
private  java.util.Map allocatedIDs
          Integer IDs allocated to resource keys.
private static java.lang.String ARGUMENT_COUNT_PREFIX
          Prefix for argument count in resource key names.
private static java.lang.String CLASS_NAME
          The class name for the interfaces to be generated.
private static java.lang.String[] ORDER
          Special order for resource keys starting with the specified prefix.
private static java.lang.String PREFIX
          File name prefix for properties and resources files.
private static java.lang.String PROPERTIES_EXT
          Extension for properties files.
private  java.util.Map resources
          Resources keys and their localized values.
private static java.lang.String RESOURCES_EXT
          Extension for resources files.
 
Constructor Summary
private ResourceCompiler(java.io.File directory)
          Construct a new ResourceCompiler.
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          Compare two resource keys.
private  void loadPropertyFile(java.io.File file)
          Load all properties from a .properties file.
static void main(java.lang.String[] args)
          Run the resources compilator.
private static void scanForResources(java.io.File directory)
          Scan the specified directory and all subdirectory for resources.
private static java.lang.String toClassName(java.io.File file)
          Returns the class name for the specified source file.
private static java.lang.String toMessageFormatString(java.lang.String text)
          Change a "normal" text string into a pattern compatible with MessageFormat.
private static java.lang.String toRelative(java.io.File path)
          Make a file path relative to the classpath.
private static java.io.File toSourceFile(java.lang.String classname)
          Returns the source file for the specified class.
private static void warning(java.io.File file, java.lang.String key, java.lang.String message, java.lang.Exception exception)
          Print a message to the error output stream System.err.
private  void writeJavaSource(java.io.File file)
          Create a source file for resources keys.
private static void writeMultiLines(java.io.BufferedWriter out, java.lang.String text)
          Write a multi-lines text to the specified output stream.
private  void writeUTFFile(java.io.File file)
          Write UTF file.
private static void writeWhiteSpaces(java.io.Writer out, int count)
          Write count spaces to the out stream.
 
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
 

Field Detail

ORDER

private static final java.lang.String[] ORDER
Special order for resource keys starting with the specified prefix.


CLASS_NAME

private static final java.lang.String CLASS_NAME
The class name for the interfaces to be generated.

See Also:
Constant Field Values

PREFIX

private static final java.lang.String PREFIX
File name prefix for properties and resources files.

See Also:
Constant Field Values

PROPERTIES_EXT

private static final java.lang.String PROPERTIES_EXT
Extension for properties files.

See Also:
Constant Field Values

RESOURCES_EXT

private static final java.lang.String RESOURCES_EXT
Extension for resources files.

See Also:
Constant Field Values

ARGUMENT_COUNT_PREFIX

private static final java.lang.String ARGUMENT_COUNT_PREFIX
Prefix for argument count in resource key names. For example a resource expecting one argument may has a key name like "HELLO_$1".

See Also:
Constant Field Values

allocatedIDs

private final java.util.Map allocatedIDs
Integer IDs allocated to resource keys. This map use <Integer,String> entries.


resources

private final java.util.Map resources
Resources keys and their localized values. This map use <String,String> entries.

Constructor Detail

ResourceCompiler

private ResourceCompiler(java.io.File directory)
                  throws java.io.IOException
Construct a new ResourceCompiler. This method will immediately looks for a ResourceKeys.class file. If one is found, integer keys are loaded in order to reuse same values.

Parameters:
directory - The resource directory. This directory should or will contains the following input and output files:
Throws:
java.io.IOException - if an input/output operation failed.
Method Detail

scanForResources

private static void scanForResources(java.io.File directory)
                              throws java.io.IOException
Scan the specified directory and all subdirectory for resources.

Parameters:
directory - The root directory.
Throws:
java.io.IOException - if an input/output operation failed.

loadPropertyFile

private void loadPropertyFile(java.io.File file)
                       throws java.io.IOException
Load all properties from a .properties file. Resource keys are checked for naming convention (i.e. resources expecting some arguments must have a key ending with "_$n" where "n" is the number of arguments). This method transform resource values in legal MessageFormat patterns when necessary.

Parameters:
file - Resource file to read.
Throws:
java.io.IOException - if an input/output operation failed.

writeUTFFile

private void writeUTFFile(java.io.File file)
                   throws java.io.IOException
Write UTF file. Method loadPropertyFile(java.io.File) should be invoked before to writeUTFFile.

Parameters:
file - The destination file.
Throws:
java.io.IOException - if an input/output operation failed.

toSourceFile

private static java.io.File toSourceFile(java.lang.String classname)
Returns the source file for the specified class.


toClassName

private static java.lang.String toClassName(java.io.File file)
Returns the class name for the specified source file. The returned class name to not include package name.


toRelative

private static java.lang.String toRelative(java.io.File path)
                                    throws java.io.IOException
Make a file path relative to the classpath. The file path may be relative (to current chdir) or absolute. This method find the canonical form of path and compare it with canonical forms of every paths in the class path. If a classpath matchs the begining of path, then the corresponding part of path is removed. If there is more than one matches, the one resulting in the shortest relative path is choosen.

Throws:
java.io.IOException

toMessageFormatString

private static java.lang.String toMessageFormatString(java.lang.String text)
Change a "normal" text string into a pattern compatible with MessageFormat. The main operation consist in changing ' for '', except for '{' and '}' strings.


warning

private static void warning(java.io.File file,
                            java.lang.String key,
                            java.lang.String message,
                            java.lang.Exception exception)
Print a message to the error output stream System.err.

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.

writeWhiteSpaces

private static void writeWhiteSpaces(java.io.Writer out,
                                     int count)
                              throws java.io.IOException
Write count spaces to the out stream.

Throws:
java.io.IOException - if an input/output operation failed.

writeMultiLines

private static void writeMultiLines(java.io.BufferedWriter out,
                                    java.lang.String text)
                             throws java.io.IOException
Write a multi-lines text to the specified output stream. All occurences of '\r' will be replaced by the line separator for the underlying operating system.

Parameters:
out - The output stream.
text - The text to write.
Throws:
java.io.IOException - if an input/output operation failed.

writeJavaSource

private void writeJavaSource(java.io.File file)
                      throws java.io.IOException
Create a source file for resources keys.

Parameters:
file - The destination file.
Throws:
java.io.IOException - if an input/output operation failed.

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Compare two resource keys. Object o1 and o2 are usually String objects representing resource keys (for example "MISMATCHED_DIMENSION"). This method compares strings as of String.compareTo(java.lang.Object), except that string starting with one of the prefix enumetated in ORDER will appear last in the sorted array.

Specified by:
compare in interface java.util.Comparator

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Run the resources compilator.

Parameters:
args - Command-line arguments.
Throws:
java.io.IOException - if an input/output operation failed.