3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.migration
;
17 import java
.util
.regex
.*;
19 import org
.apache
.xmlbeans
.XmlObject
;
20 import org
.apache
.xmlbeans
.XmlObject
.Factory
;
21 import org
.tianocore
.DbPathAndFilename
;
22 import org
.tianocore
.FrameworkDatabaseDocument
;
23 import org
.tianocore
.FrameworkDatabaseDocument
.FrameworkDatabase
;
24 import org
.tianocore
.GuidDeclarationsDocument
.GuidDeclarations
;
25 import org
.tianocore
.PpiDeclarationsDocument
.PpiDeclarations
;
26 import org
.tianocore
.ProtocolDeclarationsDocument
.ProtocolDeclarations
;
28 import org
.tianocore
.PackageListDocument
.PackageList
;
29 import org
.tianocore
.PackageSurfaceAreaDocument
;
30 import org
.tianocore
.PackageSurfaceAreaDocument
.PackageSurfaceArea
;
32 public final class Database
{
33 private static final Database INSTANCE
= Database
.init();
35 Database(String path
) {
39 // collectWorkSpaceDatabase();
40 importPkgGuid("PkgGuid.csv");
41 importDBLib("Library.csv");
42 importDBGuid("Guid.csv", "Guid");
43 importDBGuid("Ppi.csv", "Ppi");
44 importDBGuid("Protocol.csv", "Protocol");
45 importDBMacro("Macro.csv");
47 } catch (Exception e
) {
48 System
.out
.println(e
.getMessage());
52 public String DatabasePath
;
53 public Set
<String
> error
= new HashSet
<String
>();
54 public Set
<String
> r8only
= new HashSet
<String
>();
56 private Map
<String
,Guid
> hashguid
= new HashMap
<String
,Guid
>();
57 private Map
<String
,Func
> hashfunc
= new HashMap
<String
,Func
>();
58 private Map
<String
,Macro
> hashmacro
= new HashMap
<String
,Macro
>();
59 private Map
<String
,String
> hashPkgGuid
= new HashMap
<String
,String
>();
62 //-------------------------------------import------------------------------------------//
63 private void importPkgGuid(String filename
) throws Exception
{
64 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
69 System
.out
.println("Found " + filename
+ ", Importing Package Guid Database.");
71 // Skip the title row.
74 while ((line
= rd
.readLine()) != null) {
75 if (line
.length() != 0) {
76 linecontext
= line
.split(",");
77 hashPkgGuid
.put(linecontext
[0], linecontext
[1]);
82 public Iterator
<String
> dumpAllPkgGuid() {
83 return hashPkgGuid
.values().iterator();
85 private void importDBLib(String filename
) throws Exception
{
86 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
92 System
.out
.println("Found " + filename
+ ", Importing Library Database.");
93 while ((line
= rd
.readLine()) != null) {
94 if (line
.length() != 0) {
95 linecontext
= line
.split(",");
96 lf
= new Func(linecontext
);
97 hashfunc
.put(lf
.r8funcname
,lf
);
103 private void importDBGuid(String filename
, String type
) throws Exception
{
104 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
106 String
[] linecontext
;
110 System
.out
.println("Found " + filename
+ ", Importing " + type
+ " Database.");
111 while ((line
= rd
.readLine()) != null) {
112 if (line
.length() != 0) {
113 linecontext
= line
.split(",");
114 gu
= new Guid(linecontext
, type
);
115 hashguid
.put(gu
.r8name
,gu
);
121 private void importDBMacro(String filename
) throws Exception
{
122 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
124 String
[] linecontext
;
128 System
.out
.println("Found " + filename
+ ", Importing Macro Database.");
129 while ((line
= rd
.readLine()) != null) {
130 if (line
.length() != 0) {
131 linecontext
= line
.split(",");
132 mc
= new Macro(linecontext
);
133 hashmacro
.put(mc
.r8name
,mc
);
139 private void importListR8Only() throws Exception
{
140 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern
.DOTALL
);
141 String wholeline
= Common
.file2string(DatabasePath
+ File
.separator
+ "R8Lib.c");
142 System
.out
.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");
143 Matcher mtrr8only
= ptnr8only
.matcher(wholeline
);
144 while (mtrr8only
.find()) {
145 r8only
.add(mtrr8only
.group(2));
149 //-------------------------------------import------------------------------------------//
151 //-------------------------------------get------------------------------------------//
153 public String
getR9Lib(String r8funcname
) {
155 if (hashfunc
.containsKey(r8funcname
)) {
156 temp
= hashfunc
.get(r8funcname
).r9libname
;
161 public String
getR9Func(String r8funcname
) {
163 if (hashfunc
.containsKey(r8funcname
)) {
164 temp
= hashfunc
.get(r8funcname
).r9funcname
;
169 public String
getR9Macro(String r8macro
) {
170 return hashmacro
.get(r8macro
).r9name
; // the verification job of if the macro exists in the database is done when registering it
173 public String
getR9Guidname(String r8Guid
) {
176 temp
= hashguid
.get(r8Guid
).r9name
;
177 } catch (NullPointerException e
) {
178 error
.add("getR9Guidname :" + r8Guid
);
183 public String
getGuidType(String r8Guid
) {
186 temp
= hashguid
.get(r8Guid
).type
;
187 } catch (NullPointerException e
) {
188 error
.add("getR9Guidname :" + r8Guid
);
193 //-------------------------------------get------------------------------------------//
195 //-------------------------------------has------------------------------------------//
197 public boolean hasFunc(String r8lib
) {
198 return hashfunc
.containsKey(r8lib
);
201 public boolean hasGuid(String r8guid
) {
202 return hashguid
.containsKey(r8guid
);
205 public boolean hasMacro(String r8macro
) {
206 return hashmacro
.containsKey(r8macro
);
209 //-------------------------------------has------------------------------------------//
211 //-------------------------------------init------------------------------------------//
213 private static final Database
init() {
214 if (System
.getenv("WORKSPACE") == null) {
215 return new Database("C:" + File
.separator
+ "tianocore" + File
.separator
+ "edk2" + File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ "Migration");
217 return new Database(System
.getenv("WORKSPACE") + File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ "Migration");
221 public static final Database
getInstance() {
225 private HashMap
<String
, String
> hashDatabaseGuids
= new HashMap
<String
, String
> ();
226 private HashMap
<String
, String
> hashDatabasePpis
= new HashMap
<String
, String
> ();
227 private HashMap
<String
, String
> hashDatabaseProtocols
= new HashMap
<String
, String
> ();
229 private final void collectGuidDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
231 Iterator
<GuidDeclarations
.Entry
> itGuids
;
233 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
235 itGuids
= spdDatabase
.getGuidDeclarations().getEntryList().iterator();
236 while (itGuids
.hasNext()) {
237 hashDatabaseGuids
.put(itGuids
.next().getCName(), pkgGuid
);
241 private final void collectPpiDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
243 Iterator
<PpiDeclarations
.Entry
> itPpis
;
245 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
247 itPpis
= spdDatabase
.getPpiDeclarations().getEntryList().iterator();
248 while (itPpis
.hasNext()) {
249 hashDatabasePpis
.put(itPpis
.next().getCName(), pkgGuid
);
253 private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
255 Iterator
<ProtocolDeclarations
.Entry
> itProtocols
;
257 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
259 itProtocols
= spdDatabase
.getProtocolDeclarations().getEntryList().iterator();
260 while (itProtocols
.hasNext()) {
261 hashDatabaseGuids
.put(itProtocols
.next().getCName(), pkgGuid
);
265 private final void collectPackageDatabase(String packageFileName
) throws Exception
{
266 XmlObject xmlPackage
;
267 PackageSurfaceArea spdDatabase
;
269 xmlPackage
= XmlObject
.Factory
.parse(new File(packageFileName
));
270 spdDatabase
= ((PackageSurfaceAreaDocument
) xmlPackage
).getPackageSurfaceArea();
272 collectGuidDatabase(spdDatabase
);
273 collectProtocolDatabase(spdDatabase
);
274 collectPpiDatabase(spdDatabase
);
278 public final void collectWorkSpaceDatabase() throws Exception
{
279 String workspacePath
;
280 String databaseFileName
;
282 XmlObject xmlDatabase
;
283 FrameworkDatabase frameworkDatabase
;
284 Iterator
<DbPathAndFilename
> packageFile
;
286 workspacePath
= System
.getenv("WORKSPACE");
288 if (workspacePath
== null) {
289 String errorMessage
= "Envivornment variable \"WORKSPACE\" is not set!";
290 throw new Exception(errorMessage
);
292 databaseFileName
= workspacePath
+ File
.separator
+
293 "Tools" + File
.separator
+
294 "Conf" + File
.separator
+
295 "FrameworkDatabase.db";
296 System
.out
.println("Open " + databaseFileName
);
297 databaseFile
= new File(databaseFileName
);
298 xmlDatabase
= XmlObject
.Factory
.parse(databaseFile
);
299 frameworkDatabase
= ((FrameworkDatabaseDocument
) xmlDatabase
).getFrameworkDatabase();
300 packageFile
= frameworkDatabase
.getPackageList().getFilenameList().iterator();
302 while (packageFile
.hasNext()) {
303 String packageFileName
= packageFile
.next().getStringValue();
304 packageFileName
= workspacePath
+ File
.separator
+ packageFileName
;
305 packageFileName
= packageFileName
.replace("/", File
.separator
);
307 System
.out
.println("Parse: " + packageFileName
);
309 collectPackageDatabase(packageFileName
);
310 } catch (Exception e
) {
311 System
.out
.println("Error occured when opening " + packageFileName
+ e
.getMessage());