frameworkplugins/com.nokia.s60tools.util/src/com/nokia/s60tools/util/resource/FileUtils.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
child 8 df27719e5a0d
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
package com.nokia.s60tools.util.resource;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import java.io.BufferedInputStream;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
import java.io.BufferedWriter;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import java.io.File;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import java.io.FileInputStream;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
import java.io.FileNotFoundException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import java.io.FileOutputStream;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import java.io.FileWriter;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
import java.io.IOException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
import java.io.OutputStreamWriter;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
import org.eclipse.core.resources.IFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
import org.eclipse.core.resources.IProject;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
import org.eclipse.core.resources.IResource;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
import org.eclipse.core.resources.IWorkspace;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
import org.eclipse.core.resources.ResourcesPlugin;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
import org.eclipse.core.runtime.CoreException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
import org.eclipse.core.runtime.IPath;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
import org.eclipse.core.runtime.Path;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
import org.eclipse.swt.SWT;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
import org.eclipse.swt.widgets.FileDialog;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
import org.eclipse.swt.widgets.Shell;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
import org.eclipse.ui.IEditorDescriptor;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
import org.eclipse.ui.IWorkbench;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
import org.eclipse.ui.IWorkbenchPage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
import org.eclipse.ui.IWorkbenchWindow;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
import org.eclipse.ui.PartInitException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
import org.eclipse.ui.PlatformUI;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
import org.eclipse.ui.part.FileEditorInput;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
import com.nokia.s60tools.util.internal.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
 * Miscellaneous utility methods related to files.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
public class FileUtils {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
	 * Constant for UTF-8 encoding type.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
	public static final String ENCODING_TYPE_UTF_8 = "UTF-8"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	 * Constant for UTF-8 encoding type.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	public static final String ENCODING_TYPE_UTF_16 = "UTF-16"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
	 * Queries user for an external file to be opened, and opens it.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	 * @param projectName Name of the project to open file into.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
	 * @throws CoreException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	 * @throws PartInitException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
	 * @throws FileNotFoundException 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
	public static void openExternalFileDialog(String projectName) throws CoreException, PartInitException, FileNotFoundException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
	   IWorkbench workbench = PlatformUI.getWorkbench();			   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
	   IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
	   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
	   Shell shell = window.getShell();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	   String fileAbsolutePathName = new FileDialog(shell, SWT.OPEN).open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
	   if (fileAbsolutePathName == null)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
	      return;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
	   openExternalFileImpl(fileAbsolutePathName, workbench, projectName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
	 * Opens the external file given as parameter.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	 * @param fileAbsolutePathName Absolute path name to the file to be opened.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
	 * @param projectName Name of the project to open file into.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	 * @throws CoreException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
	 * @throws PartInitException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
	 * @throws FileNotFoundException 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
	public static void openExternalFile(String fileAbsolutePathName, String projectName) throws CoreException, PartInitException, FileNotFoundException {		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
	   IWorkbench workbench = PlatformUI.getWorkbench();			   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
	   openExternalFileImpl(fileAbsolutePathName, workbench, projectName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	 * Opens the external file given as parameter.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
	 * @param fileAbsolutePathName Absolute path name to the file to be opened.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
	 * @param workbench Workbench to be used.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
	 * @param projectName Name of the project to open file into.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
	 * @throws CoreException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
	 * @throws PartInitException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
	 * @throws FileNotFoundException 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
	private static void openExternalFileImpl(String fileAbsolutePathName, IWorkbench workbench, String projectName) throws CoreException, PartInitException, FileNotFoundException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
		File f = new File(fileAbsolutePathName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
		if(!f.exists()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
			throw new FileNotFoundException(Messages.getString("FileUtils.NonExistingExternalFile_ErrMsg") + fileAbsolutePathName); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
	   IPath location = new Path(fileAbsolutePathName);		   		   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
	   IWorkspace ws = ResourcesPlugin.getWorkspace();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
	   IProject project = ws.getRoot().getProject(projectName); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
		   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
	   if (!project.exists())
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
	      project.create(null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
	   if (!project.isOpen())
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
	      project.open(null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
	   IFile file = project.getFile(location.lastSegment());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
	   file.createLink(location, IResource.REPLACE, null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
	   final FileEditorInput editorInput = new FileEditorInput(file);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
	   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
	   IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
	   IWorkbenchPage page = window.getActivePage();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
	   if (page != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
		   IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(file.getName());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
		   page.openEditor(editorInput, desc.getId());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
	   }
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
	 * Loads the contents of the given file into memory. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
	 * @param filePathName Absolute path name for the file.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
	 * @return StringBuffer instance having the contents of the file.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
	 * @throws FileNotFoundException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
	 * @throws IOException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
	public static StringBuffer loadDataFromFile(String filePathName) throws FileNotFoundException, IOException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
		StringBuffer dataBuffer = new StringBuffer();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
		File f = new File(filePathName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
		FileInputStream fis = new FileInputStream(f);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
		BufferedInputStream br = new BufferedInputStream(fis);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
		int available = fis.available();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
		for(int readLoopCount=1; available > 0;readLoopCount++){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
			byte[] tmpByteArr = new byte[available];
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
			int readBytes = br.read(tmpByteArr, 0, available);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
			if(readBytes == -1){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
				break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
			else if(readBytes != available){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
				br.close();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   155
				fis.close();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   156
				throw new IOException(Messages.getString("FileUtils.FAILED_TO_LOAD_DATA")  //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   157
						              + filePathName 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
						              + Messages.getString("FileUtils.LESS_BYTES_THAN_EXPECTED")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
			dataBuffer.append(new String(tmpByteArr, 0, readBytes));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
			available = fis.available();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   162
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   163
		br.close();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   164
		fis.close();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   165
		return dataBuffer;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   166
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   167
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   168
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   169
	 * Save text to file. If the folder where file is located does not exist, creating one.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   170
	 * @param filename file name with full path
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   171
	 * @param file file contents
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   172
	 * @throws IOException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   173
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   174
	public static void writeToFile( String fileName, String fileContents ) throws IOException {		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   175
		writeToFile(fileName, fileContents, null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   176
	}		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   177
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   178
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   179
	 * Save text to file. If the folder where file is located does not exist, creating one.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   180
	 * Sets file encoding type.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   181
	 * @param filename file name with full path
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   182
	 * @param file file contents
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   183
	 * @param encoding encoding type e.g. {@link FileUtils.ENCODING_TYPE_UTF_8}  
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   184
	 * @throws IOException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   185
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   186
	public static void writeToFile( String fileName, String fileContents, String encoding ) throws IOException {		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   187
		BufferedWriter out = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   188
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   189
						
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   190
			File file = new File(fileName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   191
			File parent = file.getParentFile();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   192
			if(!parent.exists()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   193
				parent.mkdirs();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   194
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   195
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   196
			//If encoding is set
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   197
			if(encoding != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   198
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), encoding));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   199
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   200
			else{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   201
				out = new BufferedWriter(new FileWriter(fileName));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   202
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   203
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   204
			out.write(fileContents);			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   205
		} catch (IOException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   206
			e.printStackTrace();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   207
			throw e;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   208
		}finally{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   209
			if(out != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   210
				out.close();				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   211
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   212
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   213
	}	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   214
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   215
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   216
	 * Gets logical drive for the given file object pointing to a file or path name.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   217
	 * @param absolutePathNameFileObject File object containing a directory or a file path name..
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   218
	 * @return logical drive for the file or path name including colon and file separator (e.g. C:\)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   219
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   220
	public static String getLogicalDrive(File absolutePathNameFileObject) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   221
		File parentFile = absolutePathNameFileObject;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   222
		while(parentFile.getParentFile() != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   223
			parentFile = parentFile.getParentFile();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   224
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   225
		return parentFile.getAbsolutePath();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   226
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   227
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   228
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   229
	 * Gets logical drive for the given file or path name string. The given path should be absolute.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   230
	 * If given path is relative (i.e. does not contain logical drive in path name), a new file
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   231
	 * object is created which adds current directory with current logical drive before the relative path.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   232
	 * @param absolutePathName Absolute path name string to a directory a file containing logical drive
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   233
	 * @return logical drive for the file or path name including colon and file separator (e.g. C:\)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   234
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   235
	public static String getLogicalDrive(String absolutePathName) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   236
		return getLogicalDrive(new File(absolutePathName));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   237
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   238
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   239
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   240
	 * Checks if the given path starts with drive letter sequence e.g. with C:\ and contains
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   241
	 * at least a single character after the drive letter start sequence.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   242
	 * Check is done with the following regular expression <code>[a-zA-Z]:[\\/].+</code> that 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   243
	 * e.g. matches the following paths
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   244
	 * - A:\my\example\path1, c:\my\example\path1, Z:/my\example\path3, and r:/my/example/path1
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   245
	 * but does not match the following ones:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   246
	 * - :\my\example\path2, \my\example\path, z:\, R:/ 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   247
	 * If regular expression matches the first three letters are removed from the path.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   248
	 * @param pathName Path name to check for drive letter removal. Path can be absolute
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   249
	 *                 or relative, and does not have point to valid file or directory. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   250
	 * @return Source path without drive letter, if it existed previously.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   251
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   252
	public static String removeDriveLetterPortionFromPathIfExists(String pathName) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   253
		String pathWithDriveLetterRegExp = "[a-zA-Z]:[\\\\/].+"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   254
		if(pathName.matches(pathWithDriveLetterRegExp)){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   255
			// In case of match skipping first three letters
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   256
			return pathName.substring(3); 		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   257
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   258
		return pathName;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   259
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   260
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   261
  	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   262
	 * Replaces all characters that cannot be used in Win32 directory and file names.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   263
	 * @param stringToBeChecked string to be checked for illegal characters.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   264
	 * @param replaceChar replacement character for illegal filename characters found
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   265
	 * @return string from which all illegal filename characters are replaced
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   266
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   267
	public static String removeIllegalWin32FilenameCharactersFromString(String stringToBeChecked, char replaceChar) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   268
		// Set of characters that cannot be part of a file or directory name (list was got from win32 error message) 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   269
		final char illegalFilenameCharsArr[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   270
		// Checking that replacement character itself is not illegal character
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   271
		if(new String(illegalFilenameCharsArr).indexOf(replaceChar) != -1){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   272
			throw new IllegalArgumentException("Tried to replace illegal Win32 filename character with also illegal character" + ": " + replaceChar);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   273
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   274
		String resultString = new String(stringToBeChecked);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   275
		if(resultString != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   276
			for (int i = 0; i < illegalFilenameCharsArr.length; i++) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   277
				char ch = illegalFilenameCharsArr[i];				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   278
				resultString = resultString.replace(ch, replaceChar);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   279
			}		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   280
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   281
		return resultString;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   282
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   283
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   284
}