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
.SpdHeaderDocument
.SpdHeader
;
30 import org
.tianocore
.frameworkwizard
.common
.DataType
;
31 import org
.tianocore
.frameworkwizard
.common
.Identification
;
32 import org
.tianocore
.frameworkwizard
.common
.Log
;
33 import org
.tianocore
.frameworkwizard
.common
.OpenFile
;
34 import org
.tianocore
.frameworkwizard
.common
.SaveFile
;
35 import org
.tianocore
.frameworkwizard
.common
.Tools
;
36 import org
.tianocore
.frameworkwizard
.module
.Identification
.ModuleIdentification
;
37 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
38 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
40 public class WorkspaceTools
{
42 // Define class members
44 private FrameworkDatabaseDocument
.FrameworkDatabase fdb
= null;
46 private Vector
<ModuleIdentification
> vModuleList
= new Vector
<ModuleIdentification
>();
48 private Vector
<PackageIdentification
> vPackageList
= new Vector
<PackageIdentification
>();
50 private Vector
<PlatformIdentification
> vPlatformList
= new Vector
<PlatformIdentification
>();
54 Open Framework Database file
57 private void openFrameworkDb() {
58 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
59 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
61 fdb
= OpenFile
.openFrameworkDb(strFrameworkDbFilePath
);
62 } catch (XmlException e
) {
63 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, e
.getMessage());
65 } catch (Exception e
) {
66 Log
.err("Open Framework Database " + strFrameworkDbFilePath
, "Invalid file type");
72 Get all modules' paths from one package
74 @return a Vector with all modules' path
77 public Vector
<String
> getAllModulesOfPackage(String path
) {
78 Vector
<String
> modulePath
= new Vector
<String
>();
80 MsaFiles files
= OpenFile
.openSpdFile(path
).getMsaFiles();
82 for (int index
= 0; index
< files
.getFilenameList().size(); index
++) {
83 modulePath
.addElement(files
.getFilenameList().get(index
));
86 } catch (IOException e
) {
88 } catch (XmlException e
) {
90 } catch (Exception e
) {
97 Get all package basic information form the FrameworkDatabase.db file
99 @return vPackageList A vector includes all packages' basic information
102 public Vector
<PackageIdentification
> getAllPackages() {
103 Identification id
= null;
104 vPackageList
.removeAllElements();
108 for (int index
= 0; index
< fdb
.getPackageList().getFilenameList().size(); index
++) {
109 String path
= Workspace
.getCurrentWorkspace() + DataType
.FILE_SEPARATOR
110 + fdb
.getPackageList().getFilenameArray(index
).getStringValue();
111 path
= Tools
.convertPathToCurrentOsType(path
);
113 id
= getId(path
, OpenFile
.openSpdFile(path
));
114 vPackageList
.addElement(new PackageIdentification(id
));
115 } catch (IOException e
) {
116 Log
.err("Open Package Surface Area " + path
, e
.getMessage());
118 } catch (XmlException e
) {
119 Log
.err("Open Package Surface Area " + path
, e
.getMessage());
121 } catch (Exception e
) {
122 Log
.err("Open Package Surface Area " + path
, "Invalid file type");
130 Get all package basic information form the FrameworkDatabase.db file
132 @return vPackageList A vector includes all packages' basic information
135 public Vector
<ModuleIdentification
> getAllModules() {
136 vModuleList
.removeAllElements();
138 // First get all packages
141 Vector
<String
> modulePaths
= new Vector
<String
>();
142 Identification id
= null;
143 String packagePath
= null;
144 String modulePath
= null;
147 // For each package, get all modules list
149 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
150 packagePath
= vPackageList
.elementAt(indexI
).getPath();
151 modulePaths
= this.getAllModulesOfPackage(packagePath
);
152 packagePath
= packagePath
.substring(0, packagePath
.lastIndexOf(DataType
.FILE_SEPARATOR
)
153 + DataType
.FILE_SEPARATOR
.length());
154 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
155 modulePath
= Tools
.convertPathToCurrentOsType(packagePath
+ modulePaths
.elementAt(indexJ
));
157 id
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
158 vModuleList
.addElement(new ModuleIdentification(id
, vPackageList
.elementAt(indexI
)));
159 } catch (IOException e
) {
160 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
162 } catch (XmlException e
) {
163 Log
.err("Open Module Surface Area " + modulePath
, e
.getMessage());
165 } catch (Exception e
) {
166 Log
.err("Open Module Surface Area " + modulePath
, "Invalid file type");
176 Get all platform basic information form the FrameworkDatabase.db file
178 @return vplatformList A vector includes all platforms' basic information
181 public Vector
<PlatformIdentification
> getAllPlatforms() {
182 Identification id
= null;
183 vPlatformList
.removeAllElements();
187 for (int index
= 0; index
< fdb
.getPlatformList().getFilenameList().size(); index
++) {
188 String path
= Workspace
.getCurrentWorkspace() + DataType
.FILE_SEPARATOR
189 + fdb
.getPlatformList().getFilenameArray(index
).getStringValue();
190 path
= Tools
.convertPathToCurrentOsType(path
);
192 id
= getId(path
, OpenFile
.openFpdFile(path
));
193 vPlatformList
.addElement(new PlatformIdentification(id
));
194 } catch (IOException e
) {
195 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
197 } catch (XmlException e
) {
198 Log
.err("Open Platform Surface Area " + path
, e
.getMessage());
200 } catch (Exception e
) {
201 Log
.err("Open Platform Surface Area " + path
, "Invalid file type");
205 return vPlatformList
;
209 Get all Library Class Definitions from a package
213 public Vector
<String
> getAllLibraryClassDefinitionsFromPackage(PackageSurfaceArea spd
) {
214 Vector
<String
> vector
= new Vector
<String
>();
215 if (spd
.getLibraryClassDeclarations() != null) {
216 if (spd
.getLibraryClassDeclarations().getLibraryClassList().size() > 0) {
217 for (int index
= 0; index
< spd
.getLibraryClassDeclarations().getLibraryClassList().size(); index
++) {
218 vector
.addElement(spd
.getLibraryClassDeclarations().getLibraryClassList().get(index
).getName());
226 Get all Protocol Definitions from a package
230 public Vector
<String
> getAllProtocolDeclarationsFromPackage(PackageSurfaceArea spd
) {
231 Vector
<String
> vector
= new Vector
<String
>();
232 if (spd
.getProtocolDeclarations() != null) {
233 if (spd
.getProtocolDeclarations().getEntryList().size() > 0) {
234 for (int index
= 0; index
< spd
.getProtocolDeclarations().getEntryList().size(); index
++) {
235 vector
.addElement(spd
.getProtocolDeclarations().getEntryList().get(index
).getCName());
243 Get all Ppi Definitions from a package
247 public Vector
<String
> getAllPpiDeclarationsFromPackage(PackageSurfaceArea spd
) {
248 Vector
<String
> vector
= new Vector
<String
>();
249 if (spd
.getPpiDeclarations() != null) {
250 if (spd
.getPpiDeclarations().getEntryList().size() > 0) {
251 for (int index
= 0; index
< spd
.getPpiDeclarations().getEntryList().size(); index
++) {
252 vector
.addElement(spd
.getPpiDeclarations().getEntryList().get(index
).getCName());
260 Get all Guid Definitions from a package
264 public Vector
<String
> getAllGuidDeclarationsFromPackage(PackageSurfaceArea spd
) {
265 Vector
<String
> vector
= new Vector
<String
>();
266 if (spd
.getGuidDeclarations() != null) {
267 if (spd
.getGuidDeclarations().getEntryList().size() > 0) {
268 for (int index
= 0; index
< spd
.getGuidDeclarations().getEntryList().size(); index
++) {
269 vector
.addElement(spd
.getGuidDeclarations().getEntryList().get(index
).getCName());
277 Get all Pcd Definitions from a package
281 public Vector
<String
> getAllPcdDeclarationsFromPackage(PackageSurfaceArea spd
) {
282 Vector
<String
> vector
= new Vector
<String
>();
283 if (spd
.getPcdDeclarations() != null) {
284 if (spd
.getPcdDeclarations().getPcdEntryList().size() > 0) {
285 for (int index
= 0; index
< spd
.getPcdDeclarations().getPcdEntryList().size(); index
++) {
286 vector
.addElement(spd
.getPcdDeclarations().getPcdEntryList().get(index
).getCName());
293 public Vector
<String
> getAllLibraryClassDefinitionsFromWorkspace() {
295 // First get all packages
297 this.getAllPackages();
299 Vector
<String
> vector
= new Vector
<String
>();
300 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
302 Vector
<String
> v
= getAllLibraryClassDefinitionsFromPackage(OpenFile
303 .openSpdFile(vPackageList
306 if (v
!= null && v
.size() > 0) {
309 } catch (IOException e
) {
310 // TODO Auto-generated catch block
312 } catch (XmlException e
) {
313 // TODO Auto-generated catch block
315 } catch (Exception e
) {
316 // TODO Auto-generated catch block
323 public Vector
<String
> getAllProtocolDeclarationsFromWorkspace() {
325 // First get all packages
327 this.getAllPackages();
329 Vector
<String
> vector
= new Vector
<String
>();
330 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
332 Vector
<String
> v
= getAllProtocolDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
334 if (v
!= null && v
.size() > 0) {
337 } catch (IOException e
) {
338 // TODO Auto-generated catch block
340 } catch (XmlException e
) {
341 // TODO Auto-generated catch block
343 } catch (Exception e
) {
344 // TODO Auto-generated catch block
351 public Vector
<String
> getAllPpiDeclarationsFromWorkspace() {
353 // First get all packages
355 this.getAllPackages();
357 Vector
<String
> vector
= new Vector
<String
>();
358 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
360 Vector
<String
> v
= getAllPpiDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
362 if (v
!= null && v
.size() > 0) {
365 } catch (IOException e
) {
366 // TODO Auto-generated catch block
368 } catch (XmlException e
) {
369 // TODO Auto-generated catch block
371 } catch (Exception e
) {
372 // TODO Auto-generated catch block
379 public Vector
<String
> getAllGuidDeclarationsFromWorkspace() {
381 // First get all packages
383 this.getAllPackages();
385 Vector
<String
> vector
= new Vector
<String
>();
386 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
388 Vector
<String
> v
= getAllGuidDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
390 if (v
!= null && v
.size() > 0) {
393 } catch (IOException e
) {
394 // TODO Auto-generated catch block
396 } catch (XmlException e
) {
397 // TODO Auto-generated catch block
399 } catch (Exception e
) {
400 // TODO Auto-generated catch block
407 public Vector
<String
> getAllPcdDeclarationsFromWorkspace() {
409 // First get all packages
411 this.getAllPackages();
413 Vector
<String
> vector
= new Vector
<String
>();
414 for (int index
= 0; index
< this.vPackageList
.size(); index
++) {
416 Vector
<String
> v
= getAllPcdDeclarationsFromPackage(OpenFile
.openSpdFile(vPackageList
.get(index
)
418 if (v
!= null && v
.size() > 0) {
421 } catch (IOException e
) {
422 // TODO Auto-generated catch block
424 } catch (XmlException e
) {
425 // TODO Auto-generated catch block
427 } catch (Exception e
) {
428 // TODO Auto-generated catch block
436 Find a module's package's id
438 @param id input module id
442 public PackageIdentification
getPackageIdByModuleId(Identification id
) {
444 Vector
<String
> modulePaths
= new Vector
<String
>();
445 Identification mid
= null;
446 String packagePath
= null;
447 String modulePath
= null;
450 // For each package, get all modules list
452 for (int indexI
= 0; indexI
< vPackageList
.size(); indexI
++) {
453 packagePath
= vPackageList
.elementAt(indexI
).getPath();
454 modulePaths
= this.getAllModulesOfPackage(packagePath
);
455 packagePath
= packagePath
.substring(0, packagePath
.lastIndexOf(DataType
.FILE_SEPARATOR
)
456 + DataType
.FILE_SEPARATOR
.length());
457 for (int indexJ
= 0; indexJ
< modulePaths
.size(); indexJ
++) {
458 modulePath
= Tools
.convertPathToCurrentOsType(packagePath
+ modulePaths
.elementAt(indexJ
));
460 mid
= getId(modulePath
, OpenFile
.openMsaFile(modulePath
));
464 if (mid
.equals(id
)) {
465 return vPackageList
.elementAt(indexI
);
467 } catch (IOException e
) {
469 } catch (XmlException e
) {
471 } catch (Exception e
) {
480 public Identification
getId(String path
, ModuleSurfaceArea msa
) {
481 MsaHeader head
= msa
.getMsaHeader();
482 String name
= head
.getModuleName();
483 String guid
= head
.getGuidValue();
484 String version
= head
.getVersion();
485 Identification id
= new Identification(name
, guid
, version
, path
);
489 public Identification
getId(String path
, PackageSurfaceArea spd
) {
490 SpdHeader head
= spd
.getSpdHeader();
491 String name
= head
.getPackageName();
492 String guid
= head
.getGuidValue();
493 String version
= head
.getVersion();
494 Identification id
= new Identification(name
, guid
, version
, path
);
498 public Identification
getId(String path
, PlatformSurfaceArea fpd
) {
499 PlatformHeader head
= fpd
.getPlatformHeader();
500 String name
= head
.getPlatformName();
501 String guid
= head
.getGuidValue();
502 String version
= head
.getVersion();
503 Identification id
= new Identification(name
, guid
, version
, path
);
508 Add module information to package surface area
516 public void addModuleToPackage(ModuleIdentification mid
, PackageSurfaceArea spd
) throws IOException
, XmlException
,
518 String packagePath
= mid
.getPackageId().getPath();
519 String modulePath
= mid
.getPath();
521 spd
= OpenFile
.openSpdFile(packagePath
);
523 MsaFiles msaFile
= spd
.getMsaFiles();
524 if (msaFile
== null) {
525 msaFile
= MsaFiles
.Factory
.newInstance();
527 packagePath
= packagePath
.substring(0, packagePath
.lastIndexOf(DataType
.FILE_SEPARATOR
));
528 String fn
= Tools
.getRelativePath(modulePath
, packagePath
);
529 msaFile
.addNewFilename();
530 msaFile
.setFilenameArray(msaFile
.getFilenameList().size() - 1, fn
);
531 spd
.setMsaFiles(msaFile
);
532 SaveFile
.saveSpdFile(mid
.getPackageId().getPath(), spd
);
536 Add input package into database
542 public void addPackageToDatabase(PackageIdentification id
) throws Exception
{
543 String path
= id
.getPath();
544 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
545 this.openFrameworkDb();
546 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
547 filename
.setStringValue(path
);
548 fdb
.getPackageList().addNewFilename();
549 fdb
.getPackageList().setFilenameArray(fdb
.getPackageList().sizeOfFilenameArray() - 1, filename
);
550 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
551 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
552 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);
556 Add input package into database
562 public void addPlatformToDatabase(PlatformIdentification id
) throws Exception
{
563 String path
= id
.getPath();
564 path
= Tools
.getRelativePath(path
, Workspace
.getCurrentWorkspace());
565 this.openFrameworkDb();
566 DbPathAndFilename filename
= DbPathAndFilename
.Factory
.newInstance();
567 filename
.setStringValue(path
);
568 fdb
.getPlatformList().addNewFilename();
569 fdb
.getPlatformList().setFilenameArray(fdb
.getPlatformList().sizeOfFilenameArray() - 1, filename
);
570 String strFrameworkDbFilePath
= Workspace
.getCurrentWorkspace() + Workspace
.getStrWorkspaceDatabaseFile();
571 strFrameworkDbFilePath
= Tools
.convertPathToCurrentOsType(strFrameworkDbFilePath
);
572 SaveFile
.saveDbFile(strFrameworkDbFilePath
, fdb
);