|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 package com.symbian.smt.gui; |
|
17 |
|
18 import static com.symbian.smt.gui.ResourcesEnums.BORDER_SHAPES; |
|
19 import static com.symbian.smt.gui.ResourcesEnums.BORDER_STYLES; |
|
20 import static com.symbian.smt.gui.ResourcesEnums.COLOURS; |
|
21 import static com.symbian.smt.gui.ResourcesEnums.DEPENDENCIES; |
|
22 import static com.symbian.smt.gui.ResourcesEnums.LEVELS; |
|
23 import static com.symbian.smt.gui.ResourcesEnums.LOCALISATION; |
|
24 import static com.symbian.smt.gui.ResourcesEnums.PATTERNS; |
|
25 import static com.symbian.smt.gui.ResourcesEnums.S12_XML; |
|
26 import static com.symbian.smt.gui.ResourcesEnums.SHAPES; |
|
27 import static com.symbian.smt.gui.ResourcesEnums.SYSTEM_INFO; |
|
28 |
|
29 import java.io.File; |
|
30 import java.io.IOException; |
|
31 import java.util.HashMap; |
|
32 import java.util.List; |
|
33 import java.util.regex.Pattern; |
|
34 |
|
35 import org.eclipse.core.resources.IContainer; |
|
36 import org.eclipse.core.resources.IFile; |
|
37 import org.eclipse.core.resources.IFolder; |
|
38 import org.eclipse.core.resources.IMarker; |
|
39 import org.eclipse.core.resources.IProject; |
|
40 import org.eclipse.core.resources.IResource; |
|
41 import org.eclipse.core.resources.ProjectScope; |
|
42 import org.eclipse.core.runtime.CoreException; |
|
43 import org.eclipse.core.runtime.IPath; |
|
44 import org.eclipse.core.runtime.Path; |
|
45 import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
|
46 import org.eclipse.core.runtime.preferences.IScopeContext; |
|
47 |
|
48 public class ManageResources { |
|
49 |
|
50 private static final List<String> RESOURCE_FOLDER_NAMES = Helper |
|
51 .toListOfStrings(ResourcesEnums.values()); |
|
52 |
|
53 public static final String IS_URL = "isUrl"; |
|
54 public static final String URL_STRING = "urlString"; |
|
55 |
|
56 /** |
|
57 * Deletes a resource, if it exists within a specified folder within the |
|
58 * project. |
|
59 * |
|
60 * @param project |
|
61 * The project to amend |
|
62 * @param folderName |
|
63 * The folder in which the resource is to be updated |
|
64 * @return void |
|
65 */ |
|
66 private static void deleteResources(IProject project, String folderName) { |
|
67 // Create/Get the folder in the project |
|
68 IFolder folder = getFolderInProject(project, folderName); |
|
69 |
|
70 try { |
|
71 for (IResource res : folder.members()) { |
|
72 res.delete(true, null); |
|
73 } |
|
74 } catch (CoreException e) { |
|
75 Logger.log(e.getMessage(), e); |
|
76 } |
|
77 } |
|
78 |
|
79 /** |
|
80 * Returns an IFolder object for the specified folder and project. Creates |
|
81 * the folder if it does not already exist. |
|
82 * |
|
83 * @param project |
|
84 * The project to amend |
|
85 * @param folderName |
|
86 * The folder name required |
|
87 * @return IFolder |
|
88 */ |
|
89 private static IFolder getFolderInProject(IProject project, |
|
90 String folderName) { |
|
91 // Makes a folder in the project |
|
92 final IFolder folder = project.getFolder(new Path(folderName)); |
|
93 |
|
94 // Check to see if the folder already exists before creating it |
|
95 if (!folder.exists()) { |
|
96 try { |
|
97 folder.create(true, true, null); |
|
98 } catch (CoreException e) { |
|
99 Logger.log(e.getMessage(), e); |
|
100 } |
|
101 } |
|
102 return folder; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Creates and returns an AbstractPersistentDataStore object, used for |
|
107 * persising data. |
|
108 * |
|
109 * @param project |
|
110 * The project to amend |
|
111 * @return AbstractPersistentDataStore |
|
112 */ |
|
113 private static AbstractPersistentDataStore getPersistDataStore( |
|
114 IProject project) { |
|
115 IScopeContext projectScope = new ProjectScope(project); |
|
116 IEclipsePreferences node = projectScope.getNode(Activator.PLUGIN_ID); |
|
117 AbstractPersistentDataStore dataStore = new PersistentDataStore(node); |
|
118 |
|
119 return dataStore; |
|
120 } |
|
121 |
|
122 public static ResourcesEnums getResourceType(IFile file) { |
|
123 if (isResourceFile(file)) { |
|
124 String folderName = ((IFolder) file.getParent()).getName(); |
|
125 |
|
126 return ResourcesEnums.getResourcesEnums(folderName); |
|
127 } |
|
128 |
|
129 return null; |
|
130 } |
|
131 |
|
132 public static String getResourceUrl(IFile file) throws CoreException { |
|
133 String urlString = null; |
|
134 IMarker[] messageMarkers = file.findMarkers(IMarker.TASK, false, |
|
135 IResource.DEPTH_ZERO); |
|
136 |
|
137 for (int i = 0; i < messageMarkers.length; i++) { |
|
138 IMarker marker = messageMarkers[i]; |
|
139 |
|
140 if (marker.getAttribute(ManageResources.IS_URL, false)) { |
|
141 urlString = (String) marker.getAttribute(URL_STRING); |
|
142 } |
|
143 } |
|
144 |
|
145 return urlString; |
|
146 } |
|
147 |
|
148 public static boolean isLocalPath(String path) { |
|
149 int index = path.indexOf(':'); |
|
150 |
|
151 if (index == -1 || index == 1) { |
|
152 return true; |
|
153 } else if (index > 1) { |
|
154 return false; |
|
155 } else { |
|
156 throw new RuntimeException("Unexpected file path format."); |
|
157 } |
|
158 } |
|
159 |
|
160 public static boolean isResourceFile(IFile file) { |
|
161 IContainer container = file.getParent(); |
|
162 |
|
163 if (container instanceof IFolder) { |
|
164 IFolder folder = (IFolder) container; |
|
165 String folderName = folder.getName(); |
|
166 |
|
167 if (RESOURCE_FOLDER_NAMES.contains(folderName)) { |
|
168 return true; |
|
169 } |
|
170 } |
|
171 |
|
172 return false; |
|
173 } |
|
174 |
|
175 public static boolean isSystemDefinitionFile(IFile file) { |
|
176 IContainer container = file.getParent(); |
|
177 |
|
178 if (container instanceof IProject) { |
|
179 return true; |
|
180 } |
|
181 |
|
182 return false; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Adds a file shortcut to the specified project |
|
187 * |
|
188 * @param project |
|
189 * The project to amend |
|
190 * @param folder |
|
191 * The folder to create the shortcut in |
|
192 * @param filenameInOS |
|
193 * The file in the OS to create the shortcut to |
|
194 * @param filenameInProject |
|
195 * The name to use for the shortcut |
|
196 * @return void |
|
197 */ |
|
198 private static void makeFileShortcut(IProject project, String folder, |
|
199 String filenameInOS, String fileNameInProject) { |
|
200 IFile file; |
|
201 |
|
202 // Check to see if the file needs to go into a folder and create the |
|
203 // IFile object |
|
204 if (folder == null || folder.length() == 0) { |
|
205 file = project.getFile(fileNameInProject); |
|
206 } else { |
|
207 IFolder folder2 = project.getFolder(folder); |
|
208 file = folder2.getFile(fileNameInProject); |
|
209 } |
|
210 |
|
211 // If the file is not being linked to a file in the OS then create a new |
|
212 // empty file |
|
213 if (filenameInOS == null || filenameInOS.length() == 0) { |
|
214 filenameInOS = file.getRawLocation().toString(); |
|
215 |
|
216 File newEmptyFile = new File(filenameInOS); |
|
217 |
|
218 try { |
|
219 newEmptyFile.createNewFile(); |
|
220 } catch (IOException e) { |
|
221 Logger.log(e.getMessage(), e); |
|
222 } |
|
223 } |
|
224 |
|
225 // Create a link to the file |
|
226 try { |
|
227 if (isLocalPath(filenameInOS)) { |
|
228 IPath path = new Path(filenameInOS); |
|
229 file.createLink(path, IResource.ALLOW_MISSING_LOCAL, null); |
|
230 } else { |
|
231 file.create(null, false, null); |
|
232 IMarker marker = file.createMarker(IMarker.TASK); |
|
233 marker.setAttribute(IS_URL, true); |
|
234 marker.setAttribute(URL_STRING, filenameInOS); |
|
235 } |
|
236 |
|
237 FileValidationHelper.validateSysDefFile(file); |
|
238 } catch (CoreException e) { |
|
239 Logger.log(e.getMessage(), e); |
|
240 } |
|
241 } |
|
242 |
|
243 /** |
|
244 * Updates the border shapes file resources within the project. |
|
245 * |
|
246 * @param project |
|
247 * The project to amend |
|
248 * @param options |
|
249 * An array of filenames or the empty array if there are no files |
|
250 * @return void |
|
251 */ |
|
252 public static void updateBorderShapesFiles(IProject project, |
|
253 String[] options) { |
|
254 updateResources(project, options, BORDER_SHAPES); |
|
255 |
|
256 // persist the data |
|
257 getPersistDataStore(project).setSelectedBorderShapesFiles(options); |
|
258 } |
|
259 |
|
260 /** |
|
261 * Updates the border styles file resources within the project. |
|
262 * |
|
263 * @param project |
|
264 * The project to amend |
|
265 * @param options |
|
266 * An array of filenames or the empty array if there are no files |
|
267 * @return void |
|
268 */ |
|
269 public static void updateBorderStylesFiles(IProject project, |
|
270 String[] options) { |
|
271 updateResources(project, options, BORDER_STYLES); |
|
272 |
|
273 // persist the data |
|
274 getPersistDataStore(project).setSelectedBorderStylesFiles(options); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Updates the colours file resources within the project. |
|
279 * |
|
280 * @param project |
|
281 * The project to amend |
|
282 * @param options |
|
283 * An array of filenames or the empty array if there are no files |
|
284 * @return void |
|
285 */ |
|
286 public static void updateColoursFiles(IProject project, String[] options) { |
|
287 updateResources(project, options, COLOURS); |
|
288 |
|
289 // persist the data |
|
290 getPersistDataStore(project).setSelectedColoursFiles(options); |
|
291 } |
|
292 |
|
293 /** |
|
294 * Updates the dependencies file resources within the project. |
|
295 * |
|
296 * @param project |
|
297 * The project to amend |
|
298 * @param options |
|
299 * An array of filenames or the empty array if there are no files |
|
300 * @return void |
|
301 */ |
|
302 public static void updateDependenciesFiles(IProject project, |
|
303 String[] options) { |
|
304 updateResources(project, options, DEPENDENCIES); |
|
305 |
|
306 // persist the data |
|
307 getPersistDataStore(project).setSelectedDependenciesFiles(options); |
|
308 } |
|
309 |
|
310 /** |
|
311 * Updates the levels file resources within the project. |
|
312 * |
|
313 * @param project |
|
314 * The project to amend |
|
315 * @param options |
|
316 * An array of filenames or the empty array if there are no files |
|
317 * @return void |
|
318 */ |
|
319 public static void updateLevelsFiles(IProject project, String[] options) { |
|
320 updateResources(project, options, LEVELS); |
|
321 |
|
322 // persist the data |
|
323 getPersistDataStore(project).setSelectedLevelsFiles(options); |
|
324 } |
|
325 |
|
326 /** |
|
327 * Updates the localisation file resources within the project. |
|
328 * |
|
329 * @param project |
|
330 * The project to amend |
|
331 * @param options |
|
332 * An array of filenames or the empty array if there are no files |
|
333 * @return void |
|
334 */ |
|
335 public static void updateLocalisationFiles(IProject project, |
|
336 String[] options) { |
|
337 updateResources(project, options, LOCALISATION); |
|
338 |
|
339 // persist the data |
|
340 getPersistDataStore(project).setSelectedLocalisationFiles(options); |
|
341 } |
|
342 |
|
343 /** |
|
344 * Updates the patterns file resources within the project. |
|
345 * |
|
346 * @param project |
|
347 * The project to amend |
|
348 * @param options |
|
349 * An array of filenames or the empty array if there are no files |
|
350 * @return void |
|
351 */ |
|
352 public static void updatePatternsFiles(IProject project, String[] options) { |
|
353 updateResources(project, options, PATTERNS); |
|
354 |
|
355 // persist the data |
|
356 getPersistDataStore(project).setSelectedPatternsFiles(options); |
|
357 } |
|
358 |
|
359 /** |
|
360 * Updates a resource within a specified folder within the project. |
|
361 * |
|
362 * @param project |
|
363 * The project to amend |
|
364 * @param folderName |
|
365 * The folder in which the resource is to be updated |
|
366 * @param option |
|
367 * Either a filename or AUTO_LEVEL |
|
368 * @return void |
|
369 */ |
|
370 private static void updateResource(IProject project, String folderName, |
|
371 String option) { |
|
372 // The other arguments are checked by the callers |
|
373 if (folderName == null) { |
|
374 throw new IllegalArgumentException("Arguments cannot be null."); |
|
375 } |
|
376 |
|
377 // Create/Get the folder in the project |
|
378 IFolder folder = getFolderInProject(project, folderName); |
|
379 |
|
380 // If we were passed the empty string as the file name, just return, |
|
381 // otherwise |
|
382 // create the file |
|
383 if (option.length() > 0) { |
|
384 String newName; |
|
385 |
|
386 if (isLocalPath(option)) { |
|
387 String[] filenameParts = option.split("[\\\\/]"); |
|
388 newName = filenameParts[filenameParts.length - 1]; |
|
389 } else { // If option is a URL |
|
390 int beginIndex = option.lastIndexOf("/"); |
|
391 newName = option.substring(beginIndex + 1, option.length()); |
|
392 } |
|
393 |
|
394 if (!option.equals("Auto")) { |
|
395 // Create a link to the file |
|
396 try { |
|
397 IFile file = folder.getFile(newName); |
|
398 |
|
399 if (isLocalPath(option)) { |
|
400 IPath path = new Path(option); |
|
401 file.createLink(path, IResource.ALLOW_MISSING_LOCAL, null); |
|
402 } |
|
403 else { |
|
404 file.create(null, false, null); |
|
405 IMarker marker = file.createMarker(IMarker.TASK); |
|
406 marker.setAttribute(IS_URL, true); |
|
407 marker.setAttribute(URL_STRING, option); |
|
408 } |
|
409 |
|
410 // TODO:BRS:Remove if test when Shapes.xsd is available |
|
411 if (!folderName.equals("Shapes")) { |
|
412 FileValidationHelper.validateResourceFile(file); |
|
413 } |
|
414 } catch (CoreException e) { |
|
415 Logger.log(e.getMessage(), e); |
|
416 } |
|
417 } |
|
418 } |
|
419 } |
|
420 |
|
421 private static void updateResources(IProject project, String[] options, |
|
422 ResourcesEnums type) { |
|
423 if (project == null || options == null) { |
|
424 throw new IllegalArgumentException("Arguments cannot be null."); |
|
425 } |
|
426 |
|
427 String folderName = type.arg(); |
|
428 |
|
429 // First delete any existing resources from their folder |
|
430 deleteResources(project, folderName); |
|
431 |
|
432 // If options is empty, we simply need to invoke updateResource() with |
|
433 // the empty |
|
434 // String to ensure that the corresponding folder is created and then |
|
435 // return. |
|
436 if (options.length == 0) { |
|
437 updateResource(project, folderName, ""); |
|
438 return; |
|
439 } |
|
440 |
|
441 for (String option : options) { |
|
442 updateResource(project, folderName, option); |
|
443 } |
|
444 } |
|
445 |
|
446 /** |
|
447 * Updates the S12 XML file resources within the project. |
|
448 * |
|
449 * @param project |
|
450 * The project to amend |
|
451 * @param options |
|
452 * An array of filenames or the empty array if there are no files |
|
453 * @return void |
|
454 */ |
|
455 public static void updateS12XmlFiles(IProject project, String[] options) { |
|
456 updateResources(project, options, S12_XML); |
|
457 |
|
458 // persist the data |
|
459 getPersistDataStore(project).setSelectedS12XmlFiles(options); |
|
460 } |
|
461 |
|
462 /** |
|
463 * Updates the shapes file resources within the project. |
|
464 * |
|
465 * @param project |
|
466 * The project to amend |
|
467 * @param options |
|
468 * An array of filenames or the empty array if there are no files |
|
469 * @return void |
|
470 */ |
|
471 public static void updateShapesFiles(IProject project, String[] options) { |
|
472 updateResources(project, options, SHAPES); |
|
473 |
|
474 // persist the data |
|
475 getPersistDataStore(project).setSelectedShapesFiles(options); |
|
476 |
|
477 } |
|
478 |
|
479 /** |
|
480 * Updates the system definition files within the project. |
|
481 * |
|
482 * @param project |
|
483 * The project to amend |
|
484 * @param systemDefinitionFiles |
|
485 * The system definition files to be used by the project |
|
486 * @param force |
|
487 * Forces new shortcuts to be created in the project for the |
|
488 * system definition files |
|
489 * @return void |
|
490 */ |
|
491 public static void updateSystemDefinitionFiles(IProject project, |
|
492 String[] sysdefFiles, Boolean force) { |
|
493 HashMap<String, Integer> systemDefinitionFiles = new HashMap<String, Integer>(); |
|
494 |
|
495 // Work out if we need to add any numbers to filenames to keep them |
|
496 // unique |
|
497 // It is not possible to have multiple files with the same name, so if |
|
498 // this does happen a |
|
499 // number is added to further instances, e.g afile, afile(2), afile(3) |
|
500 // etc. |
|
501 HashMap<String, Integer> systemDefinitionFilesCounts = new HashMap<String, Integer>(); |
|
502 |
|
503 java.util.regex.Pattern p = Pattern.compile("\\((\\d+)\\)\\.xml", |
|
504 Pattern.CASE_INSENSITIVE); |
|
505 |
|
506 try { |
|
507 IResource[] members; |
|
508 members = project.members(); |
|
509 for (IResource res : members) { |
|
510 |
|
511 java.util.regex.Matcher m = p.matcher(res.getName()); |
|
512 |
|
513 if (m.find()) { |
|
514 int num = Integer.valueOf(m.group(1)); |
|
515 String simpleName = m.replaceAll(".xml"); |
|
516 if (!systemDefinitionFilesCounts.containsKey(simpleName |
|
517 .toLowerCase()) |
|
518 || systemDefinitionFilesCounts.get(simpleName |
|
519 .toLowerCase()) < ++num) { |
|
520 systemDefinitionFilesCounts.put(simpleName |
|
521 .toLowerCase(), ++num); |
|
522 } |
|
523 } else { |
|
524 if (!systemDefinitionFilesCounts.containsKey(res.getName() |
|
525 .toLowerCase())) { |
|
526 systemDefinitionFilesCounts.put(res.getName() |
|
527 .toLowerCase(), 2); |
|
528 } |
|
529 } |
|
530 } |
|
531 } catch (CoreException e) { |
|
532 e.printStackTrace(); |
|
533 } |
|
534 |
|
535 // Get the existing system definition files and put them into a hashmap |
|
536 IScopeContext projectScope = new ProjectScope(project); |
|
537 PersistentDataStore projectStore = new PersistentDataStore(projectScope |
|
538 .getNode(Activator.PLUGIN_ID)); |
|
539 |
|
540 for (String sysdefFile : projectStore.getSystemDefinitionFiles()) { |
|
541 systemDefinitionFiles.put(sysdefFile.toLowerCase(), 0); |
|
542 } |
|
543 |
|
544 for (String sysdefFile : sysdefFiles) { |
|
545 // If the sysdef file has not been added or removed set to 1 |
|
546 if (systemDefinitionFiles.containsKey(sysdefFile.toLowerCase()) |
|
547 && force != true) { |
|
548 systemDefinitionFiles.put(sysdefFile.toLowerCase(), 1); |
|
549 } |
|
550 // Otherwise the file must be a new file |
|
551 else { |
|
552 systemDefinitionFiles.put(sysdefFile.toLowerCase(), 2); |
|
553 } |
|
554 } |
|
555 |
|
556 for (String sysdefFile : systemDefinitionFiles.keySet()) { |
|
557 // All files with a value of 0 must be deleted |
|
558 if (systemDefinitionFiles.get(sysdefFile) == 0) { |
|
559 IResource[] members; |
|
560 |
|
561 try { |
|
562 members = project.members(); |
|
563 |
|
564 for (IResource res : members) { |
|
565 if (isLocalPath(sysdefFile)) { |
|
566 if (res.isLinked()) { |
|
567 if (sysdefFile.equalsIgnoreCase(res |
|
568 .getRawLocation().toOSString())) { |
|
569 res.delete(true, null); |
|
570 } |
|
571 } |
|
572 } else { // If it is a URL |
|
573 if (res instanceof IFile) { |
|
574 String urlString = getResourceUrl((IFile) res); |
|
575 |
|
576 if (urlString != null |
|
577 && sysdefFile |
|
578 .equalsIgnoreCase(urlString)) { |
|
579 res.delete(true, null); |
|
580 } |
|
581 } |
|
582 } |
|
583 } |
|
584 |
|
585 } catch (CoreException e) { |
|
586 Logger.log(e.getMessage(), e); |
|
587 } |
|
588 } else if (systemDefinitionFiles.get(sysdefFile.toLowerCase()) == 2) { |
|
589 // All files with a value if 2 must be added |
|
590 String newName; |
|
591 |
|
592 if (isLocalPath(sysdefFile)) { |
|
593 String[] filenameParts = sysdefFile.split("[\\\\/]"); |
|
594 newName = filenameParts[filenameParts.length - 1]; |
|
595 } else { // If option is a URL |
|
596 int beginIndex = sysdefFile.lastIndexOf("/"); |
|
597 newName = sysdefFile.substring(beginIndex + 1, sysdefFile |
|
598 .length()); |
|
599 } |
|
600 |
|
601 int i = 1; |
|
602 |
|
603 if (systemDefinitionFilesCounts.containsKey(newName |
|
604 .toLowerCase())) { |
|
605 i = systemDefinitionFilesCounts.get(newName.toLowerCase()); |
|
606 |
|
607 newName = newName.substring(0, newName.length() - 4) |
|
608 + "(" |
|
609 + i |
|
610 + ")" |
|
611 + newName.substring(newName.length() - 4, newName |
|
612 .length()); |
|
613 } |
|
614 |
|
615 systemDefinitionFilesCounts.put(newName.toLowerCase(), ++i); |
|
616 |
|
617 makeFileShortcut(project, null, sysdefFile, newName); |
|
618 } |
|
619 // Files with a value of 1 are unchanged so are ignored |
|
620 } |
|
621 |
|
622 // Persist the system definition files |
|
623 projectStore.setSystemDefinitionFiles(sysdefFiles); |
|
624 } |
|
625 |
|
626 /** |
|
627 * Updates the system info file resources within the project. |
|
628 * |
|
629 * @param project |
|
630 * The project to amend |
|
631 * @param options |
|
632 * An array of filenames or the empty array if there are no files |
|
633 * @return void |
|
634 */ |
|
635 public static void updateSystemInfoFiles(IProject project, String[] options) { |
|
636 updateResources(project, options, SYSTEM_INFO); |
|
637 |
|
638 // persist the data |
|
639 getPersistDataStore(project).setSelectedSystemInfoFiles(options); |
|
640 } |
|
641 |
|
642 } |