3 The file is used to init workspace and get basic information of workspace
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
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.
15 package org
.tianocore
.frameworkwizard
.workspace
;
17 import java
.io
.IOException
;
18 import java
.util
.Vector
;
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
;
42 public class WorkspaceTools
{
44 // Define class members
46 private FrameworkDatabaseDocument
.FrameworkDatabase fdb
= null;
48 private Vector
<ModuleIdentification
> vModuleList
= new Vector
<ModuleIdentification
>();
50 private Vector
<PackageIdentification
> vPackageList
= new Vector
<PackageIdentification
>();
52 private Vector
<PlatformIdentification
> vPlatformList
= new Vector
<PlatformIdentification
>();
56 Open Framework Database file
59 private void openFrameworkDb() {
60 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
61 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
63 fdb
= OpenFile
.openFrameworkDb(strFrameworkDbFilePath
);
64 } catch (XmlException e
) {
65 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, e
.getMessage());
67 } catch (Exception e
) {
68 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, "Invalid file type");
74 Get all modules' paths from one package
76 @return a Vector with all modules' path
79 public Vector
<String
> getAllModulesOfPackage(String path
) {
80 Vector
<String
> modulePath
= new Vector
<String
>();
82 MsaFiles files
= OpenFile
.openSpdFile(path
).getMsaFiles();
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
);
91 } catch (IOException e
) {
93 } catch (XmlException e
) {
95 } catch (Exception e
) {
102 Get all Industry Std Includes' paths from one package
104 @return a Vector with all paths
107 public Vector
<String
> getAllIndustryStdIncludesOfPackage(String path
) {
108 Vector
<String
> includePath
= new Vector
<String
>();
110 IndustryStdIncludes files
= OpenFile
.openSpdFile(path
).getIndustryStdIncludes();
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
);
119 } catch (IOException e
) {
121 } catch (XmlException e
) {
123 } catch (Exception e
) {
130 Get all package basic information form the FrameworkDatabase.db file
132 @return vPackageList A vector includes all packages' basic information
135 public Vector
<PackageIdentification
> getAllPackages() {
136 Identification id
= null;
137 vPackageList
.removeAllElements();
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
);
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());
151 } catch (XmlException e
) {
152 Log
.err("Open Package Surface Area " + path
, e
.getMessage());
154 } catch (Exception e
) {
155 Log
.err("Open Package Surface Area " + path
, "Invalid file type");
159 Tools
.sortPackages(vPackageList
, DataType
.SORT_TYPE_ASCENDING
);
164 Get all module basic information from a package
167 @return A vector includes all modules' basic information
170 public Vector
<ModuleIdentification
> getAllModules(PackageIdentification pid
) {
171 Vector
<ModuleIdentification
> v
= new Vector
<ModuleIdentification
>();
172 Vector
<String
> modulePaths
= this.getAllModulesOfPackage(pid
.getPath());
173 Identification id
= null;
174 String modulePath
= null;
176 for (int index
= 0; index
< modulePaths
.size(); index
++) {
178 modulePath
= modulePaths
.get(index
);
179 id
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
180 } catch (IOException e
) {
181 Log
.log("Error when Open Module Surface Area " + modulePath
, e
.getMessage());
182 } catch (XmlException e
) {
183 Log
.log("Error when Open Module Surface Area " + modulePath
, e
.getMessage());
184 } catch (Exception e
) {
185 Log
.log("Error when Open Module Surface Area " + modulePath
, "Invalid file type " + e
.getMessage());
187 v
.addElement(new ModuleIdentification(id
, pid
));
189 Tools
.sortModules(v
, DataType
.SORT_TYPE_ASCENDING
);
195 Get all module basic information form the FrameworkDatabase.db file
197 @return vModuleList A vector includes all modules' basic information
200 public Vector
<ModuleIdentification
> getAllModules() {
201 vModuleList
.removeAllElements();
203 // First get all packages
206 Vector
<String
> modulePaths
= new Vector
<String
>();
207 Identification id
= null;
208 String packagePath
= null;
209 String modulePath
= null;
212 // For each package, get all modules list
214 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
215 packagePath
= vPackageList
.elementAt(indexI
).getPath();
216 modulePaths
= this.getAllModulesOfPackage(packagePath
);
218 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
220 modulePath
= modulePaths
.get(indexJ
);
221 id
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
222 vModuleList
.addElement(new ModuleIdentification(id
, vPackageList
.elementAt(indexI
)));
223 } catch (IOException e
) {
224 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
225 } catch (XmlException e
) {
226 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
227 } catch (Exception e
) {
228 Log
.err("Open Module Surface Area " + modulePath
, "Invalid file type");
232 Tools
.sortModules(vModuleList
, DataType
.SORT_TYPE_ASCENDING
);
237 Get all platform basic information form the FrameworkDatabase.db file
239 @return vplatformList A vector includes all platforms' basic information
242 public Vector
<PlatformIdentification
> getAllPlatforms() {
243 Identification id
= null;
244 vPlatformList
.removeAllElements();
248 for (int index
= 0; index
< fdb
.getPlatformList().getFilenameList().size(); index
++) {
249 String path
= Workspace
.getCurrentWorkspace() + DataType
.FILE_SEPARATOR
250 + fdb
.getPlatformList().getFilenameArray(index
).getStringValue();
251 path
= Tools
.convertPathToCurrentOsType(path
);
253 id
= getId(path
, OpenFile
.openFpdFile(path
));
254 vPlatformList
.addElement(new PlatformIdentification(id
));
255 } catch (IOException e
) {
256 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
257 } catch (XmlException e
) {
258 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
259 } catch (Exception e
) {
260 Log
.err("Open Platform Surface Area " + path
, "Invalid file type");
263 Tools
.sortPlatforms(vPlatformList
, DataType
.SORT_TYPE_ASCENDING
);
264 return vPlatformList
;
268 Get all Library Class Definitions from a package
272 public Vector
<String
> getAllLibraryClassDefinitionsFromPackage(PackageSurfaceArea spd
) {
273 Vector
<String
> vector
= new Vector
<String
>();
274 if (spd
.getLibraryClassDeclarations() != null) {
275 if (spd
.getLibraryClassDeclarations().getLibraryClassList().size() > 0) {
276 for (int index
= 0; index
< spd
.getLibraryClassDeclarations().getLibraryClassList().size(); index
++) {
277 vector
.addElement(spd
.getLibraryClassDeclarations().getLibraryClassList().get(index
).getName());
281 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
286 Get all Protocol Definitions from a package
290 public Vector
<String
> getAllProtocolDeclarationsFromPackage(PackageSurfaceArea spd
) {
291 Vector
<String
> vector
= new Vector
<String
>();
292 if (spd
.getProtocolDeclarations() != null) {
293 if (spd
.getProtocolDeclarations().getEntryList().size() > 0) {
294 for (int index
= 0; index
< spd
.getProtocolDeclarations().getEntryList().size(); index
++) {
295 vector
.addElement(spd
.getProtocolDeclarations().getEntryList().get(index
).getCName());
299 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
304 Get all Ppi Definitions from a package
308 public Vector
<String
> getAllPpiDeclarationsFromPackage(PackageSurfaceArea spd
) {
309 Vector
<String
> vector
= new Vector
<String
>();
310 if (spd
.getPpiDeclarations() != null) {
311 if (spd
.getPpiDeclarations().getEntryList().size() > 0) {
312 for (int index
= 0; index
< spd
.getPpiDeclarations().getEntryList().size(); index
++) {
313 vector
.addElement(spd
.getPpiDeclarations().getEntryList().get(index
).getCName());
317 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
322 Get all Guid Definitions from a package
326 public Vector
<String
> getAllGuidDeclarationsFromPackage(PackageSurfaceArea spd
) {
327 Vector
<String
> vector
= new Vector
<String
>();
328 if (spd
.getGuidDeclarations() != null) {
329 if (spd
.getGuidDeclarations().getEntryList().size() > 0) {
330 for (int index
= 0; index
< spd
.getGuidDeclarations().getEntryList().size(); index
++) {
331 vector
.addElement(spd
.getGuidDeclarations().getEntryList().get(index
).getCName());
335 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
340 Get all Pcd Definitions from a package
344 public Vector
<String
> getAllPcdDeclarationsFromPackage(PackageSurfaceArea spd
) {
345 Vector
<String
> vector
= new Vector
<String
>();
346 if (spd
.getPcdDeclarations() != null) {
347 if (spd
.getPcdDeclarations().getPcdEntryList().size() > 0) {
348 for (int index
= 0; index
< spd
.getPcdDeclarations().getPcdEntryList().size(); index
++) {
349 vector
.addElement(spd
.getPcdDeclarations().getPcdEntryList().get(index
).getCName());
353 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
357 public Vector
<String
> getAllLibraryClassDefinitionsFromWorkspace() {
359 // First get all packages
361 this.getAllPackages();
363 Vector
<String
> vector
= new Vector
<String
>();
364 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
366 Vector
<String
> v
= getAllLibraryClassDefinitionsFromPackage(OpenFile
367 .openSpdFile(vPackageList
370 if (v
!= null && v
.size() > 0) {
373 } catch (IOException e
) {
374 // TODO Auto-generated catch block
375 } catch (XmlException e
) {
376 // TODO Auto-generated catch block
377 } catch (Exception e
) {
378 // TODO Auto-generated catch block
381 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
385 public Vector
<String
> getAllProtocolDeclarationsFromWorkspace() {
387 // First get all packages
389 this.getAllPackages();
391 Vector
<String
> vector
= new Vector
<String
>();
392 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
394 Vector
<String
> v
= getAllProtocolDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
396 if (v
!= null && v
.size() > 0) {
399 } catch (IOException e
) {
400 // TODO Auto-generated catch block
401 } catch (XmlException e
) {
402 // TODO Auto-generated catch block
403 } catch (Exception e
) {
404 // TODO Auto-generated catch block
407 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
411 public Vector
<String
> getAllPpiDeclarationsFromWorkspace() {
413 // First get all packages
415 this.getAllPackages();
417 Vector
<String
> vector
= new Vector
<String
>();
418 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
420 Vector
<String
> v
= getAllPpiDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
422 if (v
!= null && v
.size() > 0) {
425 } catch (IOException e
) {
426 // TODO Auto-generated catch block
427 } catch (XmlException e
) {
428 // TODO Auto-generated catch block
429 } catch (Exception e
) {
430 // TODO Auto-generated catch block
433 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
437 public Vector
<String
> getAllGuidDeclarationsFromWorkspace() {
439 // First get all packages
441 this.getAllPackages();
443 Vector
<String
> vector
= new Vector
<String
>();
444 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
446 Vector
<String
> v
= getAllGuidDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
448 if (v
!= null && v
.size() > 0) {
451 } catch (IOException e
) {
452 // TODO Auto-generated catch block
453 } catch (XmlException e
) {
454 // TODO Auto-generated catch block
455 } catch (Exception e
) {
456 // TODO Auto-generated catch block
459 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
463 public Vector
<String
> getAllPcdDeclarationsFromWorkspace() {
465 // First get all packages
467 this.getAllPackages();
469 Vector
<String
> vector
= new Vector
<String
>();
470 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
472 Vector
<String
> v
= getAllPcdDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
474 if (v
!= null && v
.size() > 0) {
477 } catch (IOException e
) {
478 // TODO Auto-generated catch block
479 } catch (XmlException e
) {
480 // TODO Auto-generated catch block
481 } catch (Exception e
) {
482 // TODO Auto-generated catch block
485 Tools
.sortVectorString(vector
, DataType
.SORT_TYPE_ASCENDING
);
490 Find a module's package's id
492 @param id input module id
496 public PackageIdentification
getPackageIdByModuleId(Identification id
) {
498 Vector
<String
> modulePaths
= new Vector
<String
>();
499 Identification mid
= null;
500 String packagePath
= null;
501 String modulePath
= null;
504 // For each package, get all modules list
506 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
507 packagePath
= vPackageList
.elementAt(indexI
).getPath();
508 modulePaths
= this.getAllModulesOfPackage(packagePath
);
509 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
510 modulePath
= modulePaths
.get(indexJ
);
512 mid
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
516 if (mid
.equals(id
)) {
517 return vPackageList
.elementAt(indexI
);
519 } catch (IOException e
) {
521 } catch (XmlException e
) {
523 } catch (Exception e
) {
532 public Identification
getId(String path
, ModuleSurfaceArea msa
) {
533 MsaHeader head
= msa
.getMsaHeader();
534 String name
= head
.getModuleName();
535 String guid
= head
.getGuidValue();
536 String version
= head
.getVersion();
537 Identification id
= new Identification(name
, guid
, version
, path
);
541 public Identification
getId(String path
, PackageSurfaceArea spd
) {
542 SpdHeader head
= spd
.getSpdHeader();
543 String name
= head
.getPackageName();
544 String guid
= head
.getGuidValue();
545 String version
= head
.getVersion();
546 Identification id
= new Identification(name
, guid
, version
, path
);
550 public Identification
getId(String path
, PlatformSurfaceArea fpd
) {
551 PlatformHeader head
= fpd
.getPlatformHeader();
552 String name
= head
.getPlatformName();
553 String guid
= head
.getGuidValue();
554 String version
= head
.getVersion();
555 Identification id
= new Identification(name
, guid
, version
, path
);
560 Add module information to package surface area
568 public void addModuleToPackage(ModuleIdentification mid
, PackageSurfaceArea spd
) throws IOException
, XmlException
,
570 String packagePath
= mid
.getPackageId().getPath();
571 String modulePath
= mid
.getPath();
573 spd
= OpenFile
.openSpdFile(packagePath
);
575 MsaFiles msaFile
= spd
.getMsaFiles();
576 if (msaFile
== null) {
577 msaFile
= MsaFiles
.Factory
.newInstance();
579 packagePath
= packagePath
.substring(0, packagePath
.lastIndexOf(DataType
.FILE_SEPARATOR
));
580 String fn
= Tools
.getRelativePath(modulePath
, packagePath
);
581 msaFile
.addNewFilename();
582 msaFile
.setFilenameArray(msaFile
.getFilenameList().size() - 1, fn
);
583 spd
.setMsaFiles(msaFile
);
584 SaveFile
.saveSpdFile(mid
.getPackageId().getPath(), spd
);
588 Add input package into database
594 public void addPackageToDatabase(PackageIdentification id
) throws Exception
{
595 String path
= id
.getPath();
596 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
597 this.openFrameworkDb();
598 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
599 filename
.setStringValue(path
);
600 fdb
.getPackageList().addNewFilename();
601 fdb
.getPackageList().setFilenameArray(fdb
.getPackageList().sizeOfFilenameArray() - 1, filename
);
602 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
603 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
604 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);
608 Add input package into database
614 public void addPlatformToDatabase(PlatformIdentification id
) throws Exception
{
615 String path
= id
.getPath();
616 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
617 this.openFrameworkDb();
618 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
619 filename
.setStringValue(path
);
620 fdb
.getPlatformList().addNewFilename();
621 fdb
.getPlatformList().setFilenameArray(fdb
.getPlatformList().sizeOfFilenameArray() - 1, filename
);
622 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
623 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
624 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);
628 Get all file's path from one module
637 public Vector
<String
> getAllModuleFilesPath(String path
) throws IOException
, XmlException
, Exception
{
638 Vector
<String
> v
= new Vector
<String
>();
639 path
= Tools
.convertPathToCurrentOsType(path
);
641 ModuleSurfaceArea msa
= OpenFile
.openMsaFile(path
);
644 // Get all files' path of a module
646 SourceFiles sf
= msa
.getSourceFiles();
648 for (int index
= 0; index
< sf
.getFilenameList().size(); index
++) {
649 String temp
= sf
.getFilenameList().get(index
).getStringValue();
650 temp
= Tools
.addFileSeparator(Tools
.getFilePathOnly(path
)) + temp
;
651 v
.addElement(Tools
.convertPathToCurrentOsType(temp
));
660 Get all file's path from one package
669 public Vector
<String
> getAllPakcageFilesPath(String path
) throws IOException
, XmlException
, Exception
{
670 Vector
<String
> v
= new Vector
<String
>();
671 path
= Tools
.convertPathToCurrentOsType(path
);
678 // Add the package's industry std includes one by one
680 Vector
<String
> f1
= new Vector
<String
>();
681 f1
= getAllIndustryStdIncludesOfPackage(path
);
682 for (int index
= 0; index
< f1
.size(); index
++) {
683 v
.addElement(f1
.get(index
));
687 // Add module's files one by one
689 f1
= new Vector
<String
>();
690 f1
= getAllModulesOfPackage(path
);
691 for (int indexI
= 0; indexI
< f1
.size(); indexI
++) {
692 Vector
<String
> f2
= getAllModuleFilesPath(f1
.get(indexI
));
693 for (int indexJ
= 0; indexJ
< f2
.size(); indexJ
++) {
694 v
.addElement(f2
.get(indexJ
));