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