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
.ModuleSurfaceAreaDocument
.ModuleSurfaceArea
;
24 import org
.tianocore
.MsaFilesDocument
.MsaFiles
;
25 import org
.tianocore
.MsaHeaderDocument
.MsaHeader
;
26 import org
.tianocore
.PackageSurfaceAreaDocument
.PackageSurfaceArea
;
27 import org
.tianocore
.PlatformHeaderDocument
.PlatformHeader
;
28 import org
.tianocore
.PlatformSurfaceAreaDocument
.PlatformSurfaceArea
;
29 import org
.tianocore
.SourceFilesDocument
.SourceFiles
;
30 import org
.tianocore
.SpdHeaderDocument
.SpdHeader
;
31 import org
.tianocore
.frameworkwizard
.common
.DataType
;
32 import org
.tianocore
.frameworkwizard
.common
.Log
;
33 import org
.tianocore
.frameworkwizard
.common
.SaveFile
;
34 import org
.tianocore
.frameworkwizard
.common
.Tools
;
35 import org
.tianocore
.frameworkwizard
.common
.Identifications
.Identification
;
36 import org
.tianocore
.frameworkwizard
.common
.Identifications
.OpenFile
;
37 import org
.tianocore
.frameworkwizard
.module
.Identifications
.ModuleIdentification
;
38 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
39 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
41 public class WorkspaceTools
{
43 // Define class members
45 private FrameworkDatabaseDocument
.FrameworkDatabase fdb
= null;
47 private Vector
<ModuleIdentification
> vModuleList
= new Vector
<ModuleIdentification
>();
49 private Vector
<PackageIdentification
> vPackageList
= new Vector
<PackageIdentification
>();
51 private Vector
<PlatformIdentification
> vPlatformList
= new Vector
<PlatformIdentification
>();
55 Open Framework Database file
58 private void openFrameworkDb() {
59 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
60 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
62 fdb
= OpenFile
.openFrameworkDb(strFrameworkDbFilePath
);
63 } catch (XmlException e
) {
64 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, e
.getMessage());
66 } catch (Exception e
) {
67 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, "Invalid file type");
73 Get all modules' paths from one package
75 @return a Vector with all modules' path
78 public Vector
<String
> getAllModulesOfPackage(String path
) {
79 Vector
<String
> modulePath
= new Vector
<String
>();
81 MsaFiles files
= OpenFile
.openSpdFile(path
).getMsaFiles();
83 for (int index
= 0; index
< files
.getFilenameList().size(); index
++) {
84 String msaPath
= files
.getFilenameList().get(index
);
85 msaPath
= Tools
.addFileSeparator(Tools
.getFilePathOnly(path
)) + msaPath
;
86 msaPath
= Tools
.convertPathToCurrentOsType(msaPath
);
87 modulePath
.addElement(msaPath
);
90 } catch (IOException e
) {
92 } catch (XmlException e
) {
94 } catch (Exception e
) {
101 Get all package basic information form the FrameworkDatabase.db file
103 @return vPackageList A vector includes all packages' basic information
106 public Vector
<PackageIdentification
> getAllPackages() {
107 Identification id
= null;
108 vPackageList
.removeAllElements();
112 for (int index
= 0; index
< fdb
.getPackageList().getFilenameList().size(); index
++) {
113 String path
= Workspace
.getCurrentWorkspace() + DataType
.FILE_SEPARATOR
114 + fdb
.getPackageList().getFilenameArray(index
).getStringValue();
115 path
= Tools
.convertPathToCurrentOsType(path
);
117 id
= getId(path
, OpenFile
.openSpdFile(path
));
118 vPackageList
.addElement(new PackageIdentification(id
));
119 } catch (IOException e
) {
120 Log
.err("Open Package Surface Area " + path
, e
.getMessage());
122 } catch (XmlException e
) {
123 Log
.err("Open Package Surface Area " + path
, e
.getMessage());
125 } catch (Exception e
) {
126 Log
.err("Open Package Surface Area " + path
, "Invalid file type");
134 Get all package basic information form the FrameworkDatabase.db file
136 @return vPackageList A vector includes all packages' basic information
139 public Vector
<ModuleIdentification
> getAllModules() {
140 vModuleList
.removeAllElements();
142 // First get all packages
145 Vector
<String
> modulePaths
= new Vector
<String
>();
146 Identification id
= null;
147 String packagePath
= null;
148 String modulePath
= null;
151 // For each package, get all modules list
153 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
154 packagePath
= vPackageList
.elementAt(indexI
).getPath();
155 modulePaths
= this.getAllModulesOfPackage(packagePath
);
157 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
159 modulePath
= modulePaths
.get(indexJ
);
160 id
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
161 vModuleList
.addElement(new ModuleIdentification(id
, vPackageList
.elementAt(indexI
)));
162 } catch (IOException e
) {
163 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
165 } catch (XmlException e
) {
166 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
168 } catch (Exception e
) {
169 Log
.err("Open Module Surface Area " + modulePath
, "Invalid file type");
179 Get all platform basic information form the FrameworkDatabase.db file
181 @return vplatformList A vector includes all platforms' basic information
184 public Vector
<PlatformIdentification
> getAllPlatforms() {
185 Identification id
= null;
186 vPlatformList
.removeAllElements();
190 for (int index
= 0; index
< fdb
.getPlatformList().getFilenameList().size(); index
++) {
191 String path
= Workspace
.getCurrentWorkspace() + DataType
.FILE_SEPARATOR
192 + fdb
.getPlatformList().getFilenameArray(index
).getStringValue();
193 path
= Tools
.convertPathToCurrentOsType(path
);
195 id
= getId(path
, OpenFile
.openFpdFile(path
));
196 vPlatformList
.addElement(new PlatformIdentification(id
));
197 } catch (IOException e
) {
198 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
200 } catch (XmlException e
) {
201 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
203 } catch (Exception e
) {
204 Log
.err("Open Platform Surface Area " + path
, "Invalid file type");
208 return vPlatformList
;
212 Get all Library Class Definitions from a package
216 public Vector
<String
> getAllLibraryClassDefinitionsFromPackage(PackageSurfaceArea spd
) {
217 Vector
<String
> vector
= new Vector
<String
>();
218 if (spd
.getLibraryClassDeclarations() != null) {
219 if (spd
.getLibraryClassDeclarations().getLibraryClassList().size() > 0) {
220 for (int index
= 0; index
< spd
.getLibraryClassDeclarations().getLibraryClassList().size(); index
++) {
221 vector
.addElement(spd
.getLibraryClassDeclarations().getLibraryClassList().get(index
).getName());
229 Get all Protocol Definitions from a package
233 public Vector
<String
> getAllProtocolDeclarationsFromPackage(PackageSurfaceArea spd
) {
234 Vector
<String
> vector
= new Vector
<String
>();
235 if (spd
.getProtocolDeclarations() != null) {
236 if (spd
.getProtocolDeclarations().getEntryList().size() > 0) {
237 for (int index
= 0; index
< spd
.getProtocolDeclarations().getEntryList().size(); index
++) {
238 vector
.addElement(spd
.getProtocolDeclarations().getEntryList().get(index
).getCName());
246 Get all Ppi Definitions from a package
250 public Vector
<String
> getAllPpiDeclarationsFromPackage(PackageSurfaceArea spd
) {
251 Vector
<String
> vector
= new Vector
<String
>();
252 if (spd
.getPpiDeclarations() != null) {
253 if (spd
.getPpiDeclarations().getEntryList().size() > 0) {
254 for (int index
= 0; index
< spd
.getPpiDeclarations().getEntryList().size(); index
++) {
255 vector
.addElement(spd
.getPpiDeclarations().getEntryList().get(index
).getCName());
263 Get all Guid Definitions from a package
267 public Vector
<String
> getAllGuidDeclarationsFromPackage(PackageSurfaceArea spd
) {
268 Vector
<String
> vector
= new Vector
<String
>();
269 if (spd
.getGuidDeclarations() != null) {
270 if (spd
.getGuidDeclarations().getEntryList().size() > 0) {
271 for (int index
= 0; index
< spd
.getGuidDeclarations().getEntryList().size(); index
++) {
272 vector
.addElement(spd
.getGuidDeclarations().getEntryList().get(index
).getCName());
280 Get all Pcd Definitions from a package
284 public Vector
<String
> getAllPcdDeclarationsFromPackage(PackageSurfaceArea spd
) {
285 Vector
<String
> vector
= new Vector
<String
>();
286 if (spd
.getPcdDeclarations() != null) {
287 if (spd
.getPcdDeclarations().getPcdEntryList().size() > 0) {
288 for (int index
= 0; index
< spd
.getPcdDeclarations().getPcdEntryList().size(); index
++) {
289 vector
.addElement(spd
.getPcdDeclarations().getPcdEntryList().get(index
).getCName());
296 public Vector
<String
> getAllLibraryClassDefinitionsFromWorkspace() {
298 // First get all packages
300 this.getAllPackages();
302 Vector
<String
> vector
= new Vector
<String
>();
303 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
305 Vector
<String
> v
= getAllLibraryClassDefinitionsFromPackage(OpenFile
306 .openSpdFile(vPackageList
309 if (v
!= null && v
.size() > 0) {
312 } catch (IOException e
) {
313 // TODO Auto-generated catch block
315 } catch (XmlException e
) {
316 // TODO Auto-generated catch block
318 } catch (Exception e
) {
319 // TODO Auto-generated catch block
326 public Vector
<String
> getAllProtocolDeclarationsFromWorkspace() {
328 // First get all packages
330 this.getAllPackages();
332 Vector
<String
> vector
= new Vector
<String
>();
333 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
335 Vector
<String
> v
= getAllProtocolDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
337 if (v
!= null && v
.size() > 0) {
340 } catch (IOException e
) {
341 // TODO Auto-generated catch block
343 } catch (XmlException e
) {
344 // TODO Auto-generated catch block
346 } catch (Exception e
) {
347 // TODO Auto-generated catch block
354 public Vector
<String
> getAllPpiDeclarationsFromWorkspace() {
356 // First get all packages
358 this.getAllPackages();
360 Vector
<String
> vector
= new Vector
<String
>();
361 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
363 Vector
<String
> v
= getAllPpiDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
365 if (v
!= null && v
.size() > 0) {
368 } catch (IOException e
) {
369 // TODO Auto-generated catch block
371 } catch (XmlException e
) {
372 // TODO Auto-generated catch block
374 } catch (Exception e
) {
375 // TODO Auto-generated catch block
382 public Vector
<String
> getAllGuidDeclarationsFromWorkspace() {
384 // First get all packages
386 this.getAllPackages();
388 Vector
<String
> vector
= new Vector
<String
>();
389 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
391 Vector
<String
> v
= getAllGuidDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
393 if (v
!= null && v
.size() > 0) {
396 } catch (IOException e
) {
397 // TODO Auto-generated catch block
399 } catch (XmlException e
) {
400 // TODO Auto-generated catch block
402 } catch (Exception e
) {
403 // TODO Auto-generated catch block
410 public Vector
<String
> getAllPcdDeclarationsFromWorkspace() {
412 // First get all packages
414 this.getAllPackages();
416 Vector
<String
> vector
= new Vector
<String
>();
417 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
419 Vector
<String
> v
= getAllPcdDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
421 if (v
!= null && v
.size() > 0) {
424 } catch (IOException e
) {
425 // TODO Auto-generated catch block
427 } catch (XmlException e
) {
428 // TODO Auto-generated catch block
430 } catch (Exception e
) {
431 // TODO Auto-generated catch block
439 Find a module's package's id
441 @param id input module id
445 public PackageIdentification
getPackageIdByModuleId(Identification id
) {
447 Vector
<String
> modulePaths
= new Vector
<String
>();
448 Identification mid
= null;
449 String packagePath
= null;
450 String modulePath
= null;
453 // For each package, get all modules list
455 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
456 packagePath
= vPackageList
.elementAt(indexI
).getPath();
457 modulePaths
= this.getAllModulesOfPackage(packagePath
);
458 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
459 modulePath
= modulePaths
.get(indexJ
);
461 mid
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
465 if (mid
.equals(id
)) {
466 return vPackageList
.elementAt(indexI
);
468 } catch (IOException e
) {
470 } catch (XmlException e
) {
472 } catch (Exception e
) {
481 public Identification
getId(String path
, ModuleSurfaceArea msa
) {
482 MsaHeader head
= msa
.getMsaHeader();
483 String name
= head
.getModuleName();
484 String guid
= head
.getGuidValue();
485 String version
= head
.getVersion();
486 Identification id
= new Identification(name
, guid
, version
, path
);
490 public Identification
getId(String path
, PackageSurfaceArea spd
) {
491 SpdHeader head
= spd
.getSpdHeader();
492 String name
= head
.getPackageName();
493 String guid
= head
.getGuidValue();
494 String version
= head
.getVersion();
495 Identification id
= new Identification(name
, guid
, version
, path
);
499 public Identification
getId(String path
, PlatformSurfaceArea fpd
) {
500 PlatformHeader head
= fpd
.getPlatformHeader();
501 String name
= head
.getPlatformName();
502 String guid
= head
.getGuidValue();
503 String version
= head
.getVersion();
504 Identification id
= new Identification(name
, guid
, version
, path
);
509 Add module information to package surface area
517 public void addModuleToPackage(ModuleIdentification mid
, PackageSurfaceArea spd
) throws IOException
, XmlException
,
519 String packagePath
= mid
.getPackageId().getPath();
520 String modulePath
= mid
.getPath();
522 spd
= OpenFile
.openSpdFile(packagePath
);
524 MsaFiles msaFile
= spd
.getMsaFiles();
525 if (msaFile
== null) {
526 msaFile
= MsaFiles
.Factory
.newInstance();
528 packagePath
= packagePath
.substring(0, packagePath
.lastIndexOf(DataType
.FILE_SEPARATOR
));
529 String fn
= Tools
.getRelativePath(modulePath
, packagePath
);
530 msaFile
.addNewFilename();
531 msaFile
.setFilenameArray(msaFile
.getFilenameList().size() - 1, fn
);
532 spd
.setMsaFiles(msaFile
);
533 SaveFile
.saveSpdFile(mid
.getPackageId().getPath(), spd
);
537 Add input package into database
543 public void addPackageToDatabase(PackageIdentification id
) throws Exception
{
544 String path
= id
.getPath();
545 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
546 this.openFrameworkDb();
547 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
548 filename
.setStringValue(path
);
549 fdb
.getPackageList().addNewFilename();
550 fdb
.getPackageList().setFilenameArray(fdb
.getPackageList().sizeOfFilenameArray() - 1, filename
);
551 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
552 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
553 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);
557 Add input package into database
563 public void addPlatformToDatabase(PlatformIdentification id
) throws Exception
{
564 String path
= id
.getPath();
565 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
566 this.openFrameworkDb();
567 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
568 filename
.setStringValue(path
);
569 fdb
.getPlatformList().addNewFilename();
570 fdb
.getPlatformList().setFilenameArray(fdb
.getPlatformList().sizeOfFilenameArray() - 1, filename
);
571 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
572 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
573 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);
577 Get all file's path from one module
586 public Vector
<String
> getAllModuleFiles(String path
) throws IOException
, XmlException
, Exception
{
587 Vector
<String
> v
= new Vector
<String
>();
588 path
= Tools
.convertPathToCurrentOsType(path
);
590 ModuleSurfaceArea msa
= OpenFile
.openMsaFile(path
);
593 // Get all files' path of a module
595 SourceFiles sf
= msa
.getSourceFiles();
597 for (int index
= 0; index
< sf
.getFilenameList().size(); index
++) {
598 String temp
= sf
.getFilenameList().get(index
).getStringValue();
599 temp
= Tools
.addFileSeparator(Tools
.getFilePathOnly(path
)) + temp
;
600 v
.addElement(Tools
.convertPathToCurrentOsType(temp
));
609 Get all file's path from one package
618 public Vector
<String
> getAllPakcageFiles(String path
) throws IOException
, XmlException
, Exception
{
619 Vector
<String
> v
= new Vector
<String
>();
620 path
= Tools
.convertPathToCurrentOsType(path
);
627 // Add module's files one by one
629 Vector
<String
> f1
= new Vector
<String
>();
630 f1
= getAllModulesOfPackage(path
);
631 for (int indexI
= 0; indexI
< f1
.size(); indexI
++) {
632 Vector
<String
> f2
= getAllModuleFiles(f1
.get(indexI
));
633 for (int indexJ
= 0; indexJ
< f2
.size(); indexJ
++) {
634 v
.addElement(f2
.get(indexJ
));