hmi.util
Class Resources

java.lang.Object
  extended by hmi.util.Resources

public class Resources
extends Object

Resources are objects that serve as an ``anchor'' for loading files called ``resources''. Such files are assumed to be at a fixed place within the directory structure associated with a project, so no interactive file dialog is needed. They are, however, not assumed to be at a fixed place within the file system of the OS, so as to avoid undesirable system dependencies. Think of a situation where an application is packaged in the form of a single executable jar file, that has to run from within some unknown directory on an unknown machine. In this case, about the only location that we can rely on is the jar archive itself, so resource files should be specified relative to the jar archive. A Resources object determines a data directory inside such a jar archive, and will open files inside this directory, via the getReader() or getFileStream method. By convention the name of this data directory follows the package structure, so for example, the data used by some class inside the project.vmr package shouold read data from files inside the project/vmr data directory. When the application is not run from a jar archive, the same data directory should be available, and should be included in the java run time classpath. By convention, we use a "data" or "resources" directory inside java projects, which is included in the the java classpath, either inside the Java development tool or inside the ant build scripts.

Author:
Job Zwiers

Field Summary
private static ClassLoader loader
           
private static Logger logger
           
private  boolean logging
           
private  String resourceDir
           
private  String resourceRoot
           
 
Constructor Summary
private Resources()
           
  Resources(String resourceDir)
          Create a new Resources object, for a specified resource directory.
 
Method Summary
private static String adjustPath2(String rootPath)
           
 BufferedInputStream getInputStream(String fileName)
          Like getReader(filename), but returns a buffered InputStream, rather than a buffered Reader.
 BufferedReader getReader(String fileName)
          Opens a file, and returns a buffered Reader for that file.
 String getResourceDirectory()
          returns the resource directory for this Resources object
 String getResourceRoot()
          returns the resource root directory for this Resources object
private  InputStream getUnbufferedInputStream(String fileName)
           
 String read(String fileName)
          Tries to read the specified file, and returns the contents as a String.
 void setLogging(boolean logging)
          Turns logging on or off
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resourceRoot

private String resourceRoot

loader

private static ClassLoader loader

logger

private static Logger logger

logging

private boolean logging

resourceDir

private String resourceDir
Constructor Detail

Resources

private Resources()

Resources

public Resources(String resourceDir)
Create a new Resources object, for a specified resource directory. This directory is searched for on the run time classpath.

Method Detail

getResourceDirectory

public String getResourceDirectory()
returns the resource directory for this Resources object


getInputStream

public BufferedInputStream getInputStream(String fileName)
Like getReader(filename), but returns a buffered InputStream, rather than a buffered Reader. Returns null when the resource file could not be found.


getUnbufferedInputStream

private InputStream getUnbufferedInputStream(String fileName)

getReader

public BufferedReader getReader(String fileName)
Opens a file, and returns a buffered Reader for that file. The file is searched for in the resource directory determined by this Resources object. Assume that this method is called for a class with name C that extends "Resources". Call the package prefix for this C class "package". If "filename" is a relative path, i.e. if it does not start with a "/", then the package prefix is prepended, and the resulting file is searched for on the classpath. For the HMI projects, we assume that the data directory is included in the classpath. If "filename" is an absolute file name", than the package prefix is ignored, and the file is searched for on the class path. Returns null when the resource file could not be found.


read

public String read(String fileName)
            throws IOException
Tries to read the specified file, and returns the contents as a String. If the file cannot be opened, a null result is returned.

Throws:
IOException

setLogging

public void setLogging(boolean logging)
Turns logging on or off


adjustPath2

private static String adjustPath2(String rootPath)

getResourceRoot

public String getResourceRoot()
returns the resource root directory for this Resources object