Package net.sf.colossus.util
Class StaticResourceLoader
java.lang.Object
net.sf.colossus.util.StaticResourceLoader
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
Nested ClassesModifier and TypeClassDescriptionprivate static class
Class ColossusClassLoader allows for class loading outside the CLASSPATH, i.e. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ClassLoader
private static final StaticResourceLoader.ColossusClassLoader
static final Font
private static final String
private static final int
private static final int
static final String
private static final String[]
static final String
private static final Logger
private static final String
static final String
private static String
private static int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Color
colorFromFilename
(String filename, String prefix) Extract a color from a filename, ignoring a prefix.private static String
colorNameFromFilename
(String filename, String prefix) Extract a color name from a filename, ignoring a prefixprivate static Image
createColorizedImage
(String filename, Color color, List<String> directories, int width, int height) Create a colorized version of the image contained in the given file.private static Image
createNameImage
(int width, int height, String name, boolean down, Color color) Create an Image with only the given String on it.private static Image
createNumberImage
(int width, int height, int value, boolean right, Color color) Create an Image with only the given number on it.private static Image
createPlainImage
(int width, int height, Color color) Create an Image that is only a plain rectangle.private static Image
createPlainImage
(int width, int height, Color color, boolean border) Create an Image that is only a plain rectangle, with an optional border.private static Image
createPlainImage
(int width, int height, 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 String
fixFilename
(String filename) Fix a filename by replacing space with underscore.static byte[]
getBytesFromFile
(String filename, List<String> directories, boolean cachedOnly, boolean ignoreFail) Return the content of the specified file as an array of byte.private static byte[]
Return the content of the specified InputStream as an array of byte.static Image
getCompositeImage
(String[] filenames, List<String> directories, int width, int height) Return the composite image made from blending the given filenames from the given directories.static Document
getDocument
(String filename, List<String> directories) Return the first Document from file of name filename in the list of directories.static List<org.jdom.Element>
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 Image
Return the first Image of name filename in the list of directories.static ImageIcon
getImageIcon
(String filename, List<String> directories, int width, int height) Return the first ImageIcon of name filename in the list of directories.static InputStream
getInputStream
(String filename, List<String> directories) Return the first InputStream from file of name filename in the list of directories.private static InputStream
getInputStream
(String filename, List<String> directories, boolean remote, boolean cachedOnly, boolean ignoreFail) Return the first InputStream from file of name filename in the list of directories.private static InputStream
getInputStreamFromBytes
(byte[] data) Return the content of the specified byte array as an InputStream.static InputStream
getInputStreamIgnoreFail
(String filename, List<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 String
Return the key to use in the image cache.private static String
Return the key to use in the image and file caches.static Object
getNewObject
(String className, List<String> directories) Create an instance of the class whose name is in parameter.static Object
getNewObject
(String className, List<String> directories, Object[] parameter) Create an instance of the class whose name is in parameter, using parameters.static OutputStream
getOutputStream
(String filename, List<String> directories) Return the first OutputStream from file of name filename in the list of directories.static String
Give the String to mark directories.private static int
numberFromFilename
(String filename, String prefix) Extract a number from a filename, ignoring a prefix.static void
empty the cache so that all files have to be reloadedstatic void
empty the cache so that all Chits have to be redrawnstatic void
putIntoFileCache
(String mapKey, byte[] data) Force adding the given data as belonging to the given key in the file cache.static void
static void
setDataServer
(String server, int port) private static Image
tryBuildingNonexistentImage
(String filename, int width, int height, List<String> directories) Try to build an image when there is no source file to create it.private static Image
tryLoadImageFromFile
(String filename, String path, int width, int height) Try loading the file with the given filename in the given path as an Image.private static ImageIcon
tryLoadImageIconFromResource
(String filename, 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
(Image image) Wait until the Image in parameter is fully drawn.
-
Field Details
-
LOGGER
-
FILESERVER_IGNOREFAIL_SIGNAL
- See Also:
-
KEY_CONTENT_TYPE
- See Also:
-
DEFAULT_FONT_NAME
- See Also:
-
DEFAULT_FONT_STYLE
private static final int DEFAULT_FONT_STYLE- See Also:
-
DEFAULT_FONT_SIZE
private static final int DEFAULT_FONT_SIZE- See Also:
-
DEFAULT_FONT
-
pathSeparator
- See Also:
-
imageExtension
-
baseCL
-
cl
-
imageCache
-
fileCache
-
REQUEST_TOKEN_SEPARATOR
- See Also:
-
server
-
serverPort
private static int serverPort
-
-
Constructor Details
-
StaticResourceLoader
public StaticResourceLoader()
-
-
Method Details
-
setDataServer
-
resetDataServer
public static void resetDataServer() -
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
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 ImageIcon getImageIcon(String filename, List<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
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 ImageIcon tryLoadImageIconFromResource(String filename, 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
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
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 InputStream getInputStream(String filename, List<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(String filename, List<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
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
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
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
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
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
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 Image getCompositeImage(String[] filenames, List<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 Image tryBuildingNonexistentImage(String filename, int width, int height, List<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 Image createNumberImage(int width, int height, int value, boolean right, 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
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
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
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 Image createPlainImage(int width, int height, 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 createcolor
- 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 Image createColorizedImage(String filename, Color color, List<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
Wait until the Image in parameter is fully drawn.- Parameters:
image
- Image to wait upon.
-
numberFromFilename
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
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
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
Fix a filename by replacing space with underscore.- Parameters:
filename
- Filename to fix.- Returns:
- The fixed filename.
-
getNewObject
public static Object getNewObject(String className, List<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 Object getNewObject(String className, List<String> directories, 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
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:
-
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.
-