Class StaticResourceLoader

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

public final class StaticResourceLoader extends Object
Class ResourceLoader is an utility class to load a resource from a filename and a list of directory.
Author:
Romain Dolbeau, David Ripton
  • Field Details

  • Constructor Details

    • StaticResourceLoader

      public StaticResourceLoader()
  • Method Details

    • setDataServer

      public static void setDataServer(String server, int port)
    • resetDataServer

      public static void resetDataServer()
    • getPathSeparator

      public static 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 Image getImage(String filename, List<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 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

      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.
      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

      public 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.
      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 InputStream getInputStream(String filename, List<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 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

      private static byte[] getBytesFromInputStream(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 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 OutputStream getOutputStream(String filename, List<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 Document getDocument(String filename, List<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 String getMapKey(String filename, List<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 String getMapKey(String[] filenames, List<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 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

      private static Image createNameImage(int width, int height, String name, boolean down, 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 Image createPlainImage(int width, int height, 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 Image createPlainImage(int width, int height, 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 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 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 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

      private static void waitOnImage(Image image)
      Wait until the Image in parameter is fully drawn.
      Parameters:
      image - Image to wait upon.
    • numberFromFilename

      private static int numberFromFilename(String filename, 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 String colorNameFromFilename(String filename, 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 Color colorFromFilename(String filename, 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 String fixFilename(String filename)
      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

      public static void putIntoFileCache(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:
    • getFileCacheDump

      public static 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.