com.silverpeas.util
Class FileUtil

java.lang.Object
  extended by com.silverpeas.util.FileUtil
All Implemented Interfaces:
MimeTypes

public class FileUtil
extends Object
implements MimeTypes


Field Summary
static String BASE_CONTEXT
           
static String CONTEXT_TOKEN
           
 
Fields inherited from interface com.silverpeas.util.MimeTypes
ARCHIVE_MIME_TYPE, ARCHIVE_MIME_TYPES, BZ2_ARCHIVE_MIME_TYPE, DEFAULT_MIME_TYPE, EAR_EXTENSION, EXCEL_2007_EXTENSION, EXCEL_2007_MIME_TYPE, EXCEL_2007_TEMPLATE_MIME_TYPE, EXCEL_MIME_TYPE1, EXCEL_MIME_TYPE2, GUNZIP_ARCHIVE_MIME_TYPE, GZ_ARCHIVE_MIME_TYPE, HTML_MIME_TYPE, JAR_EXTENSION, JAVA_ARCHIVE_MIME_TYPE, JSP_EXTENSION, JSP_MIME_TYPE, MIME_TYPE_OO_DB, MIME_TYPE_OO_DIAGRAM, MIME_TYPE_OO_FORMATTED_TEXT, MIME_TYPE_OO_FORMULA, MIME_TYPE_OO_GRAPHICS, MIME_TYPE_OO_IMAGE, MIME_TYPE_OO_MASTER, MIME_TYPE_OO_PRESENTATION, MIME_TYPE_OO_SPREADSHEET, MS_OFFICE_MIME_TYPES, OPEN_OFFICE_MIME_TYPES, PDF_MIME_TYPE, PHP_EXTENSION, PHP_MIME_TYPE, PLAIN_TEXT_MIME_TYPE, POWERPOINT_2007_EXTENSION, POWERPOINT_2007_MIME_TYPE, POWERPOINT_2007_TEMPLATE_MIME_TYPE, POWERPOINT_MIME_TYPE1, POWERPOINT_MIME_TYPE2, RSS_MIME_TYPE, RTF_EXTENSION, RTF_MIME_TYPE, SERVLET_HTML_CONTENT_TYPE, SHORT_ARCHIVE_MIME_TYPE, SPINFIRE_MIME_TYPE, TARGZ_ARCHIVE_MIME_TYPE, WAR_EXTENSION, WORD_2007_EXTENSION, WORD_2007_MIME_TYPE, WORD_2007_TEMPLATE_MIME_TYPE, WORD_MIME_TYPE, XHTML_MIME_TYPE, XML_MIME_TYPE
 
Method Summary
static void checkPathNotRelative(String path)
          Checking that the path doesn't contain relative navigation between pathes.
static String convertBundleName(String bundle)
          Utility method for migration of Silverpeas configuration from : com.silverpeas, com.stratelia.silverpeas, com.stratelia.webactiv to org.silverpeas
static String convertFilePath(File file)
           
static String convertPathToServerOS(String undeterminedOsPath)
          Convert a path to the current OS path format.
static String convertResourceName(String resource)
          Utility method for migration of Silverpeas configuration from : com/silverpeas, com/stratelia/silverpeas, com/stratelia/webactiv to org/silverpeas
static void copyFile(File source, File destination)
          Copies the specified source file to the specified destination.
static boolean deleteEmptyDir(File directory)
           
static void forceDeletion(File fileToDelete)
          Forces the deletion of the specified file.
static String[] getAttachmentContext(String context)
          Create the array of strings this array represents the repertories where the files must be stored.
static String getFilename(String fileName)
           
static String getMimeType(String fileName)
          Detects the mime-type of the specified file.
static boolean isArchive(String filename)
          Indicates if the current file is of type archive.
static boolean isFileSecure(String fileName, String intendedDir)
           
static boolean isImage(String filename)
          Indicates if the current file is of type image.
static boolean isMail(String filename)
          Indicates if the current file is of type mail.
static boolean isOpenOfficeCompatible(String filename)
           
static boolean isPdf(String filename)
          Indicates if the current file is of type PDF.
static boolean isSpinfireDocument(String filename)
          If 3D document.
static boolean isWindows()
          Indicates if the OS is from the Microsoft Windows familly
static Collection<File> listFiles(File directory, String[] extensions, boolean recursive)
           
static Collection<File> listFiles(File directory, String[] extensions, boolean caseSensitive, boolean recursive)
           
static ResourceBundle loadBundle(String bundleName, Locale locale)
          Loads a ResourceBundle from the Silverpeas configuration directory.
static void loadProperties(Properties properties, String resourceName)
          Loads loads the resource into the specified properties.
static File[] moveAllFilesAtRootFolder(File rootFolder)
          Moves all files from sub folders to the given root folder and deletes after all the sub folders.
static File[] moveAllFilesAtRootFolder(File rootFolder, boolean deleteFolders)
          Moves all files from sub folders to the given root folder.
static void moveFile(File source, File destination)
          Moves the specified source file to the specified destination.
static byte[] readFile(File file)
          Read the content of a file in a byte array.
static String readFileToString(File file)
          Read the content of a file as text (the text is supposed to be in the UTF-8 charset).
static String validateFilename(String fileName, String intendedDir)
          Validate that fileName given in parameter is inside extraction target directory (intendedDir)
static void writeFile(File file, InputStream data)
          Write a stream into a file.
static void writeFile(File file, Reader data)
          Write a stream into a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTEXT_TOKEN

public static final String CONTEXT_TOKEN
See Also:
Constant Field Values

BASE_CONTEXT

public static final String BASE_CONTEXT
See Also:
Constant Field Values
Method Detail

convertBundleName

public static String convertBundleName(String bundle)
Utility method for migration of Silverpeas configuration from : com.silverpeas, com.stratelia.silverpeas, com.stratelia.webactiv to org.silverpeas

Parameters:
bundle - the name of the bundle.
Returns:
the name of the migrated bundle.

convertResourceName

public static String convertResourceName(String resource)
Utility method for migration of Silverpeas configuration from : com/silverpeas, com/stratelia/silverpeas, com/stratelia/webactiv to org/silverpeas

Parameters:
resource - the name of the resource.
Returns:
the name of the migrated resource.

getMimeType

public static String getMimeType(String fileName)
Detects the mime-type of the specified file. The mime-type is first extracted from its content. If the detection fails or if the file cannot be located by its specified name, then the mime-type is detected from the file extension.

Parameters:
fileName - the name of the file with its path.
Returns:
the mime-type of the specified file.

getAttachmentContext

public static String[] getAttachmentContext(String context)
Create the array of strings this array represents the repertories where the files must be stored.

Parameters:
context -
Returns:

readFile

public static byte[] readFile(File file)
                       throws IOException
Read the content of a file in a byte array.

Parameters:
file - the file to be read.
Returns:
the bytes array containing the content of the file.
Throws:
IOException

readFileToString

public static String readFileToString(File file)
                               throws IOException
Read the content of a file as text (the text is supposed to be in the UTF-8 charset).

Parameters:
file - the file to read.
Returns:
the file content as a String.
Throws:
IOException - if an error occurs while reading the file.

writeFile

public static void writeFile(File file,
                             InputStream data)
                      throws IOException
Write a stream into a file.

Parameters:
file - the file to be written.
data - the data to be written.
Throws:
IOException

writeFile

public static void writeFile(File file,
                             Reader data)
                      throws IOException
Write a stream into a file.

Parameters:
file - the file to be written.
data - the data to be written.
Throws:
IOException

loadBundle

public static ResourceBundle loadBundle(String bundleName,
                                        Locale locale)
Loads a ResourceBundle from the Silverpeas configuration directory.

Parameters:
bundleName - the name of the bundle.
locale - the locale of the bundle.
Returns:
the corresponding ResourceBundle if it exists - null otherwise.

loadProperties

public static void loadProperties(Properties properties,
                                  String resourceName)
                           throws IOException
Loads loads the resource into the specified properties.

Parameters:
properties - the properties to be loaded with the resource.
resourceName - the name of the resource.
Throws:
IOException

isWindows

public static boolean isWindows()
Indicates if the OS is from the Microsoft Windows familly

Returns:
true if the OS is from the Microsoft Windows familly - false otherwise.

isSpinfireDocument

public static boolean isSpinfireDocument(String filename)
If 3D document.

Parameters:
filename - the name of the file.
Returns:
true or false

isArchive

public static boolean isArchive(String filename)
Indicates if the current file is of type archive.

Parameters:
filename - the name of the file.
Returns:
true is the file s of type archive - false otherwise.

isImage

public static boolean isImage(String filename)
Indicates if the current file is of type image.

Parameters:
filename - the name of the file.
Returns:
true is the file is of type image - false otherwise.

isMail

public static boolean isMail(String filename)
Indicates if the current file is of type mail.

Parameters:
filename - the name of the file.
Returns:
true is the file is of type mail - false otherwise.

isPdf

public static boolean isPdf(String filename)
Indicates if the current file is of type PDF.

Parameters:
filename - the name of the file.
Returns:
true is the file s of type archive - false otherwise.

isOpenOfficeCompatible

public static boolean isOpenOfficeCompatible(String filename)

checkPathNotRelative

public static void checkPathNotRelative(String path)
                                 throws RelativeFileAccessException
Checking that the path doesn't contain relative navigation between pathes.

Parameters:
path -
Throws:
RelativeFileAccessException

listFiles

public static Collection<File> listFiles(File directory,
                                         String[] extensions,
                                         boolean recursive)

listFiles

public static Collection<File> listFiles(File directory,
                                         String[] extensions,
                                         boolean caseSensitive,
                                         boolean recursive)

forceDeletion

public static void forceDeletion(File fileToDelete)
                          throws IOException
Forces the deletion of the specified file. If the write property of the file to delete isn't set, this property is then set before deleting.

Parameters:
fileToDelete - file to delete.
Throws:
IOException - if the deletion failed or if the file doesn't exist.

moveFile

public static void moveFile(File source,
                            File destination)
                     throws IOException
Moves the specified source file to the specified destination. If the destination exists, it is then replaced by the source; if the destination is a directory, then it is deleted with all of its contain.

Parameters:
source - the file to move.
destination - the destination file of the move.
Throws:
IOException - if the source or the destination is invalid or if an error occurs while moving the file.

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
Copies the specified source file to the specified destination. If the destination exists, it is then replaced by the source. If the destination can be overwritten, its write property is set before the copy.

Parameters:
source - the file to copy.
destination - the destination file of the move.
Throws:
IOException - if the source or the destination is invalid or if an error occurs while copying the file.

getFilename

public static String getFilename(String fileName)

convertPathToServerOS

public static String convertPathToServerOS(String undeterminedOsPath)
Convert a path to the current OS path format.

Parameters:
undeterminedOsPath -
Returns:
server OS pah.

convertFilePath

public static String convertFilePath(File file)

deleteEmptyDir

public static boolean deleteEmptyDir(File directory)

moveAllFilesAtRootFolder

public static File[] moveAllFilesAtRootFolder(File rootFolder)
                                       throws IOException
Moves all files from sub folders to the given root folder and deletes after all the sub folders.

Parameters:
rootFolder - the root folder from which the sub folders are retrieved and into which the files will be moved if any.
Returns:
an array of File that represents the found sub folders. The returned array is never null.
Throws:
IOException

moveAllFilesAtRootFolder

public static File[] moveAllFilesAtRootFolder(File rootFolder,
                                              boolean deleteFolders)
                                       throws IOException
Moves all files from sub folders to the given root folder.

Parameters:
rootFolder - the root folder from which the sub folders are retrieved and into which the files will be moved if any.
deleteFolders - true if the sub folders must be deleted.
Returns:
an array of File that represents the found sub folders. The returned array is never null.
Throws:
IOException

validateFilename

public static String validateFilename(String fileName,
                                      String intendedDir)
                               throws IOException
Validate that fileName given in parameter is inside extraction target directory (intendedDir)

Parameters:
fileName - the file name to extract
intendedDir - the extraction target directory
Returns:
the filename if fileName is inside extraction target directory
Throws:
IOException - if fileName is outside extraction target directory

isFileSecure

public static boolean isFileSecure(String fileName,
                                   String intendedDir)


Copyright © 2016 Silverpeas. All Rights Reserved.