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