add MigrationTool
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Database.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
eee63a7b 17import java.util.regex.*;\r
0dc8c589 18\r
7bcb8d17 19public final class Database {\r
20 Database(String path) throws Exception {\r
21 DatabasePath = path;\r
0dc8c589 22 \r
23 importDBLib("Library.csv");\r
24 importDBGuid("Guid.csv", "Guid");\r
25 importDBGuid("Ppi.csv", "Ppi");\r
26 importDBGuid("Protocol.csv", "Protocol");\r
27 importDBMacro("Macro.csv");\r
eee63a7b 28 importListR8Only();\r
0dc8c589 29 }\r
30 \r
0dc8c589 31 public String DatabasePath;\r
32 public Set<String> error = new HashSet<String>();\r
eee63a7b 33 public Set<String> r8only = new HashSet<String>();\r
0dc8c589 34 \r
35 private Map<String,Guid> hashguid = new HashMap<String,Guid>();\r
36 private Map<String,Func> hashfunc = new HashMap<String,Func>();\r
37 private Map<String,Macro> hashmacro = new HashMap<String,Macro>();\r
38 \r
eee63a7b 39 //-------------------------------------import------------------------------------------//\r
40 \r
0dc8c589 41 private void importDBLib(String filename) throws Exception {\r
42 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r
43 String line;\r
44 String[] linecontext;\r
45 Func lf;\r
46 \r
47 if (rd.ready()) {\r
719cebfe 48 System.out.println("Found " + filename + ", Importing Library Database.");\r
0dc8c589 49 while ((line = rd.readLine()) != null) {\r
50 if (line.length() != 0) {\r
51 linecontext = line.split(",");\r
52 lf = new Func(linecontext);\r
53 hashfunc.put(lf.r8funcname,lf);\r
54 }\r
55 }\r
56 }\r
57 }\r
58 \r
59 private void importDBGuid(String filename, String type) throws Exception {\r
60 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r
61 String line;\r
62 String[] linecontext;\r
63 Guid gu;\r
64 \r
65 if (rd.ready()) {\r
719cebfe 66 System.out.println("Found " + filename + ", Importing " + type + " Database.");\r
0dc8c589 67 while ((line = rd.readLine()) != null) {\r
68 if (line.length() != 0) {\r
69 linecontext = line.split(",");\r
70 gu = new Guid(linecontext, type);\r
71 hashguid.put(gu.r8name,gu);\r
72 }\r
73 }\r
74 }\r
75 }\r
76 \r
77 private void importDBMacro(String filename) throws Exception {\r
78 BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r
79 String line;\r
80 String[] linecontext;\r
81 Macro mc;\r
82 \r
83 if (rd.ready()) {\r
719cebfe 84 System.out.println("Found " + filename + ", Importing Macro Database.");\r
0dc8c589 85 while ((line = rd.readLine()) != null) {\r
86 if (line.length() != 0) {\r
87 linecontext = line.split(",");\r
88 mc = new Macro(linecontext);\r
89 hashmacro.put(mc.r8name,mc);\r
90 }\r
91 }\r
92 }\r
93 }\r
eee63a7b 94\r
95 private void importListR8Only() throws Exception {\r
96 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);\r
97 String wholeline = Common.file2string(DatabasePath + File.separator + "R8Lib.c");\r
98 System.out.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");\r
99 Matcher mtrr8only = ptnr8only.matcher(wholeline);\r
100 while (mtrr8only.find()) {\r
101 r8only.add(mtrr8only.group(2));\r
102 }\r
103 }\r
0dc8c589 104 \r
eee63a7b 105 //-------------------------------------import------------------------------------------//\r
106\r
107 //-------------------------------------get------------------------------------------//\r
108\r
0dc8c589 109 public String getR9Lib(String r8funcname) {\r
110 String temp = null;\r
111 if (hashfunc.containsKey(r8funcname)) {\r
112 temp = hashfunc.get(r8funcname).r9libname;\r
113 }\r
114 return temp;\r
115 }\r
116 \r
117 public String getR9Func(String r8funcname) {\r
118 String temp = null;\r
119 if (hashfunc.containsKey(r8funcname)) {\r
120 temp = hashfunc.get(r8funcname).r9funcname;\r
121 }\r
122 return temp;\r
123 }\r
124 \r
0dc8c589 125 public String getR9Macro(String r8macro) {\r
126 return hashmacro.get(r8macro).r9name; // the verification job of if the macro exists in the database is done when registering it\r
127 }\r
7bcb8d17 128\r
0dc8c589 129 public String getR9Guidname(String r8Guid) {\r
130 String temp = null;\r
131 try {\r
132 temp = hashguid.get(r8Guid).r9name;\r
133 } catch (NullPointerException e) {\r
134 error.add("getR9Guidname :" + r8Guid);\r
135 }\r
136 return temp;\r
137 }\r
7bcb8d17 138\r
0dc8c589 139 public String getGuidType(String r8Guid) {\r
140 String temp = null;\r
141 try {\r
142 temp = hashguid.get(r8Guid).type;\r
143 } catch (NullPointerException e) {\r
144 error.add("getR9Guidname :" + r8Guid);\r
145 }\r
146 return temp;\r
147 }\r
7bcb8d17 148\r
eee63a7b 149 //-------------------------------------get------------------------------------------//\r
150\r
151 //-------------------------------------has------------------------------------------//\r
152\r
153 public boolean hasFunc(String r8lib) {\r
154 return hashfunc.containsKey(r8lib);\r
155 }\r
156\r
157 public boolean hasGuid(String r8guid) {\r
158 return hashguid.containsKey(r8guid);\r
159 }\r
160\r
161 public boolean hasMacro(String r8macro) {\r
162 return hashmacro.containsKey(r8macro);\r
163 }\r
164 \r
165 //-------------------------------------has------------------------------------------//\r
166 \r
167 //-------------------------------------init------------------------------------------//\r
168 \r
7bcb8d17 169 public static Database init() throws Exception {\r
170 if (System.getenv("WORKSPACE") == null) {\r
171 return new Database("C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r
172 } else {\r
173 return new Database(System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r
174 }\r
175 }\r
0dc8c589 176}\r