]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/MigrationTools/org/tianocore/migration/Database.java
686dbe32b4aef3a9bcfa5fea4de018f5eb85aad3
[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 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;
27
28 import org.tianocore.PackageListDocument.PackageList;
29 import org.tianocore.PackageSurfaceAreaDocument;
30 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
31
32 public final class Database {
33 private static final Database INSTANCE = Database.init();
34
35 Database(String path) {
36 DatabasePath = path;
37
38 try {
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");
46 importListR8Only();
47 } catch (Exception e) {
48 System.out.println(e.getMessage());
49 }
50 }
51
52 public String DatabasePath;
53 public Set<String> error = new HashSet<String>();
54 public Set<String> r8only = new HashSet<String>();
55
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>();
60
61
62 //-------------------------------------import------------------------------------------//
63 private void importPkgGuid(String filename) throws Exception {
64 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
65 String line;
66 String[] linecontext;
67
68 if (rd.ready()) {
69 System.out.println("Found " + filename + ", Importing Package Guid Database.");
70 //
71 // Skip the title row.
72 //
73 line = rd.readLine();
74 while ((line = rd.readLine()) != null) {
75 if (line.length() != 0) {
76 linecontext = line.split(",");
77 hashPkgGuid.put(linecontext[0], linecontext[1]);
78 }
79 }
80 }
81 }
82 public Iterator<String> dumpAllPkgGuid() {
83 return hashPkgGuid.values().iterator();
84 }
85 private void importDBLib(String filename) throws Exception {
86 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
87 String line;
88 String[] linecontext;
89 Func lf;
90
91 if (rd.ready()) {
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);
98 }
99 }
100 }
101 }
102
103 private void importDBGuid(String filename, String type) throws Exception {
104 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
105 String line;
106 String[] linecontext;
107 Guid gu;
108
109 if (rd.ready()) {
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);
116 }
117 }
118 }
119 }
120
121 private void importDBMacro(String filename) throws Exception {
122 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
123 String line;
124 String[] linecontext;
125 Macro mc;
126
127 if (rd.ready()) {
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);
134 }
135 }
136 }
137 }
138
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));
146 }
147 }
148
149 //-------------------------------------import------------------------------------------//
150
151 //-------------------------------------get------------------------------------------//
152
153 public String getR9Lib(String r8funcname) {
154 String temp = null;
155 if (hashfunc.containsKey(r8funcname)) {
156 temp = hashfunc.get(r8funcname).r9libname;
157 }
158 return temp;
159 }
160
161 public String getR9Func(String r8funcname) {
162 String temp = null;
163 if (hashfunc.containsKey(r8funcname)) {
164 temp = hashfunc.get(r8funcname).r9funcname;
165 }
166 return temp;
167 }
168
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
171 }
172
173 public String getR9Guidname(String r8Guid) {
174 String temp = null;
175 try {
176 temp = hashguid.get(r8Guid).r9name;
177 } catch (NullPointerException e) {
178 error.add("getR9Guidname :" + r8Guid);
179 }
180 return temp;
181 }
182
183 public String getGuidType(String r8Guid) {
184 String temp = null;
185 try {
186 temp = hashguid.get(r8Guid).type;
187 } catch (NullPointerException e) {
188 error.add("getR9Guidname :" + r8Guid);
189 }
190 return temp;
191 }
192
193 //-------------------------------------get------------------------------------------//
194
195 //-------------------------------------has------------------------------------------//
196
197 public boolean hasFunc(String r8lib) {
198 return hashfunc.containsKey(r8lib);
199 }
200
201 public boolean hasGuid(String r8guid) {
202 return hashguid.containsKey(r8guid);
203 }
204
205 public boolean hasMacro(String r8macro) {
206 return hashmacro.containsKey(r8macro);
207 }
208
209 //-------------------------------------has------------------------------------------//
210
211 //-------------------------------------init------------------------------------------//
212
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");
216 } else {
217 return new Database(System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");
218 }
219 }
220
221 public static final Database getInstance() {
222 return INSTANCE;
223 }
224
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> ();
228
229 private final void collectGuidDatabase(PackageSurfaceArea spdDatabase) throws Exception {
230 String pkgGuid;
231 Iterator<GuidDeclarations.Entry> itGuids;
232
233 pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
234
235 itGuids = spdDatabase.getGuidDeclarations().getEntryList().iterator();
236 while (itGuids.hasNext()) {
237 hashDatabaseGuids.put(itGuids.next().getCName(), pkgGuid);
238 }
239 }
240
241 private final void collectPpiDatabase(PackageSurfaceArea spdDatabase) throws Exception {
242 String pkgGuid;
243 Iterator<PpiDeclarations.Entry> itPpis;
244
245 pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
246
247 itPpis = spdDatabase.getPpiDeclarations().getEntryList().iterator();
248 while (itPpis.hasNext()) {
249 hashDatabasePpis.put(itPpis.next().getCName(), pkgGuid);
250 }
251 }
252
253 private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase) throws Exception {
254 String pkgGuid;
255 Iterator<ProtocolDeclarations.Entry> itProtocols;
256
257 pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
258
259 itProtocols = spdDatabase.getProtocolDeclarations().getEntryList().iterator();
260 while (itProtocols.hasNext()) {
261 hashDatabaseGuids.put(itProtocols.next().getCName(), pkgGuid);
262 }
263 }
264
265 private final void collectPackageDatabase(String packageFileName) throws Exception {
266 XmlObject xmlPackage;
267 PackageSurfaceArea spdDatabase;
268
269 xmlPackage = XmlObject.Factory.parse(new File(packageFileName));
270 spdDatabase = ((PackageSurfaceAreaDocument) xmlPackage).getPackageSurfaceArea();
271
272 collectGuidDatabase(spdDatabase);
273 collectProtocolDatabase(spdDatabase);
274 collectPpiDatabase(spdDatabase);
275
276
277 }
278 public final void collectWorkSpaceDatabase() throws Exception {
279 String workspacePath;
280 String databaseFileName;
281 File databaseFile;
282 XmlObject xmlDatabase;
283 FrameworkDatabase frameworkDatabase;
284 Iterator<DbPathAndFilename> packageFile;
285
286 workspacePath = System.getenv("WORKSPACE");
287
288 if (workspacePath == null) {
289 String errorMessage = "Envivornment variable \"WORKSPACE\" is not set!";
290 throw new Exception(errorMessage);
291 }
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();
301
302 while (packageFile.hasNext()) {
303 String packageFileName = packageFile.next().getStringValue();
304 packageFileName = workspacePath + File.separator + packageFileName;
305 packageFileName = packageFileName.replace("/", File.separator);
306
307 System.out.println("Parse: " + packageFileName);
308 try {
309 collectPackageDatabase(packageFileName);
310 } catch (Exception e) {
311 System.out.println("Error occured when opening " + packageFileName + e.getMessage());
312 }
313 }
314 return;
315 }
316
317 }