]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/workspace/WorkspaceTools.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / workspace / WorkspaceTools.java
... / ...
CommitLineData
1/** @file\r
2\r
3 The file is used to init workspace and get basic information of workspace\r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15package org.tianocore.frameworkwizard.workspace;\r
16\r
17import java.io.IOException;\r
18import java.util.Vector;\r
19\r
20import org.apache.xmlbeans.XmlException;\r
21import org.tianocore.DbPathAndFilename;\r
22import org.tianocore.FrameworkDatabaseDocument;\r
23import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
24import org.tianocore.MsaFilesDocument.MsaFiles;\r
25import org.tianocore.MsaHeaderDocument.MsaHeader;\r
26import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;\r
27import org.tianocore.PlatformHeaderDocument.PlatformHeader;\r
28import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;\r
29import org.tianocore.SpdHeaderDocument.SpdHeader;\r
30import org.tianocore.frameworkwizard.common.DataType;\r
31import org.tianocore.frameworkwizard.common.Identification;\r
32import org.tianocore.frameworkwizard.common.Log;\r
33import org.tianocore.frameworkwizard.common.OpenFile;\r
34import org.tianocore.frameworkwizard.common.SaveFile;\r
35import org.tianocore.frameworkwizard.common.Tools;\r
36import org.tianocore.frameworkwizard.module.Identification.ModuleIdentification;\r
37import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
38import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
39\r
40public class WorkspaceTools {\r
41 //\r
42 // Define class members\r
43 //\r
44 private FrameworkDatabaseDocument.FrameworkDatabase fdb = null;\r
45\r
46 private Vector<ModuleIdentification> vModuleList = new Vector<ModuleIdentification>();\r
47\r
48 private Vector<PackageIdentification> vPackageList = new Vector<PackageIdentification>();\r
49\r
50 private Vector<PlatformIdentification> vPlatformList = new Vector<PlatformIdentification>();\r
51\r
52 /**\r
53 \r
54 Open Framework Database file\r
55 \r
56 */\r
57 private void openFrameworkDb() {\r
58 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
59 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
60 try {\r
61 fdb = OpenFile.openFrameworkDb(strFrameworkDbFilePath);\r
62 } catch (XmlException e) {\r
63 Log.err("Open Framework Database " + strFrameworkDbFilePath, e.getMessage());\r
64 return;\r
65 } catch (Exception e) {\r
66 Log.err("Open Framework Database " + strFrameworkDbFilePath, "Invalid file type");\r
67 return;\r
68 }\r
69 }\r
70\r
71 /**\r
72 Get all modules' paths from one package\r
73 \r
74 @return a Vector with all modules' path\r
75 \r
76 **/\r
77 public Vector<String> getAllModulesOfPackage(String path) {\r
78 Vector<String> modulePath = new Vector<String>();\r
79 try {\r
80 MsaFiles files = OpenFile.openSpdFile(path).getMsaFiles();\r
81 if (files != null) {\r
82 for (int index = 0; index < files.getFilenameList().size(); index++) {\r
83 modulePath.addElement(files.getFilenameList().get(index));\r
84 }\r
85 }\r
86 } catch (IOException e) {\r
87 e.printStackTrace();\r
88 } catch (XmlException e) {\r
89 e.printStackTrace();\r
90 } catch (Exception e) {\r
91 e.printStackTrace();\r
92 }\r
93 return modulePath;\r
94 }\r
95\r
96 /**\r
97 Get all package basic information form the FrameworkDatabase.db file\r
98 \r
99 @return vPackageList A vector includes all packages' basic information\r
100 \r
101 */\r
102 public Vector<PackageIdentification> getAllPackages() {\r
103 Identification id = null;\r
104 vPackageList.removeAllElements();\r
105\r
106 openFrameworkDb();\r
107\r
108 for (int index = 0; index < fdb.getPackageList().getFilenameList().size(); index++) {\r
109 String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
110 + fdb.getPackageList().getFilenameArray(index).getStringValue();\r
111 path = Tools.convertPathToCurrentOsType(path);\r
112 try {\r
113 id = getId(path, OpenFile.openSpdFile(path));\r
114 vPackageList.addElement(new PackageIdentification(id));\r
115 } catch (IOException e) {\r
116 Log.err("Open Package Surface Area " + path, e.getMessage());\r
117 e.printStackTrace();\r
118 } catch (XmlException e) {\r
119 Log.err("Open Package Surface Area " + path, e.getMessage());\r
120 e.printStackTrace();\r
121 } catch (Exception e) {\r
122 Log.err("Open Package Surface Area " + path, "Invalid file type");\r
123 e.printStackTrace();\r
124 }\r
125 }\r
126 return vPackageList;\r
127 }\r
128\r
129 /**\r
130 Get all package basic information form the FrameworkDatabase.db file\r
131 \r
132 @return vPackageList A vector includes all packages' basic information\r
133 \r
134 */\r
135 public Vector<ModuleIdentification> getAllModules() {\r
136 vModuleList.removeAllElements();\r
137 //\r
138 // First get all packages\r
139 //\r
140 getAllPackages();\r
141 Vector<String> modulePaths = new Vector<String>();\r
142 Identification id = null;\r
143 String packagePath = null;\r
144 String modulePath = null;\r
145\r
146 //\r
147 // For each package, get all modules list\r
148 //\r
149 for (int indexI = 0; indexI < vPackageList.size(); indexI++) {\r
150 packagePath = vPackageList.elementAt(indexI).getPath();\r
151 modulePaths = this.getAllModulesOfPackage(packagePath);\r
152 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR)\r
153 + DataType.FILE_SEPARATOR.length());\r
154 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {\r
155 modulePath = Tools.convertPathToCurrentOsType(packagePath + modulePaths.elementAt(indexJ));\r
156 try {\r
157 id = getId(modulePath, OpenFile.openMsaFile(modulePath));\r
158 vModuleList.addElement(new ModuleIdentification(id, vPackageList.elementAt(indexI)));\r
159 } catch (IOException e) {\r
160 Log.err("Open Module Surface Area " + modulePath, e.getMessage());\r
161 e.printStackTrace();\r
162 } catch (XmlException e) {\r
163 Log.err("Open Module Surface Area " + modulePath, e.getMessage());\r
164 e.printStackTrace();\r
165 } catch (Exception e) {\r
166 Log.err("Open Module Surface Area " + modulePath, "Invalid file type");\r
167 e.printStackTrace();\r
168 }\r
169 }\r
170 }\r
171\r
172 return vModuleList;\r
173 }\r
174\r
175 /**\r
176 Get all platform basic information form the FrameworkDatabase.db file\r
177 \r
178 @return vplatformList A vector includes all platforms' basic information\r
179 \r
180 */\r
181 public Vector<PlatformIdentification> getAllPlatforms() {\r
182 Identification id = null;\r
183 vPlatformList.removeAllElements();\r
184\r
185 openFrameworkDb();\r
186\r
187 for (int index = 0; index < fdb.getPlatformList().getFilenameList().size(); index++) {\r
188 String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
189 + fdb.getPlatformList().getFilenameArray(index).getStringValue();\r
190 path = Tools.convertPathToCurrentOsType(path);\r
191 try {\r
192 id = getId(path, OpenFile.openFpdFile(path));\r
193 vPlatformList.addElement(new PlatformIdentification(id));\r
194 } catch (IOException e) {\r
195 Log.err("Open Platform Surface Area " + path, e.getMessage());\r
196 e.printStackTrace();\r
197 } catch (XmlException e) {\r
198 Log.err("Open Platform Surface Area " + path, e.getMessage());\r
199 e.printStackTrace();\r
200 } catch (Exception e) {\r
201 Log.err("Open Platform Surface Area " + path, "Invalid file type");\r
202 e.printStackTrace();\r
203 }\r
204 }\r
205 return vPlatformList;\r
206 }\r
207\r
208 /**\r
209 Get all Library Class Definitions from a package\r
210 \r
211 @return Vector\r
212 **/\r
213 public Vector<String> getAllLibraryClassDefinitionsFromPackage(PackageSurfaceArea spd) {\r
214 Vector<String> vector = new Vector<String>();\r
215 if (spd.getLibraryClassDeclarations() != null) {\r
216 if (spd.getLibraryClassDeclarations().getLibraryClassList().size() > 0) {\r
217 for (int index = 0; index < spd.getLibraryClassDeclarations().getLibraryClassList().size(); index++) {\r
218 vector.addElement(spd.getLibraryClassDeclarations().getLibraryClassList().get(index).getName());\r
219 }\r
220 }\r
221 }\r
222 return vector;\r
223 }\r
224\r
225 /**\r
226 Get all Protocol Definitions from a package\r
227 \r
228 @return Vector\r
229 **/\r
230 public Vector<String> getAllProtocolDeclarationsFromPackage(PackageSurfaceArea spd) {\r
231 Vector<String> vector = new Vector<String>();\r
232 if (spd.getProtocolDeclarations() != null) {\r
233 if (spd.getProtocolDeclarations().getEntryList().size() > 0) {\r
234 for (int index = 0; index < spd.getProtocolDeclarations().getEntryList().size(); index++) {\r
235 vector.addElement(spd.getProtocolDeclarations().getEntryList().get(index).getCName());\r
236 }\r
237 }\r
238 }\r
239 return vector;\r
240 }\r
241\r
242 /**\r
243 Get all Ppi Definitions from a package\r
244 \r
245 @return Vector\r
246 **/\r
247 public Vector<String> getAllPpiDeclarationsFromPackage(PackageSurfaceArea spd) {\r
248 Vector<String> vector = new Vector<String>();\r
249 if (spd.getPpiDeclarations() != null) {\r
250 if (spd.getPpiDeclarations().getEntryList().size() > 0) {\r
251 for (int index = 0; index < spd.getPpiDeclarations().getEntryList().size(); index++) {\r
252 vector.addElement(spd.getPpiDeclarations().getEntryList().get(index).getCName());\r
253 }\r
254 } \r
255 }\r
256 return vector;\r
257 }\r
258\r
259 /**\r
260 Get all Guid Definitions from a package\r
261 \r
262 @return Vector\r
263 **/\r
264 public Vector<String> getAllGuidDeclarationsFromPackage(PackageSurfaceArea spd) {\r
265 Vector<String> vector = new Vector<String>();\r
266 if (spd.getGuidDeclarations() != null) {\r
267 if (spd.getGuidDeclarations().getEntryList().size() > 0) {\r
268 for (int index = 0; index < spd.getGuidDeclarations().getEntryList().size(); index++) {\r
269 vector.addElement(spd.getGuidDeclarations().getEntryList().get(index).getCName());\r
270 }\r
271 } \r
272 }\r
273 return vector;\r
274 }\r
275\r
276 /**\r
277 Get all Pcd Definitions from a package\r
278 \r
279 @return Vector\r
280 **/\r
281 public Vector<String> getAllPcdDeclarationsFromPackage(PackageSurfaceArea spd) {\r
282 Vector<String> vector = new Vector<String>();\r
283 if (spd.getPcdDeclarations() != null) {\r
284 if (spd.getPcdDeclarations().getPcdEntryList().size() > 0) {\r
285 for (int index = 0; index < spd.getPcdDeclarations().getPcdEntryList().size(); index++) {\r
286 vector.addElement(spd.getPcdDeclarations().getPcdEntryList().get(index).getCName());\r
287 }\r
288 } \r
289 }\r
290 return vector;\r
291 }\r
292\r
293 public Vector<String> getAllLibraryClassDefinitionsFromWorkspace() {\r
294 //\r
295 // First get all packages\r
296 //\r
297 this.getAllPackages();\r
298\r
299 Vector<String> vector = new Vector<String>();\r
300 for (int index = 0; index < this.vPackageList.size(); index++) {\r
301 try {\r
302 Vector<String> v = getAllLibraryClassDefinitionsFromPackage(OpenFile\r
303 .openSpdFile(vPackageList\r
304 .get(index)\r
305 .getPath()));\r
306 if (v != null && v.size() > 0) {\r
307 vector.addAll(v);\r
308 }\r
309 } catch (IOException e) {\r
310 // TODO Auto-generated catch block\r
311 e.printStackTrace();\r
312 } catch (XmlException e) {\r
313 // TODO Auto-generated catch block\r
314 e.printStackTrace();\r
315 } catch (Exception e) {\r
316 // TODO Auto-generated catch block\r
317 e.printStackTrace();\r
318 }\r
319 }\r
320 return vector;\r
321 }\r
322\r
323 public Vector<String> getAllProtocolDeclarationsFromWorkspace() {\r
324 //\r
325 // First get all packages\r
326 //\r
327 this.getAllPackages();\r
328\r
329 Vector<String> vector = new Vector<String>();\r
330 for (int index = 0; index < this.vPackageList.size(); index++) {\r
331 try {\r
332 Vector<String> v = getAllProtocolDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)\r
333 .getPath()));\r
334 if (v != null && v.size() > 0) {\r
335 vector.addAll(v);\r
336 }\r
337 } catch (IOException e) {\r
338 // TODO Auto-generated catch block\r
339 e.printStackTrace();\r
340 } catch (XmlException e) {\r
341 // TODO Auto-generated catch block\r
342 e.printStackTrace();\r
343 } catch (Exception e) {\r
344 // TODO Auto-generated catch block\r
345 e.printStackTrace();\r
346 }\r
347 }\r
348 return vector;\r
349 }\r
350\r
351 public Vector<String> getAllPpiDeclarationsFromWorkspace() {\r
352 //\r
353 // First get all packages\r
354 //\r
355 this.getAllPackages();\r
356\r
357 Vector<String> vector = new Vector<String>();\r
358 for (int index = 0; index < this.vPackageList.size(); index++) {\r
359 try {\r
360 Vector<String> v = getAllPpiDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)\r
361 .getPath()));\r
362 if (v != null && v.size() > 0) {\r
363 vector.addAll(v);\r
364 }\r
365 } catch (IOException e) {\r
366 // TODO Auto-generated catch block\r
367 e.printStackTrace();\r
368 } catch (XmlException e) {\r
369 // TODO Auto-generated catch block\r
370 e.printStackTrace();\r
371 } catch (Exception e) {\r
372 // TODO Auto-generated catch block\r
373 e.printStackTrace();\r
374 }\r
375 }\r
376 return vector;\r
377 }\r
378\r
379 public Vector<String> getAllGuidDeclarationsFromWorkspace() {\r
380 //\r
381 // First get all packages\r
382 //\r
383 this.getAllPackages();\r
384\r
385 Vector<String> vector = new Vector<String>();\r
386 for (int index = 0; index < this.vPackageList.size(); index++) {\r
387 try {\r
388 Vector<String> v = getAllGuidDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)\r
389 .getPath()));\r
390 if (v != null && v.size() > 0) {\r
391 vector.addAll(v);\r
392 }\r
393 } catch (IOException e) {\r
394 // TODO Auto-generated catch block\r
395 e.printStackTrace();\r
396 } catch (XmlException e) {\r
397 // TODO Auto-generated catch block\r
398 e.printStackTrace();\r
399 } catch (Exception e) {\r
400 // TODO Auto-generated catch block\r
401 e.printStackTrace();\r
402 }\r
403 }\r
404 return vector;\r
405 }\r
406\r
407 public Vector<String> getAllPcdDeclarationsFromWorkspace() {\r
408 //\r
409 // First get all packages\r
410 //\r
411 this.getAllPackages();\r
412\r
413 Vector<String> vector = new Vector<String>();\r
414 for (int index = 0; index < this.vPackageList.size(); index++) {\r
415 try {\r
416 Vector<String> v = getAllPcdDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)\r
417 .getPath()));\r
418 if (v != null && v.size() > 0) {\r
419 vector.addAll(v);\r
420 }\r
421 } catch (IOException e) {\r
422 // TODO Auto-generated catch block\r
423 e.printStackTrace();\r
424 } catch (XmlException e) {\r
425 // TODO Auto-generated catch block\r
426 e.printStackTrace();\r
427 } catch (Exception e) {\r
428 // TODO Auto-generated catch block\r
429 e.printStackTrace();\r
430 }\r
431 }\r
432 return vector;\r
433 }\r
434\r
435 /**\r
436 Find a module's package's id\r
437 \r
438 @param id input module id\r
439 @return package id\r
440 \r
441 **/\r
442 public PackageIdentification getPackageIdByModuleId(Identification id) {\r
443 getAllPackages();\r
444 Vector<String> modulePaths = new Vector<String>();\r
445 Identification mid = null;\r
446 String packagePath = null;\r
447 String modulePath = null;\r
448\r
449 //\r
450 // For each package, get all modules list\r
451 //\r
452 for (int indexI = 0; indexI < vPackageList.size(); indexI++) {\r
453 packagePath = vPackageList.elementAt(indexI).getPath();\r
454 modulePaths = this.getAllModulesOfPackage(packagePath);\r
455 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR)\r
456 + DataType.FILE_SEPARATOR.length());\r
457 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {\r
458 modulePath = Tools.convertPathToCurrentOsType(packagePath + modulePaths.elementAt(indexJ));\r
459 try {\r
460 mid = getId(modulePath, OpenFile.openMsaFile(modulePath));\r
461 //\r
462 // Check id\r
463 //\r
464 if (mid.equals(id)) {\r
465 return vPackageList.elementAt(indexI);\r
466 }\r
467 } catch (IOException e) {\r
468 e.printStackTrace();\r
469 } catch (XmlException e) {\r
470 e.printStackTrace();\r
471 } catch (Exception e) {\r
472 e.printStackTrace();\r
473 }\r
474 }\r
475 }\r
476\r
477 return null;\r
478 }\r
479\r
480 public Identification getId(String path, ModuleSurfaceArea msa) {\r
481 MsaHeader head = msa.getMsaHeader();\r
482 String name = head.getModuleName();\r
483 String guid = head.getGuidValue();\r
484 String version = head.getVersion();\r
485 Identification id = new Identification(name, guid, version, path);\r
486 return id;\r
487 }\r
488\r
489 public Identification getId(String path, PackageSurfaceArea spd) {\r
490 SpdHeader head = spd.getSpdHeader();\r
491 String name = head.getPackageName();\r
492 String guid = head.getGuidValue();\r
493 String version = head.getVersion();\r
494 Identification id = new Identification(name, guid, version, path);\r
495 return id;\r
496 }\r
497\r
498 public Identification getId(String path, PlatformSurfaceArea fpd) {\r
499 PlatformHeader head = fpd.getPlatformHeader();\r
500 String name = head.getPlatformName();\r
501 String guid = head.getGuidValue();\r
502 String version = head.getVersion();\r
503 Identification id = new Identification(name, guid, version, path);\r
504 return id;\r
505 }\r
506\r
507 /**\r
508 Add module information to package surface area\r
509 \r
510 @param mid\r
511 @throws IOException\r
512 @throws XmlException\r
513 @throws Exception\r
514 \r
515 **/\r
516 public void addModuleToPackage(ModuleIdentification mid, PackageSurfaceArea spd) throws IOException, XmlException,\r
517 Exception {\r
518 String packagePath = mid.getPackageId().getPath();\r
519 String modulePath = mid.getPath();\r
520 if (spd == null) {\r
521 spd = OpenFile.openSpdFile(packagePath);\r
522 }\r
523 MsaFiles msaFile = spd.getMsaFiles();\r
524 if (msaFile == null) {\r
525 msaFile = MsaFiles.Factory.newInstance();\r
526 }\r
527 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR));\r
528 String fn = Tools.getRelativePath(modulePath, packagePath);\r
529 msaFile.addNewFilename();\r
530 msaFile.setFilenameArray(msaFile.getFilenameList().size() - 1, fn);\r
531 spd.setMsaFiles(msaFile);\r
532 SaveFile.saveSpdFile(mid.getPackageId().getPath(), spd);\r
533 }\r
534\r
535 /**\r
536 Add input package into database\r
537 \r
538 @param id\r
539 * @throws Exception \r
540 \r
541 **/\r
542 public void addPackageToDatabase(PackageIdentification id) throws Exception {\r
543 String path = id.getPath();\r
544 path = Tools.getRelativePath(path, Workspace.getCurrentWorkspace());\r
545 this.openFrameworkDb();\r
546 DbPathAndFilename filename = DbPathAndFilename.Factory.newInstance();\r
547 filename.setStringValue(path);\r
548 fdb.getPackageList().addNewFilename();\r
549 fdb.getPackageList().setFilenameArray(fdb.getPackageList().sizeOfFilenameArray() - 1, filename);\r
550 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
551 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
552 SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);\r
553 }\r
554\r
555 /**\r
556 Add input package into database\r
557 \r
558 @param id\r
559 * @throws Exception \r
560 \r
561 **/\r
562 public void addPlatformToDatabase(PlatformIdentification id) throws Exception {\r
563 String path = id.getPath();\r
564 path = Tools.getRelativePath(path, Workspace.getCurrentWorkspace());\r
565 this.openFrameworkDb();\r
566 DbPathAndFilename filename = DbPathAndFilename.Factory.newInstance();\r
567 filename.setStringValue(path);\r
568 fdb.getPlatformList().addNewFilename();\r
569 fdb.getPlatformList().setFilenameArray(fdb.getPlatformList().sizeOfFilenameArray() - 1, filename);\r
570 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
571 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
572 SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);\r
573 }\r
574}\r