xmlBlaster 2.2.0 client API

org.xmlBlaster.util
Class FileLocator

java.lang.Object
  extended by org.xmlBlaster.util.FileLocator

public class FileLocator
extends java.lang.Object


Constructor Summary
FileLocator(Global glob)
          Constructor.
 
Method Summary
static void appendToFile(java.lang.String outName, java.lang.String str)
          Append data from into a file.
static java.lang.String concatPath(java.lang.String path, java.lang.String name)
          Concatenate a filename to a path (DOS and UNIX, checks for separator).
 java.lang.String[] createXmlBlasterSearchPath()
           
static boolean deleteDir(java.io.File dir)
          Deletes all files and subdirectories under dir.
static void deleteFile(java.lang.String parent, java.lang.String fileName)
           
static java.lang.String extensionToMime(java.lang.String extension, java.lang.String defaultVal)
          Convert some file extensions to MIME types.
 java.lang.String findFile(java.lang.String[] path, java.lang.String filename)
          Searches in the given path for the specified filename.
 java.lang.String findFile(java.lang.String path, java.lang.String filename)
          finds the file in the given path.
 java.lang.String findFile(java.lang.String path, java.lang.String separator, java.lang.String filename)
          finds the file in the given path.
 java.net.URL findFileInXmlBlasterSearchPath(java.lang.String propertyName, java.lang.String filename)
          tries to find a file according to the xmlBlaster Strategy.
static java.lang.String getBody(java.lang.String fileName)
          Strip the path and the file name extension.
static java.lang.String getExtension(java.lang.String fileName)
          Return the file name extension.
static void main(java.lang.String[] args)
          java org.xmlBlaster.util.FileLocator -pluginsFile http://www.xmlblaster.org/empty.html
 java.lang.String[] parsePath(java.lang.String pathAsString, java.lang.String separator)
          Parses the given Path into an array of String.
 java.lang.String read(java.net.URL url)
           
static java.lang.String readAsciiFile(java.lang.String fileName)
          Read a file into String.
static java.lang.String readAsciiFile(java.lang.String parent, java.lang.String child)
          Read a file into String.
static byte[] readFile(java.io.File f)
           
static byte[] readFile(java.lang.String fileName)
          Read a file into byte[].
static byte[] readFile(java.lang.String parent, java.lang.String fileName)
          Read a file into byte[].
static void writeFile(java.lang.String name, byte[] arr)
          Write data from StringBuffer into a file.
static void writeFile(java.lang.String name, java.lang.String str)
          Write data from StringBuffer into a file.
static void writeFile(java.lang.String parent, java.lang.String child, byte[] arr)
          Write data from byte[] into a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileLocator

public FileLocator(Global glob)
Constructor. It does nothing but initializing the log and assigning the global.

Method Detail

findFile

public final java.lang.String findFile(java.lang.String[] path,
                                       java.lang.String filename)
                                throws XmlBlasterException
Searches in the given path for the specified filename. If the file has not been found in the given path, then null is returned. Otherwise the complete (absolute) path of the file is returned.

Parameters:
path - the path on which to search for the given file.
filename - the name of the file to search. NOTE: if it is an absolute filename, then the path is ignored and a warning is written to the log.
Throws:
XmlBlasterException - with error code resource.configuration if either the file has been found but it can not be read, or if it is a directory. Note that if there are several files in the given path and the first one found is either read protected or is a directory, then the second is taken and no exception is thrown.

parsePath

public final java.lang.String[] parsePath(java.lang.String pathAsString,
                                          java.lang.String separator)
Parses the given Path into an array of String. If the input path was null, null is returned. If the input path was empty null is returned. Otherwise all path are returned. If the separator is null, null is returned.


findFile

public final java.lang.String findFile(java.lang.String path,
                                       java.lang.String separator,
                                       java.lang.String filename)
                                throws XmlBlasterException
finds the file in the given path. The separator for the path is given explicitly.

Throws:
XmlBlasterException

findFile

public final java.lang.String findFile(java.lang.String path,
                                       java.lang.String filename)
                                throws XmlBlasterException
finds the file in the given path. The path separator is implicitly set to ':'.

Throws:
XmlBlasterException

createXmlBlasterSearchPath

public final java.lang.String[] createXmlBlasterSearchPath()

read

public java.lang.String read(java.net.URL url)
                      throws XmlBlasterException
Throws:
XmlBlasterException

findFileInXmlBlasterSearchPath

public final java.net.URL findFileInXmlBlasterSearchPath(java.lang.String propertyName,
                                                         java.lang.String filename)
tries to find a file according to the xmlBlaster Strategy. The strategy is:

Parameters:
filename -
Returns:
URL the URLfrom which to read the content or null if the file/resource has not been found. Note that we return the url instead of the filename since it could be a resource and therefore it could not be opened as a normal file.
See Also:
The util.property requirement

readFile

public static final byte[] readFile(java.lang.String parent,
                                    java.lang.String fileName)
                             throws XmlBlasterException
Read a file into byte[].
Example:
byte[] data=FileLocator.readFile("/tmp", "hello.txt");

Parameters:
parent - Path to the file, can be null
fileName - Name of file
Returns:
data from the file
Throws:
XmlBlasterException - if the file is not readable or any error occurred while reading the file.

readFile

public static final byte[] readFile(java.io.File f)
                             throws XmlBlasterException
Throws:
XmlBlasterException

writeFile

public static final void writeFile(java.lang.String parent,
                                   java.lang.String child,
                                   byte[] arr)
                            throws XmlBlasterException
Write data from byte[] into a file.

Parameters:
parent - the path, can be null
child - the name
arr - data
Throws:
XmlBlasterException

writeFile

public static final void writeFile(java.lang.String name,
                                   java.lang.String str)
                            throws XmlBlasterException
Write data from StringBuffer into a file.

Parameters:
outName - name of file including path
str - data
Throws:
XmlBlasterException

writeFile

public static final void writeFile(java.lang.String name,
                                   byte[] arr)
                            throws XmlBlasterException
Write data from StringBuffer into a file.

Parameters:
outName - name of file including path
str - some binary data
Throws:
XmlBlasterException

appendToFile

public static final void appendToFile(java.lang.String outName,
                                      java.lang.String str)
                               throws XmlBlasterException
Append data from into a file.

Parameters:
outName - name of file including path
str - Text
Throws:
XmlBlasterException

readAsciiFile

public static final java.lang.String readAsciiFile(java.lang.String fileName)
                                            throws XmlBlasterException
Read a file into String.

Parameters:
fileName - Complete name of file
Returns:
ASCII data from the file
null on error
Throws:
XmlBlasterException

readAsciiFile

public static final java.lang.String readAsciiFile(java.lang.String parent,
                                                   java.lang.String child)
                                            throws XmlBlasterException
Read a file into String.
Example:
String data=FileUtil.readAsciiFile("/tmp/hello");

Parameters:
parent - Path to the file
fileName - name of file
Returns:
ASCII data from the UTF-8 encoded file
null on error
Throws:
XmlBlasterException

readFile

public static final byte[] readFile(java.lang.String fileName)
                             throws XmlBlasterException
Read a file into byte[].

Parameters:
fileName - Complete name of file
Returns:
data from the file
Throws:
JUtilsException - if the file is not readable or any error occured while reading the file.
XmlBlasterException

deleteFile

public static final void deleteFile(java.lang.String parent,
                                    java.lang.String fileName)

concatPath

public static java.lang.String concatPath(java.lang.String path,
                                          java.lang.String name)
Concatenate a filename to a path (DOS and UNIX, checks for separator).

Parameters:
path - for example "/tmp"
name - for example "hello.txt"
Returns:
"/tmp/hello.txt"

getExtension

public static java.lang.String getExtension(java.lang.String fileName)
Return the file name extension.

Parameters:
fileName - for example "/tmp/hello.txt"
Returns:
extension of the filename "txt"

getBody

public static java.lang.String getBody(java.lang.String fileName)
Strip the path and the file name extension.

Parameters:
fileName - for example "/tmp/hello.txt"
Returns:
filename without extension "hello"

deleteDir

public static boolean deleteDir(java.io.File dir)
Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false. Thanks to "The Java Developers Almanac 1.4"


extensionToMime

public static java.lang.String extensionToMime(java.lang.String extension,
                                               java.lang.String defaultVal)
Convert some file extensions to MIME types.

A candidate for a property file like /etc/httpd/mime.types

Parameters:
extension - for example "xml"
defaultVal - for example "text/plain"
Returns:
for example "text/xml"

main

public static void main(java.lang.String[] args)
java org.xmlBlaster.util.FileLocator -pluginsFile http://www.xmlblaster.org/empty.html

Parameters:
args -

xmlBlaster 2.2.0 client API

Copyright © 1999-2014 The xmlBlaster.org contributers.