org.yestech.lib.io.FileLocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yeslib Show documentation
Show all versions of yeslib Show documentation
A collection of classes that can be used across yestech artifacts/components, but must not be dependant
on any yestech component. Most of the code is utility type code. When more than a few classes are
found to be in a package or the package start to handle more that a few reposibilities then a new
independant component is created and the existing code in yeslib is ported to the new component.
/*
* Copyright LGPL3
* YES Technology Association
* http://yestech.org
*
* http://www.opensource.org/licenses/lgpl-3.0.html
*/
package org.yestech.lib.io;
import java.io.InputStream;
import java.net.URL;
/**
* This class will find any file located in the users classpath.
*
*
* @author Arthur Copeland
*
*/
final public class FileLocator {
//--------------------------------------------------------------------------
// S T A T I C V A R I A B L E S
//--------------------------------------------------------------------------
/**
* Holds the default filename -> extension delimeter
*/
private final static String FILE_EXTENSION_DELIMETER = ".";
/**
* Default Ctor. Creates a new file locator
*/
private FileLocator() {
super();
}
/**
* Locates a properties file in the classes classpath and returns the URL
* associated with the file.
*
* @param file the file to locate
* @return the URL associated with the file or NULL if file not found
*/
public URL locatePropertiesFile(String file) {
return locateFile(file, "properties");
}
/**
* Locates an xml file in the classes classpath and returns the URL
* associated with the file.
*
* @param file the file to locate
* @return the URL associated with the file or NULL if file not found
*/
public URL locateXMLFile(String file) {
return locateFile(file, "xml");
}
/**
* Locates a file in the classes classpath and returns the URL
* associated with the file. If the file doesn't have an extension then the
* extension parameter MUST be NULL.
*
* @param file the file to locate
* @param extension the extension of the file to locate
* @return the URL associated with the file or NULL if file not found
*/
public URL locateFile(String file, String extension) {
return getClassLoader().getResource(getFileName(file,extension));
}
/**
* Locates a file in the classes classpath and returns the URL
* associated with the file.
*
* @param file the file to locate
* @return the URL associated with the file or NULL if file not found
*/
public URL locateFile(String file) {
return locateFile(file, null);
}
/**
* Locates a properties file in the classes classpath and returns the
* InputStream associated with the file.
*
* @param file the file to locate
* @return the InputStream associated with the file or NULL if file not
* found
*/
public InputStream locatePropertiesFileAsStream(String file) {
return locateFileAsStream(file, "properties");
}
/**
* Locates an xml file in the classes classpath and returns the InputStream
* associated with the file.
*
* @param file the file to locate
* @return the InputStream associated with the file or NULL if file not
* found
*/
public InputStream locateXMLFileAsStream(String file) {
return locateFileAsStream(file, "xml");
}
/**
* Locates a file in the classes classpath and returns the
* InputStream associated with the file.
*
* @param file the file to locate
* @param extension the extension of the file to locate
* @return the InputStream associated with the file or NULL if file not
* found
*/
public InputStream locateFileAsStream(String file,
String extension) {
return getClassLoader().getResourceAsStream(getFileName(file,
extension));
}
/**
* Locates a file in the classes classpath and returns the
* InputStream associated with the file.
*
* @param file the file to locate
* @return the InputStream associated with the file or NULL if file not
* found
*/
public InputStream locateFileAsStream(String file) {
return locateFileAsStream(file, null);
}
//--------------------------------------------------------------------------
// F A C T O R Y M E T H O D S
//--------------------------------------------------------------------------
/**
* Creates a new FileLocator
*
* @return new FileLocator
*/
public static FileLocator getFile() {
return new FileLocator();
}
//--------------------------------------------------------------------------
// I N T E R N A L M E T H O D S
//--------------------------------------------------------------------------
/**
* Return the formated name of the file to find
*
* @param file FileName of the file to find
* @param extension extension to the file name
* @return the properly formatted filename to locate
*/
private String getFileName(String file, String extension) {
String fileName = file;
if (extension != null &&
!extension.startsWith(FILE_EXTENSION_DELIMETER)) {
fileName += FILE_EXTENSION_DELIMETER + extension;
}
return fileName;
}
/**
* Retrieves the ClassLoader to use when trying to locate the resource.
*/
private ClassLoader getClassLoader() {
ClassLoader cl = getClass().getClassLoader();
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
return cl;
}
}