EGF Engine
Release 0.1.0

org.eclipse.egf.common.helper
Class FileHelper

java.lang.Object
  extended by org.eclipse.egf.common.helper.FileHelper

public class FileHelper
extends Object


Constructor Summary
FileHelper()
           
 
Method Summary
static String convertPackageNameToFolderPath(String packageName_p)
          Convert package name to a correct java folder path.
static void copyFile(String sourceFileRelativePath_p, String targetFileRelativePath_p)
          Copy given source file content in given target file.
static boolean deleteFile(String fileRelativePath_p)
          Delete given relative resource in the workspace.
static boolean deleteFolder(String folderRelativePath_p)
          Delete given relative folder in the workspace.
static boolean deleteResource(IFolder root_p, IResource resource_p, boolean deleteParent_p)
          Delete a workspace Resource.
static boolean deleteResource(IResource resource_p)
          Delete given relative resource in the workspace.
static void ensurePathAvailability(String fileFullPath_p)
          Make sure that given path is safe to use, ie ensure that all parent folders exist.
static boolean exists(String fileRelativePath_p)
          Is given file relative path pointing to an existing file ?
static String getFileExtension(String filePath_p)
          Returns the file extension portion for given file path,
or null if there is none.
static URI getFileFullUri(String fileRelativePath_p)
          Get a file uri from relative one which is not resolved against the eclipse platform.
static URL getFileFullUrl(String fileRelativePath_p)
          Get file full url from relative one.
static URL getFileFullUrl(URI fileFullUri_p)
          Get file full url from its full uri.
static IFile getPlatformFile(String fileRelativePath_p)
          Get platform file as an IResource from its relative path.
static IFolder getPlatformFolder(String folderRelativePath_p)
          Get platform folder as an IResource from its relative path.
static IResource getPlatformResource(IPath path_p)
          Get platform IResource as an IResource from its relative IPath.
static boolean makeFileWritable(String filePath_p)
          Make file writable.
static boolean moveResource(IResource resource_p, IPath destinationPath_p)
          Move resource to given destination path.
static byte[] readFile(InputStream inputStream_p)
          Read given input stream as an array of bytes.
static String readFile(String filePath_p)
          Read file as a string.
static InputStream readFileAsStream(String filePath_p)
          Get file as a stream.
static byte[] readRawFile(String filePath_p)
          Read file as an array of bytes.
static boolean renameFile(String sourceFileRelativePath_p, String destinationFileRelativePath_p)
          Rename file from source file relative path to destination relative path.
static boolean renameFolder(String sourceFolderRelativePath_p, String destinationFolderRelativePath_p)
          Rename folder from source folder relative path to destination relative path.
static boolean writeFile(String filePath_p, boolean ensureFolders_p, byte[] contents_p)
          Write given contents of bytes at specified path.
static boolean writeFile(String filePath_p, boolean ensureFolders_p, String contents_p)
          Write given string contents at specified path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileHelper

public FileHelper()
Method Detail

getFileFullUrl

public static URL getFileFullUrl(String fileRelativePath_p)
Get file full url from relative one.

Parameters:
fileRelativePath_p - File path relative to workspace.
It must start with pluginId. It is also recommended that both plug-in id and plug-in project names are the same.
As a convenience, the full path will refer to the plug-in id.
Example : com.thalesgroup.mde.mdsofa/model/example.ecore is a path relative to the workspace that refers to the com.thalesgroup.mde.mdsofa plug-in, having a model/example.ecore file in its project.
In Eclipse resource system, such a path is considered as an absolute one against the workspace root.
It's still referred to as a relative path, since the returned URL is absolute in the file system.
Returns:

getFileFullUrl

public static URL getFileFullUrl(URI fileFullUri_p)
Get file full url from its full uri.
See getFileFullUri(String) method.

Parameters:
fileFullUri_p -
Returns:

getFileFullUri

public static URI getFileFullUri(String fileRelativePath_p)
Get a file uri from relative one which is not resolved against the eclipse platform.
The returned uri starts with either 'platform:/plug-in/' or 'platform:/resource/'.

Parameters:
fileRelativePath_p - File path relative to workspace.
It must start with pluginId. It is also recommended that both plug-in id and plug-in project names are the same.
As a convenience, the full path will refer to the plug-in id.
Example : com.thalesgroup.mde.mdsofa/model/example.ecore is a path relative to the workspace that refers to the com.thalesgroup.mde.mdsofa plug-in, having a model/example.ecore file in its project.
Returns:
an URI not resolved against the eclipse platform.

convertPackageNameToFolderPath

public static String convertPackageNameToFolderPath(String packageName_p)
Convert package name to a correct java folder path.

Parameters:
packageName_p -
Returns:

readFile

public static byte[] readFile(InputStream inputStream_p)
Read given input stream as an array of bytes.

Parameters:
inputStream_p -
Returns:
a not null array.

readFile

public static String readFile(String filePath_p)
Read file as a string.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:
If an error occurred ICommonConstants.EMPTY_STRING is returned.

readFileAsStream

public static InputStream readFileAsStream(String filePath_p)
Get file as a stream.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:
If an error occurred null is returned.

readRawFile

public static byte[] readRawFile(String filePath_p)
Read file as an array of bytes.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:
a not null array.

copyFile

public static void copyFile(String sourceFileRelativePath_p,
                            String targetFileRelativePath_p)
Copy given source file content in given target file.

Parameters:
sourceFileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
targetFileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.

writeFile

public static boolean writeFile(String filePath_p,
                                boolean ensureFolders_p,
                                String contents_p)
Write given string contents at specified path.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
ensureFolders_p - Make sure all parent folders exist by creating all necessary ones.
contents_p - Contents that should be written to pointed file.
Returns:

writeFile

public static boolean writeFile(String filePath_p,
                                boolean ensureFolders_p,
                                byte[] contents_p)
Write given contents of bytes at specified path.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
ensureFolders_p - Make sure all parent folders exist by creating all necessary ones.
contents_p - Contents that should be written to pointed file.
Returns:

renameFile

public static boolean renameFile(String sourceFileRelativePath_p,
                                 String destinationFileRelativePath_p)
Rename file from source file relative path to destination relative path.

Parameters:
sourceFileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
destinationFileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:

renameFolder

public static boolean renameFolder(String sourceFolderRelativePath_p,
                                   String destinationFolderRelativePath_p)
Rename folder from source folder relative path to destination relative path.

Parameters:
sourceFolderRelativePath_p - Folder path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
destinationFolderRelativePath_p - Folder path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:

moveResource

public static boolean moveResource(IResource resource_p,
                                   IPath destinationPath_p)
Move resource to given destination path.

Parameters:
resource_p -
destinationPath_p -
Returns:
true if move occurred with no exception, false otherwise.

exists

public static boolean exists(String fileRelativePath_p)
Is given file relative path pointing to an existing file ?

Parameters:
fileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:

ensurePathAvailability

public static void ensurePathAvailability(String fileFullPath_p)
Make sure that given path is safe to use, ie ensure that all parent folders exist.

Parameters:
fileFullPath_p -

deleteResource

public static boolean deleteResource(IFolder root_p,
                                     IResource resource_p,
                                     boolean deleteParent_p)
Delete a workspace Resource. Optionally delete its parent folder if they are empty. Root folder is never deleted.

Parameters:
javaProject_p -
resourcePath_p -

deleteFile

public static boolean deleteFile(String fileRelativePath_p)
Delete given relative resource in the workspace.

Parameters:
fileRelativePath_p -

deleteFolder

public static boolean deleteFolder(String folderRelativePath_p)
Delete given relative folder in the workspace.

Parameters:
workspaceRelativePath_p -
Returns:
true if successfully deleted, false otherwise.

deleteResource

public static boolean deleteResource(IResource resource_p)
Delete given relative resource in the workspace.

Parameters:
resource_p -

getPlatformResource

public static IResource getPlatformResource(IPath path_p)
Get platform IResource as an IResource from its relative IPath.

Parameters:
path_p - IPath relative to workspace.
Returns:

getPlatformFile

public static IFile getPlatformFile(String fileRelativePath_p)
Get platform file as an IResource from its relative path.

Parameters:
fileRelativePath_p - File path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:

getPlatformFolder

public static IFolder getPlatformFolder(String folderRelativePath_p)
Get platform folder as an IResource from its relative path.

Parameters:
folderRelativePath_p - Folder path relative to the plug-in, plug-in id included.
See getFileFullUrl(String) documentation.
Returns:

getFileExtension

public static String getFileExtension(String filePath_p)
Returns the file extension portion for given file path,
or null if there is none.

The file extension portion is defined as the string
following the last period (".") character in the last segment.
If there is no period in the last segment, the path has no
file extension portion. If the last segment ends in a period,
the file extension portion is the empty string.

Parameters:
filePath_p -
Returns:
the file extension or null

makeFileWritable

public static boolean makeFileWritable(String filePath_p)
Make file writable.
That is, make sure file is modifiable after this call.
The user may be asked to take a decision if the file is held by the configuration management system.
Nevertheless, if no UI is reachable, then the system is urged into making the file writable.

Parameters:
filePath_p - File path relative to the plug-in, plug-in id included. See getFileFullUrl(String) documentation.
Returns:
false if file could not be made writable or user denied rights to (in case of a configuration management). true if it does not exist (then it is writable) or permission was granted (either by the system or by the user).

EGF Engine
Release 0.1.0

Copyright (c) Thales Corporate Services S.A.S, 2009.

This page is made available under license. For full details see the LEGAL in the documentation book that contains this page.