Add Package-Guid Map.
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / Database.java
1 /** @file
2
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
8
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.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.*;
16 import java.util.*;
17 import java.util.regex.*;
18
19 public final class Database {
20 private static final Database INSTANCE = Database.init();
21
22 Database(String path) {
23 DatabasePath = path;
24
25 try {
26 importPkgGuid("PkgGuid.csv");
27 importDBLib("Library.csv");
28 importDBGuid("Guid.csv", "Guid");
29 importDBGuid("Ppi.csv", "Ppi");
30 importDBGuid("Protocol.csv", "Protocol");
31 importDBMacro("Macro.csv");
32 importListR8Only();
33 } catch (Exception e) {
34 System.out.println(e.getMessage());
35 }
36 }
37
38 public String DatabasePath;
39 public Set<String> error = new HashSet<String>();
40 public Set<String> r8only = new HashSet<String>();
41
42 private Map<String,Guid> hashguid = new HashMap<String,Guid>();
43 private Map<String,Func> hashfunc = new HashMap<String,Func>();
44 private Map<String,Macro> hashmacro = new HashMap<String,Macro>();
45 private Map<String,String> hashPkgGuid = new HashMap<String,String>();
46
47 //-------------------------------------import------------------------------------------//
48 private void importPkgGuid(String filename) throws Exception {
49 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
50 String line;
51 String[] linecontext;
52 Func lf;
53
54 if (rd.ready()) {
55 System.out.println("Found " + filename + ", Importing Package Guid Database.");
56 //
57 // Skip the title row.
58 //
59 line = rd.readLine();
60 while ((line = rd.readLine()) != null) {
61 if (line.length() != 0) {
62 linecontext = line.split(",");
63 hashPkgGuid.put(linecontext[0], linecontext[1]);
64 }
65 }
66 }
67 }
68 public Iterator<String> dumpAllPkgGuid() {
69 return hashPkgGuid.values().iterator();
70 }
71 private void importDBLib(String filename) throws Exception {
72 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
73 String line;
74 String[] linecontext;
75 Func lf;
76
77 if (rd.ready()) {
78 System.out.println("Found " + filename + ", Importing Library Database.");
79 while ((line = rd.readLine()) != null) {
80 if (line.length() != 0) {
81 linecontext = line.split(",");
82 lf = new Func(linecontext);
83 hashfunc.put(lf.r8funcname,lf);
84 }
85 }
86 }
87 }
88
89 private void importDBGuid(String filename, String type) throws Exception {
90 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
91 String line;
92 String[] linecontext;
93 Guid gu;
94
95 if (rd.ready()) {
96 System.out.println("Found " + filename + ", Importing " + type + " Database.");
97 while ((line = rd.readLine()) != null) {
98 if (line.length() != 0) {
99 linecontext = line.split(",");
100 gu = new Guid(linecontext, type);
101 hashguid.put(gu.r8name,gu);
102 }
103 }
104 }
105 }
106
107 private void importDBMacro(String filename) throws Exception {
108 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
109 String line;
110 String[] linecontext;
111 Macro mc;
112
113 if (rd.ready()) {
114 System.out.println("Found " + filename + ", Importing Macro Database.");
115 while ((line = rd.readLine()) != null) {
116 if (line.length() != 0) {
117 linecontext = line.split(",");
118 mc = new Macro(linecontext);
119 hashmacro.put(mc.r8name,mc);
120 }
121 }
122 }
123 }
124
125 private void importListR8Only() throws Exception {
126 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);
127 String wholeline = Common.file2string(DatabasePath + File.separator + "R8Lib.c");
128 System.out.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");
129 Matcher mtrr8only = ptnr8only.matcher(wholeline);
130 while (mtrr8only.find()) {
131 r8only.add(mtrr8only.group(2));
132 }
133 }
134
135 //-------------------------------------import------------------------------------------//
136
137 //-------------------------------------get------------------------------------------//
138
139 public String getR9Lib(String r8funcname) {
140 String temp = null;
141 if (hashfunc.containsKey(r8funcname)) {
142 temp = hashfunc.get(r8funcname).r9libname;
143 }
144 return temp;
145 }
146
147 public String getR9Func(String r8funcname) {
148 String temp = null;
149 if (hashfunc.containsKey(r8funcname)) {
150 temp = hashfunc.get(r8funcname).r9funcname;
151 }
152 return temp;
153 }
154
155 public String getR9Macro(String r8macro) {
156 return hashmacro.get(r8macro).r9name; // the verification job of if the macro exists in the database is done when registering it
157 }
158
159 public String getR9Guidname(String r8Guid) {
160 String temp = null;
161 try {
162 temp = hashguid.get(r8Guid).r9name;
163 } catch (NullPointerException e) {
164 error.add("getR9Guidname :" + r8Guid);
165 }
166 return temp;
167 }
168
169 public String getGuidType(String r8Guid) {
170 String temp = null;
171 try {
172 temp = hashguid.get(r8Guid).type;
173 } catch (NullPointerException e) {
174 error.add("getR9Guidname :" + r8Guid);
175 }
176 return temp;
177 }
178
179 //-------------------------------------get------------------------------------------//
180
181 //-------------------------------------has------------------------------------------//
182
183 public boolean hasFunc(String r8lib) {
184 return hashfunc.containsKey(r8lib);
185 }
186
187 public boolean hasGuid(String r8guid) {
188 return hashguid.containsKey(r8guid);
189 }
190
191 public boolean hasMacro(String r8macro) {
192 return hashmacro.containsKey(r8macro);
193 }
194
195 //-------------------------------------has------------------------------------------//
196
197 //-------------------------------------init------------------------------------------//
198
199 private static final Database init() {
200 if (System.getenv("WORKSPACE") == null) {
201 return new Database("C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");
202 } else {
203 return new Database(System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");
204 }
205 }
206
207 public static final Database getInstance() {
208 return INSTANCE;
209 }
210 }