net.sf.colossus.util
Class StaticResourceLoader

java.lang.Object
  extended by net.sf.colossus.util.StaticResourceLoader

public final class StaticResourceLoader
extends java.lang.Object

Class ResourceLoader is an utility class to load a resource from a filename and a list of directory.

Author:
Romain Dolbeau, David Ripton

Nested Class Summary
private static class StaticResourceLoader.ColossusClassLoader
          Class ColossusClassLoader allows for class loading outside the CLASSPATH, i.e.
 
Field Summary
private static java.lang.ClassLoader baseCL
           
private static StaticResourceLoader.ColossusClassLoader cl
           
static java.awt.Font DEFAULT_FONT
           
private static java.lang.String DEFAULT_FONT_NAME
           
private static int DEFAULT_FONT_SIZE
           
private static int DEFAULT_FONT_STYLE
           
private static java.util.Map<java.lang.String,byte[]> fileCache
           
static java.lang.String FILESERVER_IGNOREFAIL_SIGNAL
           
private static java.util.Map<java.lang.String,java.lang.Object> imageCache
           
private static java.lang.String[] imageExtension
           
static java.lang.String KEY_CONTENT_TYPE
           
private static java.util.logging.Logger LOGGER
           
private static java.lang.String pathSeparator
           
static java.lang.String REQUEST_TOKEN_SEPARATOR
           
private static java.lang.String server
           
private static int serverPort
           
 
Constructor Summary
StaticResourceLoader()
           
 
Method Summary
private static java.awt.Color colorFromFilename(java.lang.String filename, java.lang.String prefix)
          Extract a color from a filename, ignoring a prefix.
private static java.lang.String colorNameFromFilename(java.lang.String filename, java.lang.String prefix)
          Extract a color name from a filename, ignoring a prefix
private static java.awt.Image createColorizedImage(java.lang.String filename, java.awt.Color color, java.util.List<java.lang.String> directories, int width, int height)
          Create a colorized version of the image contained in the given file.
private static java.awt.Image createNameImage(int width, int height, java.lang.String name, boolean down, java.awt.Color color)
          Create an Image with only the given String on it.
private static java.awt.Image createNumberImage(int width, int height, int value, boolean right, java.awt.Color color)
          Create an Image with only the given number on it.
private static java.awt.Image createPlainImage(int width, int height, java.awt.Color color)
          Create an Image that is only a plain rectangle.
private static java.awt.Image createPlainImage(int width, int height, java.awt.Color color, boolean border)
          Create an Image that is only a plain rectangle, with an optional border.
private static java.awt.Image createPlainImage(int width, int height, java.awt.Color color, int t_x, int t_y, int t_w, int t_h, boolean border)
          Create an Image that only contains a colored rectangle, with an optional border.
private static java.lang.String fixFilename(java.lang.String filename)
          Fix a filename by replacing space with underscore.
static byte[] getBytesFromFile(java.lang.String filename, java.util.List<java.lang.String> directories, boolean cachedOnly, boolean ignoreFail)
          Return the content of the specified file as an array of byte.
private static byte[] getBytesFromInputStream(java.io.InputStream is)
          Return the content of the specified InputStream as an array of byte.
static java.awt.Image getCompositeImage(java.lang.String[] filenames, java.util.List<java.lang.String> directories, int width, int height)
          Return the composite image made from blending the given filenames from the given directories.
static javax.swing.text.Document getDocument(java.lang.String filename, java.util.List<java.lang.String> directories)
          Return the first Document from file of name filename in the list of directories.
static java.util.List<org.jdom.Element> getFileCacheDump()
          Dump the file cache as a List of XML "DataFile" Element, with the file key as attribute "DataFileKey", and the file data as a CDATA content.
static java.awt.Image getImage(java.lang.String filename, java.util.List<java.lang.String> directories, int width, int height)
          Return the first Image of name filename in the list of directories.
static javax.swing.ImageIcon getImageIcon(java.lang.String filename, java.util.List<java.lang.String> directories, int width, int height)
          Return the first ImageIcon of name filename in the list of directories.
static java.io.InputStream getInputStream(java.lang.String filename, java.util.List<java.lang.String> directories)
          Return the first InputStream from file of name filename in the list of directories.
private static java.io.InputStream getInputStream(java.lang.String filename, java.util.List<java.lang.String> directories, boolean remote, boolean cachedOnly, boolean ignoreFail)
          Return the first InputStream from file of name filename in the list of directories.
private static java.io.InputStream getInputStreamFromBytes(byte[] data)
          Return the content of the specified byte array as an InputStream.
static java.io.InputStream getInputStreamIgnoreFail(java.lang.String filename, java.util.List<java.lang.String> directories)
          Return the first InputStream from file of name filename in the list of directories, tell the getInputStream not to complain if not found.
private static java.lang.String getMapKey(java.lang.String[] filenames, java.util.List<java.lang.String> directories)
          Return the key to use in the image cache.
private static java.lang.String getMapKey(java.lang.String filename, java.util.List<java.lang.String> directories)
          Return the key to use in the image and file caches.
static java.lang.Object getNewObject(java.lang.String className, java.util.List<java.lang.String> directories)
          Create an instance of the class whose name is in parameter.
static java.lang.Object getNewObject(java.lang.String className, java.util.List<java.lang.String> directories, java.lang.Object[] parameter)
          Create an instance of the class whose name is in parameter, using parameters.
static java.io.OutputStream getOutputStream(java.lang.String filename, java.util.List<java.lang.String> directories)
          Return the first OutputStream from file of name filename in the list of directories.
static java.lang.String getPathSeparator()
          Give the String to mark directories.
private static int numberFromFilename(java.lang.String filename, java.lang.String prefix)
          Extract a number from a filename, ignoring a prefix.
static void purgeFileCache()
          empty the cache so that all files have to be reloaded
static void purgeImageCache()
          empty the cache so that all Chits have to be redrawn
static void putIntoFileCache(java.lang.String mapKey, byte[] data)
          Force adding the given data as belonging to the given key in the file cache.
static void resetDataServer()
           
static void setDataServer(java.lang.String server, int port)
           
private static java.awt.Image tryBuildingNonexistentImage(java.lang.String filename, int width, int height, java.util.List<java.lang.String> directories)
          Try to build an image when there is no source file to create it.
private static java.awt.Image tryLoadImageFromFile(java.lang.String filename, java.lang.String path, int width, int height)
          Try loading the file with the given filename in the given path as an Image.
private static javax.swing.ImageIcon tryLoadImageIconFromResource(java.lang.String filename, java.lang.String path, int width, int height)
          Try loading the file file with the given filename in the given path as an ImageIcon, through a Class loader.
private static void waitOnImage(java.awt.Image image)
          Wait until the Image in parameter is fully drawn.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER

FILESERVER_IGNOREFAIL_SIGNAL

public static final java.lang.String FILESERVER_IGNOREFAIL_SIGNAL
See Also:
Constant Field Values

KEY_CONTENT_TYPE

public static final java.lang.String KEY_CONTENT_TYPE
See Also:
Constant Field Values

DEFAULT_FONT_NAME

private static final java.lang.String DEFAULT_FONT_NAME
See Also:
Constant Field Values

DEFAULT_FONT_STYLE

private static final int DEFAULT_FONT_STYLE
See Also:
Constant Field Values

DEFAULT_FONT_SIZE

private static final int DEFAULT_FONT_SIZE
See Also:
Constant Field Values

DEFAULT_FONT

public static final java.awt.Font DEFAULT_FONT

pathSeparator

private static final java.lang.String pathSeparator
See Also:
Constant Field Values

imageExtension

private static final java.lang.String[] imageExtension

baseCL

private static final java.lang.ClassLoader baseCL

cl

private static final StaticResourceLoader.ColossusClassLoader cl

imageCache

private static final java.util.Map<java.lang.String,java.lang.Object> imageCache

fileCache

private static final java.util.Map<java.lang.String,byte[]> fileCache

REQUEST_TOKEN_SEPARATOR

public static final java.lang.String REQUEST_TOKEN_SEPARATOR
See Also:
Constant Field Values

server

private static java.lang.String server

serverPort

private static int serverPort
Constructor Detail

StaticResourceLoader

public StaticResourceLoader()
Method Detail

setDataServer

public static void setDataServer(java.lang.String server,
                                 int port)

resetDataServer

public static void resetDataServer()

getPathSeparator

public static java.lang.String getPathSeparator()
Give the String to mark directories.

Returns:
The String to mark directories.

purgeImageCache

public static void purgeImageCache()
empty the cache so that all Chits have to be redrawn


purgeFileCache

public static void purgeFileCache()
empty the cache so that all files have to be reloaded


getImage

public static java.awt.Image getImage(java.lang.String filename,
                                      java.util.List<java.lang.String> directories,
                                      int width,
                                      int height)
Return the first Image of name filename in the list of directories.

Parameters:
filename - Name of the Image file to load (without extension).
directories - List of directories to search (in order).
Returns:
The Image, or null if it was not found.

getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.lang.String filename,
                                                 java.util.List<java.lang.String> directories,
                                                 int width,
                                                 int height)
Return the first ImageIcon of name filename in the list of directories.

Parameters:
filename - Name of the ImageIcon file to load (without extension).
directories - List of directories to search (in order).
Returns:
The ImageIcon, or null if it was not found.

tryLoadImageFromFile

private static java.awt.Image tryLoadImageFromFile(java.lang.String filename,
                                                   java.lang.String path,
                                                   int width,
                                                   int height)
Try loading the file with the given filename in the given path as an Image.

Parameters:
filename - Name of the file to load.
path - Path to search for the file
Returns:
Resulting Image, or null if it fails.

tryLoadImageIconFromResource

private static javax.swing.ImageIcon tryLoadImageIconFromResource(java.lang.String filename,
                                                                  java.lang.String path,
                                                                  int width,
                                                                  int height)
Try loading the file file with the given filename in the given path as an ImageIcon, through a Class loader.

Parameters:
filename - Name of the file to load.
path - Path to search for the file
Returns:
Resulting ImageIcon, or null if it fails.

getInputStreamIgnoreFail

public static java.io.InputStream getInputStreamIgnoreFail(java.lang.String filename,
                                                           java.util.List<java.lang.String> directories)
Return the first InputStream from file of name filename in the list of directories, tell the getInputStream not to complain if not found.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
Returns:
The InputStream, or null if it was not found.

getInputStream

public static java.io.InputStream getInputStream(java.lang.String filename,
                                                 java.util.List<java.lang.String> directories)
Return the first InputStream from file of name filename in the list of directories.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
Returns:
The InputStream, or null if it was not found.

getInputStream

private static java.io.InputStream getInputStream(java.lang.String filename,
                                                  java.util.List<java.lang.String> directories,
                                                  boolean remote,
                                                  boolean cachedOnly,
                                                  boolean ignoreFail)
Return the first InputStream from file of name filename in the list of directories.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
remote - Ask the server for the stream.
cachedOnly - Only look in the cache file, do not try to load the file from permanent storage.
ignoreFail - (=don't complain) if file not found
Returns:
The InputStream, or null if it was not found.

getBytesFromFile

public static byte[] getBytesFromFile(java.lang.String filename,
                                      java.util.List<java.lang.String> directories,
                                      boolean cachedOnly,
                                      boolean ignoreFail)
Return the content of the specified file as an array of byte.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
cachedOnly - Only look in the cache file, do not try to load the file from permanent storage.
Returns:
An array of byte representing the content of the file, or null if it fails.

getBytesFromInputStream

private static byte[] getBytesFromInputStream(java.io.InputStream is)
Return the content of the specified InputStream as an array of byte.

Parameters:
InputStream - The InputStream to use.
Returns:
An array of byte representing the content of the InputStream, or null if it fails.

getInputStreamFromBytes

private static java.io.InputStream getInputStreamFromBytes(byte[] data)
Return the content of the specified byte array as an InputStream.

Parameters:
data - The byte array to convert.
Returns:
An InputStream whose content is the data byte array.

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String filename,
                                                   java.util.List<java.lang.String> directories)
Return the first OutputStream from file of name filename in the list of directories.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
Returns:
The OutputStream, or null if it was not found.

getDocument

public static javax.swing.text.Document getDocument(java.lang.String filename,
                                                    java.util.List<java.lang.String> directories)
Return the first Document from file of name filename in the list of directories. It also add a property of key keyContentType and of type String describing the content type of the Document. This can currently load HTML and pure text.

Parameters:
filename - Name of the file to load.
directories - List of directories to search (in order).
Returns:
The Document, or null if it was not found.

getMapKey

private static java.lang.String getMapKey(java.lang.String filename,
                                          java.util.List<java.lang.String> directories)
Return the key to use in the image and file caches.

Parameters:
filename - Name of the file.
directories - List of directories.
Returns:
A String to use as a key when storing/loading in a cache the specified file from the specified list of directories.

getMapKey

private static java.lang.String getMapKey(java.lang.String[] filenames,
                                          java.util.List<java.lang.String> directories)
Return the key to use in the image cache.

Parameters:
filenames - Array of name of files.
directories - List of directories.
Returns:
A String to use as a key when storing/loading in a cache the specified array of name of files from the specified list of directories.

getCompositeImage

public static java.awt.Image getCompositeImage(java.lang.String[] filenames,
                                               java.util.List<java.lang.String> directories,
                                               int width,
                                               int height)
Return the composite image made from blending the given filenames from the given directories.

Parameters:
filenames - Names of the Images files to load (without extension).
directories - List of directories to search (in order).
Returns:
The composite Image, or null if any part was not found.

tryBuildingNonexistentImage

private static java.awt.Image tryBuildingNonexistentImage(java.lang.String filename,
                                                          int width,
                                                          int height,
                                                          java.util.List<java.lang.String> directories)
Try to build an image when there is no source file to create it. Includes generation of some dynamic layers of images for composite image building.

Parameters:
filename - The name of the missing file.
width - Width of the image to create.
height - Height of the image to create.
directories - List of searched directories.
Returns:
The generated Image.
See Also:
#getCompositeImage(String[], List)

createNumberImage

private static java.awt.Image createNumberImage(int width,
                                                int height,
                                                int value,
                                                boolean right,
                                                java.awt.Color color)
Create an Image with only the given number on it.

Parameters:
width - Width of the image to create.
height - Height of the image to create.
value - The number to draw on the image.
right - The number is on the right side (default is left side).
color - The color to use to draw the number.
Returns:
The generated Image.

createNameImage

private static java.awt.Image createNameImage(int width,
                                              int height,
                                              java.lang.String name,
                                              boolean down,
                                              java.awt.Color color)
Create an Image with only the given String on it.

Parameters:
width - Width of the image to create.
height - Height of the image to create.
name - The String to draw on the image.
down - The name is on the bottom (default is top).
color - The color to use to draw the String.
Returns:
The generated Image.

createPlainImage

private static java.awt.Image createPlainImage(int width,
                                               int height,
                                               java.awt.Color color)
Create an Image that is only a plain rectangle.

Parameters:
width - Width of the image to create.
height - Height of the image to create.
color - The color to use to fill the rectangle.
Returns:
The generated Image.

createPlainImage

private static java.awt.Image createPlainImage(int width,
                                               int height,
                                               java.awt.Color color,
                                               boolean border)
Create an Image that is only a plain rectangle, with an optional border.

Parameters:
width - Width of the image to create.
height - Height of the image to create.
color - The color to use to fill the rectangle.
border - Whether to add a black border.
Returns:
The generated Image.

createPlainImage

private static java.awt.Image createPlainImage(int width,
                                               int height,
                                               java.awt.Color color,
                                               int t_x,
                                               int t_y,
                                               int t_w,
                                               int t_h,
                                               boolean border)
Create an Image that only contains a colored rectangle, with an optional border.

Parameters:
width - Width of the image to create.
height - Height of the image to create
color - The color to use to fill the rectangle.
t_x - Left border of the rectangle.
t_y - Top border of the rectangle.
t_w - Width of the rectangle.
t_h - Height of the rectangle.
border - Whether to add a black border.
Returns:
The generated Image.

createColorizedImage

private static java.awt.Image createColorizedImage(java.lang.String filename,
                                                   java.awt.Color color,
                                                   java.util.List<java.lang.String> directories,
                                                   int width,
                                                   int height)
Create a colorized version of the image contained in the given file.

Parameters:
filename - Name of the Image file to load.
directories - List of directories to search (in order).
color - Color to use.
Returns:
An Image composed of the content of the file, with the opaque part filled the the given color, and everythin else white. The alpha channel (aka transparency) is untouched.

waitOnImage

private static void waitOnImage(java.awt.Image image)
Wait until the Image in parameter is fully drawn.

Parameters:
image - Image to wait upon.

numberFromFilename

private static int numberFromFilename(java.lang.String filename,
                                      java.lang.String prefix)
Extract a number from a filename, ignoring a prefix.

Parameters:
filename - File name to extract from.
prefix - Prefix to ignore. Has to match the prefix of the file name, otherwise 0 will be returned.
Returns:
The extracted number.

colorNameFromFilename

private static java.lang.String colorNameFromFilename(java.lang.String filename,
                                                      java.lang.String prefix)
Extract a color name from a filename, ignoring a prefix

Parameters:
filename - File name to extract from.
prefix - Prefix to ignore. Has to match the beginning of the file name, otherwise "black" will be used as default value.
Returns:
The extracted color name.

colorFromFilename

private static java.awt.Color colorFromFilename(java.lang.String filename,
                                                java.lang.String prefix)
Extract a color from a filename, ignoring a prefix.

Parameters:
filename - File name to extract from.
prefix - Prefix to ignore.
Returns:
The extracted Color.

fixFilename

private static java.lang.String fixFilename(java.lang.String filename)
Fix a filename by replacing space with underscore.

Parameters:
filename - Filename to fix.
Returns:
The fixed filename.

getNewObject

public static java.lang.Object getNewObject(java.lang.String className,
                                            java.util.List<java.lang.String> directories)
                                     throws ObjectCreationException
Create an instance of the class whose name is in parameter.

Parameters:
className - The name of the class to use.
directories - List of directories to search (in order).
Returns:
A new object, instance from the given class.
Throws:
ObjectCreationException - iff the object could not be created

getNewObject

public static java.lang.Object getNewObject(java.lang.String className,
                                            java.util.List<java.lang.String> directories,
                                            java.lang.Object[] parameter)
                                     throws ObjectCreationException
Create an instance of the class whose name is in parameter, using parameters. If no parameters are given, the default constructor is used.

Parameters:
className - The name of the class to use, must not be null.
directories - List of directories to search (in order), must not be null.
parameter - Array of parameters to pass to the constructor, can be null.
Returns:
A new object, instance from the given class or null if instantiation failed.
Throws:
ObjectCreationException - iff the object could not be created for some reason

putIntoFileCache

public static void putIntoFileCache(java.lang.String mapKey,
                                    byte[] data)
Force adding the given data as belonging to the given key in the file cache.

Parameters:
mapKey - Key to use in the cache.
data - File content to add.
See Also:
getMapKey(String, List), getMapKey(String[], List)

getFileCacheDump

public static java.util.List<org.jdom.Element> getFileCacheDump()
Dump the file cache as a List of XML "DataFile" Element, with the file key as attribute "DataFileKey", and the file data as a CDATA content.

Returns:
A list of XML Element.