]>
Commit | Line | Data |
---|---|---|
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 | 13 | package org.tianocore.migration;\r |
14 | \r | |
15 | import java.io.*;\r | |
16 | import java.util.*;\r | |
eee63a7b | 17 | import java.util.regex.*;\r |
0dc8c589 | 18 | \r |
e83a1d9a | 19 | import org.apache.xmlbeans.XmlObject;\r |
20 | import org.apache.xmlbeans.XmlObject.Factory;\r | |
21 | import org.tianocore.DbPathAndFilename;\r | |
22 | import org.tianocore.FrameworkDatabaseDocument;\r | |
23 | import org.tianocore.FrameworkDatabaseDocument.FrameworkDatabase;\r | |
24 | import org.tianocore.GuidDeclarationsDocument.GuidDeclarations;\r | |
25 | import org.tianocore.PpiDeclarationsDocument.PpiDeclarations;\r | |
26 | import org.tianocore.ProtocolDeclarationsDocument.ProtocolDeclarations;\r | |
27 | \r | |
28 | import org.tianocore.PackageListDocument.PackageList;\r | |
29 | import org.tianocore.PackageSurfaceAreaDocument;\r | |
30 | import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;\r | |
31 | \r | |
7bcb8d17 | 32 | public final class Database {\r |
27e0221a | 33 | private static final Database INSTANCE = Database.init();\r |
34 | \r | |
35 | Database(String path) {\r | |
36 | DatabasePath = path;\r | |
4f60c26f | 37 | \r |
27e0221a | 38 | try {\r |
e83a1d9a | 39 | // collectWorkSpaceDatabase();\r |
9c0e70cb | 40 | importPkgGuid("PkgGuid.csv");\r |
27e0221a | 41 | importDBLib("Library.csv");\r |
42 | importDBGuid("Guid.csv", "Guid");\r | |
43 | importDBGuid("Ppi.csv", "Ppi");\r | |
44 | importDBGuid("Protocol.csv", "Protocol");\r | |
45 | importDBMacro("Macro.csv");\r | |
46 | importListR8Only();\r | |
47 | } catch (Exception e) {\r | |
48 | System.out.println(e.getMessage());\r | |
49 | }\r | |
50 | }\r | |
51 | \r | |
52 | public String DatabasePath;\r | |
53 | public Set<String> error = new HashSet<String>();\r | |
54 | public Set<String> r8only = new HashSet<String>();\r | |
55 | \r | |
56 | private Map<String,Guid> hashguid = new HashMap<String,Guid>();\r | |
57 | private Map<String,Func> hashfunc = new HashMap<String,Func>();\r | |
58 | private Map<String,Macro> hashmacro = new HashMap<String,Macro>();\r | |
9c0e70cb | 59 | private Map<String,String> hashPkgGuid = new HashMap<String,String>();\r |
e83a1d9a | 60 | \r |
27e0221a | 61 | \r |
62 | //-------------------------------------import------------------------------------------//\r | |
9c0e70cb | 63 | private void importPkgGuid(String filename) throws Exception {\r |
64 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r | |
65 | String line;\r | |
66 | String[] linecontext;\r | |
9c0e70cb | 67 | \r |
68 | if (rd.ready()) {\r | |
69 | System.out.println("Found " + filename + ", Importing Package Guid Database.");\r | |
70 | //\r | |
71 | // Skip the title row.\r | |
72 | // \r | |
73 | line = rd.readLine();\r | |
74 | while ((line = rd.readLine()) != null) {\r | |
75 | if (line.length() != 0) {\r | |
76 | linecontext = line.split(",");\r | |
77 | hashPkgGuid.put(linecontext[0], linecontext[1]);\r | |
78 | }\r | |
79 | }\r | |
80 | }\r | |
81 | }\r | |
82 | public Iterator<String> dumpAllPkgGuid() {\r | |
83 | return hashPkgGuid.values().iterator();\r | |
84 | }\r | |
27e0221a | 85 | private void importDBLib(String filename) throws Exception {\r |
86 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r | |
87 | String line;\r | |
88 | String[] linecontext;\r | |
89 | Func lf;\r | |
90 | \r | |
91 | if (rd.ready()) {\r | |
92 | System.out.println("Found " + filename + ", Importing Library Database.");\r | |
93 | while ((line = rd.readLine()) != null) {\r | |
94 | if (line.length() != 0) {\r | |
95 | linecontext = line.split(",");\r | |
96 | lf = new Func(linecontext);\r | |
97 | hashfunc.put(lf.r8funcname,lf);\r | |
98 | }\r | |
99 | }\r | |
100 | }\r | |
101 | }\r | |
102 | \r | |
103 | private void importDBGuid(String filename, String type) throws Exception {\r | |
104 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r | |
105 | String line;\r | |
106 | String[] linecontext;\r | |
107 | Guid gu;\r | |
108 | \r | |
109 | if (rd.ready()) {\r | |
110 | System.out.println("Found " + filename + ", Importing " + type + " Database.");\r | |
111 | while ((line = rd.readLine()) != null) {\r | |
112 | if (line.length() != 0) {\r | |
113 | linecontext = line.split(",");\r | |
114 | gu = new Guid(linecontext, type);\r | |
115 | hashguid.put(gu.r8name,gu);\r | |
116 | }\r | |
117 | }\r | |
118 | }\r | |
119 | }\r | |
120 | \r | |
121 | private void importDBMacro(String filename) throws Exception {\r | |
122 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));\r | |
123 | String line;\r | |
124 | String[] linecontext;\r | |
125 | Macro mc;\r | |
126 | \r | |
127 | if (rd.ready()) {\r | |
128 | System.out.println("Found " + filename + ", Importing Macro Database.");\r | |
129 | while ((line = rd.readLine()) != null) {\r | |
130 | if (line.length() != 0) {\r | |
131 | linecontext = line.split(",");\r | |
132 | mc = new Macro(linecontext);\r | |
133 | hashmacro.put(mc.r8name,mc);\r | |
134 | }\r | |
135 | }\r | |
136 | }\r | |
137 | }\r | |
eee63a7b | 138 | \r |
27e0221a | 139 | private void importListR8Only() throws Exception {\r |
140 | Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);\r | |
141 | String wholeline = Common.file2string(DatabasePath + File.separator + "R8Lib.c");\r | |
142 | System.out.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");\r | |
143 | Matcher mtrr8only = ptnr8only.matcher(wholeline);\r | |
144 | while (mtrr8only.find()) {\r | |
145 | r8only.add(mtrr8only.group(2));\r | |
146 | }\r | |
147 | }\r | |
148 | \r | |
149 | //-------------------------------------import------------------------------------------//\r | |
eee63a7b | 150 | \r |
27e0221a | 151 | //-------------------------------------get------------------------------------------//\r |
eee63a7b | 152 | \r |
27e0221a | 153 | public String getR9Lib(String r8funcname) {\r |
154 | String temp = null;\r | |
155 | if (hashfunc.containsKey(r8funcname)) {\r | |
156 | temp = hashfunc.get(r8funcname).r9libname;\r | |
157 | }\r | |
158 | return temp;\r | |
159 | }\r | |
160 | \r | |
161 | public String getR9Func(String r8funcname) {\r | |
162 | String temp = null;\r | |
163 | if (hashfunc.containsKey(r8funcname)) {\r | |
164 | temp = hashfunc.get(r8funcname).r9funcname;\r | |
165 | }\r | |
166 | return temp;\r | |
167 | }\r | |
168 | \r | |
169 | public String getR9Macro(String r8macro) {\r | |
170 | return hashmacro.get(r8macro).r9name; // the verification job of if the macro exists in the database is done when registering it\r | |
171 | }\r | |
7bcb8d17 | 172 | \r |
27e0221a | 173 | public String getR9Guidname(String r8Guid) {\r |
174 | String temp = null;\r | |
175 | try {\r | |
176 | temp = hashguid.get(r8Guid).r9name;\r | |
177 | } catch (NullPointerException e) {\r | |
178 | error.add("getR9Guidname :" + r8Guid);\r | |
179 | }\r | |
180 | return temp;\r | |
181 | }\r | |
7bcb8d17 | 182 | \r |
27e0221a | 183 | public String getGuidType(String r8Guid) {\r |
184 | String temp = null;\r | |
185 | try {\r | |
186 | temp = hashguid.get(r8Guid).type;\r | |
187 | } catch (NullPointerException e) {\r | |
188 | error.add("getR9Guidname :" + r8Guid);\r | |
189 | }\r | |
190 | return temp;\r | |
191 | }\r | |
7bcb8d17 | 192 | \r |
27e0221a | 193 | //-------------------------------------get------------------------------------------//\r |
eee63a7b | 194 | \r |
27e0221a | 195 | //-------------------------------------has------------------------------------------//\r |
eee63a7b | 196 | \r |
27e0221a | 197 | public boolean hasFunc(String r8lib) {\r |
198 | return hashfunc.containsKey(r8lib);\r | |
199 | }\r | |
eee63a7b | 200 | \r |
27e0221a | 201 | public boolean hasGuid(String r8guid) {\r |
202 | return hashguid.containsKey(r8guid);\r | |
203 | }\r | |
eee63a7b | 204 | \r |
27e0221a | 205 | public boolean hasMacro(String r8macro) {\r |
206 | return hashmacro.containsKey(r8macro);\r | |
207 | }\r | |
208 | \r | |
209 | //-------------------------------------has------------------------------------------//\r | |
210 | \r | |
211 | //-------------------------------------init------------------------------------------//\r | |
4f60c26f | 212 | \r |
27e0221a | 213 | private static final Database init() {\r |
214 | if (System.getenv("WORKSPACE") == null) {\r | |
215 | return new Database("C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r | |
216 | } else {\r | |
217 | return new Database(System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration");\r | |
218 | }\r | |
219 | }\r | |
220 | \r | |
221 | public static final Database getInstance() {\r | |
222 | return INSTANCE;\r | |
223 | }\r | |
e83a1d9a | 224 | \r |
225 | private HashMap<String, String> hashDatabaseGuids = new HashMap<String, String> ();\r | |
226 | private HashMap<String, String> hashDatabasePpis = new HashMap<String, String> ();\r | |
227 | private HashMap<String, String> hashDatabaseProtocols = new HashMap<String, String> ();\r | |
228 | \r | |
229 | private final void collectGuidDatabase(PackageSurfaceArea spdDatabase) throws Exception {\r | |
230 | String pkgGuid;\r | |
231 | Iterator<GuidDeclarations.Entry> itGuids;\r | |
232 | \r | |
233 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
234 | \r | |
235 | itGuids = spdDatabase.getGuidDeclarations().getEntryList().iterator();\r | |
236 | while (itGuids.hasNext()) {\r | |
237 | hashDatabaseGuids.put(itGuids.next().getCName(), pkgGuid); \r | |
238 | }\r | |
239 | }\r | |
240 | \r | |
241 | private final void collectPpiDatabase(PackageSurfaceArea spdDatabase) throws Exception {\r | |
242 | String pkgGuid;\r | |
243 | Iterator<PpiDeclarations.Entry> itPpis;\r | |
244 | \r | |
245 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
246 | \r | |
247 | itPpis = spdDatabase.getPpiDeclarations().getEntryList().iterator();\r | |
248 | while (itPpis.hasNext()) {\r | |
249 | hashDatabasePpis.put(itPpis.next().getCName(), pkgGuid); \r | |
250 | }\r | |
251 | }\r | |
252 | \r | |
253 | private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase) throws Exception {\r | |
254 | String pkgGuid;\r | |
255 | Iterator<ProtocolDeclarations.Entry> itProtocols;\r | |
256 | \r | |
257 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
258 | \r | |
259 | itProtocols = spdDatabase.getProtocolDeclarations().getEntryList().iterator();\r | |
260 | while (itProtocols.hasNext()) {\r | |
261 | hashDatabaseGuids.put(itProtocols.next().getCName(), pkgGuid); \r | |
262 | }\r | |
263 | }\r | |
264 | \r | |
265 | private final void collectPackageDatabase(String packageFileName) throws Exception {\r | |
266 | XmlObject xmlPackage;\r | |
267 | PackageSurfaceArea spdDatabase;\r | |
268 | \r | |
269 | xmlPackage = XmlObject.Factory.parse(new File(packageFileName));\r | |
270 | spdDatabase = ((PackageSurfaceAreaDocument) xmlPackage).getPackageSurfaceArea();\r | |
271 | \r | |
272 | collectGuidDatabase(spdDatabase);\r | |
273 | collectProtocolDatabase(spdDatabase);\r | |
274 | collectPpiDatabase(spdDatabase);\r | |
275 | \r | |
276 | \r | |
277 | }\r | |
278 | public final void collectWorkSpaceDatabase() throws Exception {\r | |
279 | String workspacePath;\r | |
280 | String databaseFileName;\r | |
281 | File databaseFile;\r | |
282 | XmlObject xmlDatabase;\r | |
283 | FrameworkDatabase frameworkDatabase;\r | |
284 | Iterator<DbPathAndFilename> packageFile;\r | |
285 | \r | |
286 | workspacePath = System.getenv("WORKSPACE");\r | |
287 | \r | |
288 | if (workspacePath == null) {\r | |
289 | String errorMessage = "Envivornment variable \"WORKSPACE\" is not set!";\r | |
290 | throw new Exception(errorMessage);\r | |
291 | }\r | |
292 | databaseFileName = workspacePath + File.separator + \r | |
293 | "Tools" + File.separator +\r | |
294 | "Conf" + File.separator + \r | |
295 | "FrameworkDatabase.db";\r | |
296 | System.out.println("Open " + databaseFileName);\r | |
297 | databaseFile = new File(databaseFileName);\r | |
298 | xmlDatabase = XmlObject.Factory.parse(databaseFile);\r | |
299 | frameworkDatabase = ((FrameworkDatabaseDocument) xmlDatabase).getFrameworkDatabase();\r | |
300 | packageFile = frameworkDatabase.getPackageList().getFilenameList().iterator();\r | |
301 | \r | |
302 | while (packageFile.hasNext()) {\r | |
303 | String packageFileName = packageFile.next().getStringValue();\r | |
304 | packageFileName = workspacePath + File.separator + packageFileName;\r | |
305 | packageFileName = packageFileName.replace("/", File.separator);\r | |
306 | \r | |
307 | System.out.println("Parse: " + packageFileName);\r | |
308 | try {\r | |
309 | collectPackageDatabase(packageFileName);\r | |
310 | } catch (Exception e) {\r | |
311 | System.out.println("Error occured when opening " + packageFileName + e.getMessage());\r | |
312 | }\r | |
313 | }\r | |
314 | return;\r | |
315 | }\r | |
316 | \r | |
0dc8c589 | 317 | }\r |