56
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 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 |
* Project file utilities
|
|
17 |
*
|
|
18 |
*/
|
|
19 |
package com.nokia.tracecompiler.project;
|
|
20 |
|
|
21 |
import java.io.File;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
|
|
24 |
import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
|
|
25 |
import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
|
|
26 |
import com.nokia.tracecompiler.engine.project.ProjectEngine;
|
|
27 |
import com.nokia.tracecompiler.model.TraceCompilerException;
|
|
28 |
import com.nokia.tracecompiler.model.TraceModel;
|
|
29 |
import com.nokia.tracecompiler.source.SymbianConstants;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Project file utilities
|
|
33 |
*
|
|
34 |
*/
|
|
35 |
public final class ProjectUtils {
|
|
36 |
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Tries to locate existing file with given extension and if not found,
|
|
40 |
* proposes a new location for that file. The new location will be
|
|
41 |
* <i>createPath</i> or if that is null, the directory where the trace
|
|
42 |
* project file (.tbprj) resides. Trace project must be open before this can
|
|
43 |
* be called
|
|
44 |
*
|
|
45 |
* @param model
|
|
46 |
* the trace model
|
|
47 |
* @param createPath
|
|
48 |
* the path to create if existing file is not found
|
|
49 |
* @param fileName
|
|
50 |
* the name for the new file
|
|
51 |
* @param appendProjectName
|
|
52 |
* true if the name is appended to end of model name
|
|
53 |
* @return the project file path for the new file
|
|
54 |
* @throws TraceCompilerException
|
|
55 |
* if the model does not have any project files
|
|
56 |
*/
|
|
57 |
public static String getLocationForFile(TraceModel model,
|
|
58 |
String createPath, String fileName, boolean appendProjectName)
|
|
59 |
throws TraceCompilerException {
|
|
60 |
TraceProjectFile projectFile = model
|
|
61 |
.getExtension(TraceProjectFile.class);
|
|
62 |
if (projectFile == null) {
|
|
63 |
throw new TraceCompilerException(
|
|
64 |
TraceCompilerErrorCode.MODEL_NOT_READY);
|
|
65 |
}
|
|
66 |
String projectName = ""; //$NON-NLS-1$
|
|
67 |
if (appendProjectName) {
|
|
68 |
projectName = projectFile.getProjectName();
|
|
69 |
}
|
|
70 |
return getLocationForFile(projectFile.getPath(), projectName,
|
|
71 |
createPath, fileName);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Tries to locate existing file with given extension and if not found,
|
|
76 |
* proposes a new location for that file. The new location will be
|
|
77 |
* <i>createPath</i> or if that is null, the directory where the trace
|
|
78 |
* project file resides. Trace project must be open before this can
|
|
79 |
* be called
|
|
80 |
*
|
|
81 |
* @param projectPath
|
|
82 |
* path to the project
|
|
83 |
* @param projectName
|
|
84 |
* name of the project
|
|
85 |
* @param createPath
|
|
86 |
* the path to create if existing file is not found
|
|
87 |
* @param extension
|
|
88 |
* the file extension for the new file
|
|
89 |
* @return the project file path for the new file
|
|
90 |
*/
|
|
91 |
public static String getLocationForFile(String projectPath,
|
|
92 |
String projectName, String createPath, String extension) {
|
|
93 |
ArrayList<String> searchPaths = new ArrayList<String>();
|
|
94 |
if (createPath != null && createPath.length() > 0) {
|
|
95 |
// If create path is explicitly specified, it is added to the search
|
|
96 |
// path list
|
|
97 |
searchPaths.add(createPath);
|
|
98 |
}
|
|
99 |
// Default directories are always included into search
|
|
100 |
searchPaths.add(ProjectEngine.traceFolderName);
|
|
101 |
searchPaths.add(SymbianConstants.GROUP_DIRECTORY);
|
|
102 |
searchPaths.add(SymbianConstants.INCLUDE_DIRECTORY);
|
|
103 |
String[] pathArr = new String[searchPaths.size()];
|
|
104 |
searchPaths.toArray(pathArr);
|
|
105 |
return getLocationForFile(projectPath, projectName, pathArr,
|
|
106 |
createPath, extension);
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Tries to locate existing file and if not found, proposes a new location
|
|
111 |
* for that file. The search starts from <i>projectPath</i>. If
|
|
112 |
* <i>checkParents</i> is true, the parent directories are also checked if
|
|
113 |
* the file is not found
|
|
114 |
*
|
|
115 |
* @param projectPath
|
|
116 |
* path where to start the search
|
|
117 |
* @param projectName
|
|
118 |
* the name of the project
|
|
119 |
* @param searchPaths
|
|
120 |
* the sub-paths to be searched
|
|
121 |
* @param createPath
|
|
122 |
* the sub-path to create if file is not found
|
|
123 |
* @param fileName
|
|
124 |
* extension of the file to be located
|
|
125 |
* @return the project file path for the new file
|
|
126 |
*/
|
|
127 |
private static String getLocationForFile(String projectPath,
|
|
128 |
String projectName, String[] searchPaths, String createPath,
|
|
129 |
String fileName) {
|
|
130 |
// If the project file is in one of the search paths, the path is
|
|
131 |
// changed to its parent
|
|
132 |
File projectPathFile = new File(projectPath);
|
|
133 |
String projectPathName = projectPathFile.getName();
|
|
134 |
for (int i = 0; i < searchPaths.length; i++) {
|
|
135 |
if (searchPaths[i].equals(projectPathName)) {
|
|
136 |
projectPath = projectPathFile.getParent();
|
|
137 |
i = searchPaths.length;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
// Tries to find an existing file based on project path and project name
|
|
141 |
String filePath = findProjectFile(projectPath, projectName,
|
|
142 |
searchPaths, fileName, false);
|
|
143 |
if (filePath == null) {
|
|
144 |
// If file is not found, this returns a new path
|
|
145 |
filePath = projectPath + File.separator + createPath
|
|
146 |
+ File.separator + projectName + fileName;
|
|
147 |
}
|
|
148 |
return filePath;
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Finds a project file or directory based on a file or directory
|
|
153 |
*
|
|
154 |
* @param startPath
|
|
155 |
* the file name or path where to start search
|
|
156 |
* @param projectName
|
|
157 |
* the name of the project file
|
|
158 |
* @param searchDirectories
|
|
159 |
* the directories to search
|
|
160 |
* @param fileExtension
|
|
161 |
* the file extension to be located or null if locating one of
|
|
162 |
* the search directories
|
|
163 |
* @param checkParents
|
|
164 |
* true if parent directories should be checked
|
|
165 |
* @return the file or directory path if found or null if not
|
|
166 |
*/
|
|
167 |
private static String findProjectFile(String startPath, String projectName,
|
|
168 |
String[] searchDirectories, String fileExtension,
|
|
169 |
boolean checkParents) {
|
|
170 |
String foundFile = null;
|
|
171 |
if (startPath != null) {
|
|
172 |
File file = new File(startPath);
|
|
173 |
if (!file.isDirectory()) {
|
|
174 |
file = file.getParentFile();
|
|
175 |
}
|
|
176 |
if (file != null) {
|
|
177 |
do {
|
|
178 |
for (int i = 0; i < searchDirectories.length
|
|
179 |
&& foundFile == null; i++) {
|
|
180 |
foundFile = findProjectFile(searchDirectories[i],
|
|
181 |
projectName, fileExtension, file);
|
|
182 |
}
|
|
183 |
file = file.getParentFile();
|
|
184 |
} while (file != null && foundFile == null && checkParents);
|
|
185 |
}
|
|
186 |
}
|
|
187 |
return foundFile;
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Searches a single directory for a project file
|
|
192 |
*
|
|
193 |
* @param searchDirectory
|
|
194 |
* the directory to search
|
|
195 |
* @param fileExtension
|
|
196 |
* the file extension to be located or null if locating one of
|
|
197 |
* the search directories
|
|
198 |
* @param projectName
|
|
199 |
* the name of the project file
|
|
200 |
* @param file
|
|
201 |
* the current file
|
|
202 |
* @return the file or directory path if found or null if not
|
|
203 |
*/
|
|
204 |
private static String findProjectFile(String searchDirectory,
|
|
205 |
String projectName, String fileExtension, File file) {
|
|
206 |
String foundFile = null;
|
|
207 |
File projectPath = new File(file.getAbsolutePath() + File.separator
|
|
208 |
+ searchDirectory + File.separator);
|
|
209 |
if (projectPath.exists()) {
|
|
210 |
if (fileExtension == null) {
|
|
211 |
foundFile = projectPath.getAbsolutePath();
|
|
212 |
} else {
|
|
213 |
foundFile = findProjectFileFromDirectory(projectPath,
|
|
214 |
projectName, fileExtension);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
return foundFile;
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Gets the first file with given extension from given directory
|
|
222 |
*
|
|
223 |
* @param path
|
|
224 |
* the path to search
|
|
225 |
* @param projectName
|
|
226 |
* the name of the project file
|
|
227 |
* @param fileExtension
|
|
228 |
* the file extension
|
|
229 |
* @return the file name if found or null if not
|
|
230 |
*/
|
|
231 |
private static String findProjectFileFromDirectory(File path,
|
|
232 |
String projectName, String fileExtension) {
|
|
233 |
String proposal = null;
|
|
234 |
File[] files = path.listFiles();
|
|
235 |
if (projectName != null) {
|
|
236 |
// If project name is specified, only the file that matches the name
|
|
237 |
// is returned
|
|
238 |
String nameFromProjectFile = projectName + fileExtension;
|
|
239 |
|
|
240 |
// Check does user have access rights to the traces folder
|
|
241 |
if (path.canWrite() == false) {
|
|
242 |
TraceCompilerEngineGlobals
|
|
243 |
.getEvents()
|
|
244 |
.postErrorMessage(
|
|
245 |
Messages
|
|
246 |
.getString("ProjectUtils.accesRightErrorText"), null, true); //$NON-NLS-1$
|
|
247 |
System.exit(1);
|
|
248 |
}
|
|
249 |
|
|
250 |
for (int i = 0; i < files.length && proposal == null; i++) {
|
|
251 |
String fname = files[i].getName();
|
|
252 |
if (fname.equals(nameFromProjectFile)) {
|
|
253 |
proposal = files[i].getAbsolutePath();
|
|
254 |
}
|
|
255 |
}
|
|
256 |
} else {
|
|
257 |
// If project name is not specified, this proposes the first file
|
|
258 |
// which ends with the file extension
|
|
259 |
for (int i = 0; i < files.length && proposal == null; i++) {
|
|
260 |
String fname = files[i].getName();
|
|
261 |
if (fname.length() > fileExtension.length()) {
|
|
262 |
int extstart = fname.length() - fileExtension.length();
|
|
263 |
if (fname.substring(extstart).equals(fileExtension)) {
|
|
264 |
proposal = files[i].getAbsolutePath();
|
|
265 |
}
|
|
266 |
}
|
|
267 |
}
|
|
268 |
}
|
|
269 |
return proposal;
|
|
270 |
}
|
|
271 |
}
|