]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/workspace/WorkspaceTools.java
1. Restructure some folders and files
[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.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;
40
41 public class WorkspaceTools {
42 //
43 // Define class members
44 //
45 private FrameworkDatabaseDocument.FrameworkDatabase fdb = null;
46
47 private Vector<ModuleIdentification> vModuleList = new Vector<ModuleIdentification>();
48
49 private Vector<PackageIdentification> vPackageList = new Vector<PackageIdentification>();
50
51 private Vector<PlatformIdentification> vPlatformList = new Vector<PlatformIdentification>();
52
53 /**
54
55 Open Framework Database file
56
57 */
58 private void openFrameworkDb() {
59 String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();
60 strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);
61 try {
62 fdb = OpenFile.openFrameworkDb(strFrameworkDbFilePath);
63 } catch (XmlException e) {
64 Log.err("Open Framework Database " + strFrameworkDbFilePath, e.getMessage());
65 return;
66 } catch (Exception e) {
67 Log.err("Open Framework Database " + strFrameworkDbFilePath, "Invalid file type");
68 return;
69 }
70 }
71
72 /**
73 Get all modules' paths from one package
74
75 @return a Vector with all modules' path
76
77 **/
78 public Vector<String> getAllModulesOfPackage(String path) {
79 Vector<String> modulePath = new Vector<String>();
80 try {
81 MsaFiles files = OpenFile.openSpdFile(path).getMsaFiles();
82 if (files != null) {
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);
88 }
89 }
90 } catch (IOException e) {
91 e.printStackTrace();
92 } catch (XmlException e) {
93 e.printStackTrace();
94 } catch (Exception e) {
95 e.printStackTrace();
96 }
97 return modulePath;
98 }
99
100 /**
101 Get all package basic information form the FrameworkDatabase.db file
102
103 @return vPackageList A vector includes all packages' basic information
104
105 */
106 public Vector<PackageIdentification> getAllPackages() {
107 Identification id = null;
108 vPackageList.removeAllElements();
109
110 openFrameworkDb();
111
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);
116 try {
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());
121 e.printStackTrace();
122 } catch (XmlException e) {
123 Log.err("Open Package Surface Area " + path, e.getMessage());
124 e.printStackTrace();
125 } catch (Exception e) {
126 Log.err("Open Package Surface Area " + path, "Invalid file type");
127 e.printStackTrace();
128 }
129 }
130 return vPackageList;
131 }
132
133 /**
134 Get all package basic information form the FrameworkDatabase.db file
135
136 @return vPackageList A vector includes all packages' basic information
137
138 */
139 public Vector<ModuleIdentification> getAllModules() {
140 vModuleList.removeAllElements();
141 //
142 // First get all packages
143 //
144 getAllPackages();
145 Vector<String> modulePaths = new Vector<String>();
146 Identification id = null;
147 String packagePath = null;
148 String modulePath = null;
149
150 //
151 // For each package, get all modules list
152 //
153 for (int indexI = 0; indexI < vPackageList.size(); indexI++) {
154 packagePath = vPackageList.elementAt(indexI).getPath();
155 modulePaths = this.getAllModulesOfPackage(packagePath);
156
157 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {
158 try {
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());
164 e.printStackTrace();
165 } catch (XmlException e) {
166 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
167 e.printStackTrace();
168 } catch (Exception e) {
169 Log.err("Open Module Surface Area " + modulePath, "Invalid file type");
170 e.printStackTrace();
171 }
172 }
173 }
174
175 return vModuleList;
176 }
177
178 /**
179 Get all platform basic information form the FrameworkDatabase.db file
180
181 @return vplatformList A vector includes all platforms' basic information
182
183 */
184 public Vector<PlatformIdentification> getAllPlatforms() {
185 Identification id = null;
186 vPlatformList.removeAllElements();
187
188 openFrameworkDb();
189
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);
194 try {
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());
199 e.printStackTrace();
200 } catch (XmlException e) {
201 Log.err("Open Platform Surface Area " + path, e.getMessage());
202 e.printStackTrace();
203 } catch (Exception e) {
204 Log.err("Open Platform Surface Area " + path, "Invalid file type");
205 e.printStackTrace();
206 }
207 }
208 return vPlatformList;
209 }
210
211 /**
212 Get all Library Class Definitions from a package
213
214 @return Vector
215 **/
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());
222 }
223 }
224 }
225 return vector;
226 }
227
228 /**
229 Get all Protocol Definitions from a package
230
231 @return Vector
232 **/
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());
239 }
240 }
241 }
242 return vector;
243 }
244
245 /**
246 Get all Ppi Definitions from a package
247
248 @return Vector
249 **/
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());
256 }
257 }
258 }
259 return vector;
260 }
261
262 /**
263 Get all Guid Definitions from a package
264
265 @return Vector
266 **/
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());
273 }
274 }
275 }
276 return vector;
277 }
278
279 /**
280 Get all Pcd Definitions from a package
281
282 @return Vector
283 **/
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());
290 }
291 }
292 }
293 return vector;
294 }
295
296 public Vector<String> getAllLibraryClassDefinitionsFromWorkspace() {
297 //
298 // First get all packages
299 //
300 this.getAllPackages();
301
302 Vector<String> vector = new Vector<String>();
303 for (int index = 0; index < this.vPackageList.size(); index++) {
304 try {
305 Vector<String> v = getAllLibraryClassDefinitionsFromPackage(OpenFile
306 .openSpdFile(vPackageList
307 .get(index)
308 .getPath()));
309 if (v != null && v.size() > 0) {
310 vector.addAll(v);
311 }
312 } catch (IOException e) {
313 // TODO Auto-generated catch block
314 e.printStackTrace();
315 } catch (XmlException e) {
316 // TODO Auto-generated catch block
317 e.printStackTrace();
318 } catch (Exception e) {
319 // TODO Auto-generated catch block
320 e.printStackTrace();
321 }
322 }
323 return vector;
324 }
325
326 public Vector<String> getAllProtocolDeclarationsFromWorkspace() {
327 //
328 // First get all packages
329 //
330 this.getAllPackages();
331
332 Vector<String> vector = new Vector<String>();
333 for (int index = 0; index < this.vPackageList.size(); index++) {
334 try {
335 Vector<String> v = getAllProtocolDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
336 .getPath()));
337 if (v != null && v.size() > 0) {
338 vector.addAll(v);
339 }
340 } catch (IOException e) {
341 // TODO Auto-generated catch block
342 e.printStackTrace();
343 } catch (XmlException e) {
344 // TODO Auto-generated catch block
345 e.printStackTrace();
346 } catch (Exception e) {
347 // TODO Auto-generated catch block
348 e.printStackTrace();
349 }
350 }
351 return vector;
352 }
353
354 public Vector<String> getAllPpiDeclarationsFromWorkspace() {
355 //
356 // First get all packages
357 //
358 this.getAllPackages();
359
360 Vector<String> vector = new Vector<String>();
361 for (int index = 0; index < this.vPackageList.size(); index++) {
362 try {
363 Vector<String> v = getAllPpiDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
364 .getPath()));
365 if (v != null && v.size() > 0) {
366 vector.addAll(v);
367 }
368 } catch (IOException e) {
369 // TODO Auto-generated catch block
370 e.printStackTrace();
371 } catch (XmlException e) {
372 // TODO Auto-generated catch block
373 e.printStackTrace();
374 } catch (Exception e) {
375 // TODO Auto-generated catch block
376 e.printStackTrace();
377 }
378 }
379 return vector;
380 }
381
382 public Vector<String> getAllGuidDeclarationsFromWorkspace() {
383 //
384 // First get all packages
385 //
386 this.getAllPackages();
387
388 Vector<String> vector = new Vector<String>();
389 for (int index = 0; index < this.vPackageList.size(); index++) {
390 try {
391 Vector<String> v = getAllGuidDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
392 .getPath()));
393 if (v != null && v.size() > 0) {
394 vector.addAll(v);
395 }
396 } catch (IOException e) {
397 // TODO Auto-generated catch block
398 e.printStackTrace();
399 } catch (XmlException e) {
400 // TODO Auto-generated catch block
401 e.printStackTrace();
402 } catch (Exception e) {
403 // TODO Auto-generated catch block
404 e.printStackTrace();
405 }
406 }
407 return vector;
408 }
409
410 public Vector<String> getAllPcdDeclarationsFromWorkspace() {
411 //
412 // First get all packages
413 //
414 this.getAllPackages();
415
416 Vector<String> vector = new Vector<String>();
417 for (int index = 0; index < this.vPackageList.size(); index++) {
418 try {
419 Vector<String> v = getAllPcdDeclarationsFromPackage(OpenFile.openSpdFile(vPackageList.get(index)
420 .getPath()));
421 if (v != null && v.size() > 0) {
422 vector.addAll(v);
423 }
424 } catch (IOException e) {
425 // TODO Auto-generated catch block
426 e.printStackTrace();
427 } catch (XmlException e) {
428 // TODO Auto-generated catch block
429 e.printStackTrace();
430 } catch (Exception e) {
431 // TODO Auto-generated catch block
432 e.printStackTrace();
433 }
434 }
435 return vector;
436 }
437
438 /**
439 Find a module's package's id
440
441 @param id input module id
442 @return package id
443
444 **/
445 public PackageIdentification getPackageIdByModuleId(Identification id) {
446 getAllPackages();
447 Vector<String> modulePaths = new Vector<String>();
448 Identification mid = null;
449 String packagePath = null;
450 String modulePath = null;
451
452 //
453 // For each package, get all modules list
454 //
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);
460 try {
461 mid = getId(modulePath, OpenFile.openMsaFile(modulePath));
462 //
463 // Check id
464 //
465 if (mid.equals(id)) {
466 return vPackageList.elementAt(indexI);
467 }
468 } catch (IOException e) {
469 e.printStackTrace();
470 } catch (XmlException e) {
471 e.printStackTrace();
472 } catch (Exception e) {
473 e.printStackTrace();
474 }
475 }
476 }
477
478 return null;
479 }
480
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);
487 return id;
488 }
489
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);
496 return id;
497 }
498
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);
505 return id;
506 }
507
508 /**
509 Add module information to package surface area
510
511 @param mid
512 @throws IOException
513 @throws XmlException
514 @throws Exception
515
516 **/
517 public void addModuleToPackage(ModuleIdentification mid, PackageSurfaceArea spd) throws IOException, XmlException,
518 Exception {
519 String packagePath = mid.getPackageId().getPath();
520 String modulePath = mid.getPath();
521 if (spd == null) {
522 spd = OpenFile.openSpdFile(packagePath);
523 }
524 MsaFiles msaFile = spd.getMsaFiles();
525 if (msaFile == null) {
526 msaFile = MsaFiles.Factory.newInstance();
527 }
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);
534 }
535
536 /**
537 Add input package into database
538
539 @param id
540 * @throws Exception
541
542 **/
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);
554 }
555
556 /**
557 Add input package into database
558
559 @param id
560 * @throws Exception
561
562 **/
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);
574 }
575
576 /**
577 Get all file's path from one module
578
579 @param path
580 @return
581 @throws IOException
582 @throws XmlException
583 @throws Exception
584
585 **/
586 public Vector<String> getAllModuleFiles(String path) throws IOException, XmlException, Exception {
587 Vector<String> v = new Vector<String>();
588 path = Tools.convertPathToCurrentOsType(path);
589 v.addElement(path);
590 ModuleSurfaceArea msa = OpenFile.openMsaFile(path);
591 if (msa != null) {
592 //
593 // Get all files' path of a module
594 //
595 SourceFiles sf = msa.getSourceFiles();
596 if (sf != null) {
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));
601 }
602 }
603 }
604
605 return v;
606 }
607
608 /**
609 Get all file's path from one package
610
611 @param path
612 @return
613 @throws IOException
614 @throws XmlException
615 @throws Exception
616
617 **/
618 public Vector<String> getAllPakcageFiles(String path) throws IOException, XmlException, Exception {
619 Vector<String> v = new Vector<String>();
620 path = Tools.convertPathToCurrentOsType(path);
621 //
622 // First add package
623 //
624 v.addElement(path);
625
626 //
627 // Add module's files one by one
628 //
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));
635 }
636 }
637 //v.add(0, path);
638
639 return v;
640 }
641 }