Changed to format of the Package and Platform list outputs.
[mirror_edk2.git] / Tools / Java / Source / DbTools / src / org / tianocore / DbTools / UpdateDb.java
CommitLineData
4de92764 1// @file\r
2// This Class processes multiple MSA files and merges them into a single, \r
3// merged MSA file. It will optionally add the merged MSA file into a package.\r
4//\r
5//\r
6// Copyright (c) 2006, Intel Corporation All rights reserved.\r
7//\r
8// This program and the accompanying materials are licensed and made\r
9// available under the terms and conditions of the BSD License which\r
10// accompanies this distribution. The full text of the license may \r
11// be found at http://opensource.org/licenses/bsd-license.php\r
12//\r
13// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15//\r
16//\r
17\r
18package org.tianocore.DbTools;\r
19\r
20import java.io.*;\r
21import java.util.*;\r
22// import java.sql.Time;\r
23\r
24// import java.lang.*;\r
25// import java.lang.ExceptionInInitializerError;\r
26\r
27// import org.apache.xmlbeans.*;\r
28import org.apache.xmlbeans.XmlCursor;\r
29// import org.apache.xmlbeans.XmlObject;\r
30import org.apache.xmlbeans.XmlOptions;\r
31// import org.apache.xmlbeans.XmlException;\r
32import org.tianocore.*;\r
33\r
34// import org.tianocore.*;\r
35// FrameworkDatabase.db Schema Elements\r
36import org.tianocore.FrameworkDatabaseDocument.*;\r
37import org.tianocore.FdbHeaderDocument.*;\r
38import org.tianocore.PackageListDocument.*;\r
39import org.tianocore.PlatformListDocument.*;\r
40import org.tianocore.FarListDocument.*;\r
41\r
42// FAR Schema Elements\r
43// import org.tianocore.FrameworkArchiveManifestDocument.*;\r
44// import org.tianocore.FarHeaderDocument.*;\r
45// import org.tianocore.FarPackageDocument.*;\r
46// import org.tianocore.FarPlatformDocument.*;\r
47\r
48// FPD Schema Elements\r
49import org.tianocore.PlatformSurfaceAreaDocument.*;\r
50import org.tianocore.PlatformHeaderDocument.*;\r
51\r
52// import org.tianocore.FrameworkModulesDocument.*;\r
53// import org.tianocore.DynamicPcdBuildDefinitionsDocument.*;\r
54\r
55// SPD Schema Elements\r
56// import org.tianocore.PackageSurfaceAreaDocument.*;\r
57// import org.tianocore.SpdHeaderDocument.*;\r
58// import org.tianocore.LibraryClassDeclarationsDocument.*;\r
59// import org.tianocore.GuidDeclarationsDocument.*;\r
60// import org.tianocore.ProtocolDeclarationsDocument.*;\r
61// import org.tianocore.PpiDeclarationsDocument.*;\r
62// import org.tianocore.PcdDeclarationsDocument.*;\r
63\r
64// MSA Schema Elements\r
65// import org.tianocore.ModuleSurfaceAreaDocument.*;\r
66// import org.tianocore.MsaHeaderDocument.*;\r
67// import org.tianocore.LicenseDocument.*;\r
68// import org.tianocore.LibraryClassDefinitionsDocument.*;\r
69// import org.tianocore.PackageDependenciesDocument.*;\r
70// import org.tianocore.ProtocolsDocument.*;\r
71// import org.tianocore.PPIsDocument.*;\r
72// import org.tianocore.PcdCodedDocument.*;\r
73\r
74public class UpdateDb {\r
75\r
76 private final int DEBUG = 0;\r
77\r
78 private final int PASS = 0;\r
79\r
80 private final int FAIL = 1;\r
81\r
82 private int dCtr = 0;\r
83\r
84 private int lCtr = 0;\r
85\r
86 // Future implementations\r
87 // private XmlCursor cursor = null;\r
88\r
89 // FrameworkDatabase Component Elements\r
90 // private FrameworkDatabaseDocument fdbDoc = null;\r
91\r
92 // private FrameworkDatabase fdbInstance = null;\r
93\r
94 // private FdbHeader fdbHeader = null;\r
95\r
96 // private PackageList fdbPackageList = null;\r
97\r
98 // private ArrayList<String> packageList = new ArrayList<String>();\r
99\r
100 // private PlatformList fdbPlatformList = null;\r
101\r
102 // private ArrayList<String> platformList = new ArrayList<String>();\r
103\r
104 // private FarList fdbFarList = null;\r
105\r
106 // private ArrayList<String> farList = new ArrayList<String>();\r
107\r
108 private ArrayList<String> spdList = new ArrayList<String>();\r
109\r
110 private ArrayList<String> fpdList = new ArrayList<String>();\r
111\r
112 // Platform Components\r
113 // private PlatformSurfaceAreaDocument fpdDoc = null;\r
114\r
115 // private PlatformSurfaceArea fpd = null;\r
116\r
117 // private PlatformHeader fpdHeader = null;\r
118\r
119 private boolean wasModified = false;\r
120\r
121 private int VERBOSE = 0;\r
122\r
123 // The combineMsaFiles routine is the primary routine for creating a \r
124 // Merged MSA file.\r
125\r
126 public UpdateDb() {\r
127 init();\r
128 }\r
129\r
130 private void init() {\r
131\r
132 }\r
133\r
134 public int getAllEntries(String dbFilename, int VERBOSE_FLAG) {\r
135 System.out.println("Function not yet implemented!");\r
136 return FAIL;\r
137 }\r
138\r
139 public int getCsvEntries(String dbFilename, int VERBOSE_FLAG) {\r
140 System.out.println("Function not yet implemented!");\r
141 return FAIL;\r
142 }\r
143\r
144 public int fixDatabase(String workspace, int VERBOSE_FLAG, boolean INTERACTIVE, boolean TEST, boolean QUIET) {\r
145 int result = PASS;\r
146 String dbFile = workspace + File.separator + "Tools" + File.separator + "Conf" + File.separator\r
147 + "FrameworkDatabase.db";\r
148 String dbBak = dbFile + ".bak";\r
149\r
150 try {\r
151 // Make a backup file for frameworkDatabase\r
152 if (VERBOSE_FLAG > 3)\r
153 System.out.println("Creating Backup file: " + dbBak);\r
154 copyFile(dbFile, dbBak);\r
155 } catch (IOException e) {\r
156 System.out.println("Error: Cannot make backup file " + dbBak);\r
157 return FAIL;\r
158 }\r
159\r
160 // Datebase update for SPD and FPD files\r
d565d6cd 161 result = fixDB(workspace, dbFile, VERBOSE_FLAG, INTERACTIVE, TEST, QUIET);\r
4de92764 162 if ((result == 0) && (TEST) && (QUIET == false))\r
163 System.out.println("Workspace is consistent with current database!");\r
164 return PASS;\r
165 }\r
166\r
167 public int addItem(String dbFilename, String item, int VERBOSE_FLAG, boolean INTERACTIVE) {\r
168 System.out.println("Function not yet implemented!");\r
169 return FAIL;\r
170 }\r
171\r
172 public int delItem(String workspace, String item, int VERBOSE_FLAG, boolean INTERACTIVE) {\r
173 System.out.println("Function not yet implemented!");\r
174 return FAIL;\r
175 }\r
176\r
177 public int findItem(String workspace, int VERBOSE_FLAG, String findWhat, String item) {\r
178 System.out.println("Function not yet implemented!");\r
179 return FAIL;\r
180 }\r
181\r
182 public int findFars(String workspace, int VERBOSE_FLAG) {\r
183 System.out.println("Function not yet implemented!");\r
184 return FAIL;\r
185 }\r
186\r
187 public int findSpds(String workspace, int VERBOSE_FLAG) {\r
d565d6cd 188 System.out.println("The following Package (SPD) files are in the workspace" + workspace); \r
4de92764 189 File wsDir = new File(workspace);\r
190 findSPDFiles(wsDir, workspace, VERBOSE);\r
191 for (int i = 0; i < spdList.size(); i++) {\r
d565d6cd 192 String Filename = workspace + File.separator + spdList.get(i).trim();\r
193 File spdFilename = new File(Filename);\r
194 try {\r
195 FileReader fileReader = new FileReader(spdFilename);\r
196 BufferedReader reader = new BufferedReader(fileReader);\r
197 String PackageName = null;\r
198 String rLine = null;\r
199 while ((rLine = reader.readLine()) != null) {\r
200 if (rLine.contains("<PackageName>")) {\r
201 PackageName = rLine.replace("<PackageName>", "").trim();\r
202 PackageName = PackageName.replace("</PackageName>", "").trim();\r
203 System.out.printf(" %25s - %s\n",PackageName, spdList.get(i).trim());\r
204 break;\r
205 }\r
206 }\r
207 reader.close();\r
208 } catch (IOException e) {\r
209 System.out.println("ERROR Reading File: " + Filename + e);\r
210 System.exit(FAIL);\r
211 }\r
212 \r
4de92764 213 }\r
d565d6cd 214 return PASS;\r
4de92764 215 }\r
216\r
217 public int findFpds(String workspace, int VERBOSE_FLAG) {\r
d565d6cd 218 System.out.println("The following Platform (FPD) files are in the workspace: " + workspace);\r
4de92764 219 File wsDir = new File(workspace);\r
220 findFPDFiles(wsDir, workspace, VERBOSE);\r
221 for (int i = 0; i < fpdList.size(); i++) {\r
d565d6cd 222 String Filename = workspace + File.separator + fpdList.get(i).trim();\r
223 File fpdFilename = new File(Filename);\r
224 try {\r
225 FileReader fileReader = new FileReader(fpdFilename);\r
226 BufferedReader reader = new BufferedReader(fileReader);\r
227 String PlatformName = null;\r
228 String rLine = null;\r
229 while ((rLine = reader.readLine()) != null) {\r
230 if (rLine.contains("<PlatformName>")) {\r
231 PlatformName = rLine.replace("<PlatformName>", "").trim();\r
232 PlatformName = PlatformName.replace("</PlatformName>", "").trim();\r
233 System.out.printf(" %25s - %s\n",PlatformName, fpdList.get(i).trim());\r
234 break;\r
235 }\r
236 }\r
237 reader.close();\r
238 } catch (IOException e) {\r
239 System.out.println("ERROR Reading File: " + Filename + e);\r
240 System.exit(FAIL);\r
241 }\r
4de92764 242 }\r
d565d6cd 243 return PASS;\r
4de92764 244 }\r
245\r
246 // copy from source file to destination file\r
247 private int copyFile(String src, String dst) throws IOException {\r
248 try {\r
249 File srcFile = new File(src);\r
250 FileReader fileReader = new FileReader(srcFile);\r
251 BufferedReader reader = new BufferedReader(fileReader);\r
252\r
253 File dstFile = new File(dst);\r
254 FileWriter fileWriter = new FileWriter(dstFile);\r
255 BufferedWriter writer = new BufferedWriter(fileWriter);\r
256\r
257 String line = null;\r
258 while ((line = reader.readLine()) != null) {\r
259 writer.write(line);\r
260 writer.newLine();\r
261 }\r
262\r
263 reader.close();\r
264 writer.close();\r
265 } catch (IOException e) {\r
266 System.out.println("I/O Exception during file copy: " + e);\r
267 }\r
268\r
269 return PASS;\r
270 }\r
271\r
d565d6cd 272 private int fixDB(String workspace, String dbFile, int VERBOSE, boolean INTERACTIVE, boolean TEST, boolean QUIET) {\r
4de92764 273 File wsDir = new File(workspace);\r
274 int retValue = PASS;\r
275 // Find all .spd and .fpd files in workspace and put them in spdList and fpdList\r
276 if (VERBOSE > 0)\r
277 System.out.println("SPD File Search ");\r
278 findSPDFiles(wsDir, workspace, VERBOSE);\r
279 dCtr = 0;\r
280 lCtr = 0;\r
281 if (VERBOSE > 0) {\r
282 System.out.println(" ");\r
283 System.out.println("FPD File Search ");\r
284 }\r
285 findFPDFiles(wsDir, workspace, VERBOSE);\r
286 if (VERBOSE > 0)\r
287 System.out.println(" ");\r
288\r
289 try {\r
290 // check database file for possible update\r
d565d6cd 291 retValue = checkDBForUpdate(workspace, dbFile, VERBOSE, INTERACTIVE, TEST, QUIET);\r
4de92764 292 } catch (IOException e) {\r
293 if (QUIET == false)\r
294 System.out.println("Error: Updating " + dbFile + " file.");\r
295 return FAIL;\r
296 }\r
297\r
298 if ((VERBOSE > 0) && (TEST) && (wasModified)) {\r
299 System.out.println("FRAMEWORK Database does NOT match the contents of the WORKSPACE");\r
300 retValue = FAIL;\r
301 }\r
302 if ((VERBOSE > 0) && (wasModified == false) && (QUIET == false))\r
303 System.out.println("FRAMEWORK Database matches the contents of the WORKSPACE");\r
304\r
305 return retValue;\r
306 }\r
307\r
308 private void findSPDFiles(File dir, String workspace, int VERBOSE) {\r
309 String str;\r
310\r
311 if (dir.isDirectory()) {\r
312 dCtr++;\r
313 String[] subdir = dir.list();\r
314 if (dCtr >= 10) {\r
315 if (VERBOSE > 2)\r
316 System.out.print(".");\r
317 dCtr = 0;\r
318 lCtr++;\r
319 }\r
320 if (lCtr > 79) {\r
321 if (VERBOSE > 2)\r
322 System.out.println(" ");\r
323 lCtr = 0;\r
324 dCtr = 0;\r
325 }\r
326\r
327 for (int i = 0; i < subdir.length; i++) {\r
328 findSPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
329 }\r
330 } else {\r
331 if (dir.toString().toLowerCase().endsWith(".spd")) {\r
332 str = dir.getPath().replace('\\', '/');\r
333 workspace = workspace.replace('\\', '/');\r
334 str = strStrip(str, workspace + "/");\r
335 spdList.add(str.toString());\r
336 if (VERBOSE == 2)\r
337 System.out.println(" " + str);\r
338 if (VERBOSE > 2)\r
339 System.out.print("+");\r
340 lCtr++;\r
341 }\r
342 }\r
343\r
344 }\r
345\r
346 private void findFPDFiles(File dir, String workspace, int VERBOSE) {\r
347 String str;\r
348\r
349 if (dir.isDirectory()) {\r
350 String[] subdir = dir.list();\r
351 if (dCtr >= 10) {\r
352 if (VERBOSE > 2)\r
353 System.out.print(".");\r
354 dCtr = 0;\r
355 lCtr++;\r
356 }\r
357 if (lCtr > 79) {\r
358 if (VERBOSE > 2)\r
359 System.out.println(" ");\r
360 lCtr = 0;\r
361 dCtr = 0;\r
362 }\r
363\r
364 for (int i = 0; i < subdir.length; i++) {\r
365 findFPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
366 }\r
367 } else {\r
368 if (dir.toString().toLowerCase().endsWith(".fpd")) {\r
369 str = dir.getPath().replace('\\', '/');\r
370 workspace = workspace.replace('\\', '/');\r
371 str = strStrip(str, workspace + "/");\r
372 fpdList.add(str.toString());\r
373 if (VERBOSE == 2)\r
374 System.out.println(" " + str);\r
375 if (VERBOSE > 2)\r
376 System.out.print("+");\r
377 lCtr++;\r
378 }\r
379 }\r
380 }\r
381\r
d565d6cd 382 private int checkDBForUpdate(String workspace, String dbFileName, int VERBOSE, boolean INTERACTIVE, boolean TEST, boolean QUIET)\r
4de92764 383 throws IOException {\r
384 int SpdFlag = 0;\r
385 int FpdFlag = 0;\r
386 String SpdFile = null;\r
387 String SpdFullFile = null;\r
388 String FpdFile = null;\r
389 String FpdFullFile = null;\r
390\r
391 String tmpDbFile = dbFileName + ".tmp";\r
392 String newSpd = null;\r
393 String newFpd = null;\r
394\r
395 FileReader fileReader = new FileReader(dbFileName);\r
396 BufferedReader reader = new BufferedReader(fileReader);\r
397\r
398 FileWriter fileWriter = new FileWriter(tmpDbFile);\r
399 BufferedWriter writer = new BufferedWriter(fileWriter);\r
400\r
401 String line = null;\r
402 try {\r
403 while ((line = reader.readLine()) != null) {\r
404 if (line.indexOf("Added the following") >= 0) {\r
405 wasModified = true;\r
406 continue;\r
407 }\r
408 //\r
409 // Process for .spd files\r
410 //\r
411 if (line.indexOf("<PackageList") >= 0) {\r
412 SpdFlag = 1;\r
413 } else {\r
414 if (line.indexOf("</PackageList") >= 0) {\r
415 SpdFlag = 2;\r
416 }\r
417 }\r
418 if (SpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
419 SpdFile = strStrip(line, "<Filename>");\r
420 SpdFile = strStrip(SpdFile, "</Filename>");\r
421 SpdFile = SpdFile.trim();\r
422 SpdFullFile = workspace + File.separator + SpdFile;\r
423 if (!(new File(SpdFullFile)).exists()) {\r
424 if (VERBOSE > 0)\r
425 System.out.println("WARNING: Removing SPD file: " + SpdFile\r
426 + "from the DB, as it does not exist!");\r
427 wasModified = true;\r
428 continue;\r
429 }\r
430 // Don't add files that are already in the database\r
431 spdList.remove(SpdFile);\r
432 }\r
433 if (SpdFlag == 2) {\r
434 int cflag = 0;\r
435 for (int i = 0; i < spdList.size(); i++) {\r
436 newSpd = spdList.get(i);\r
437 newSpd = newSpd.trim();\r
438 if (newSpd.length() > 0) {\r
439 if (cflag == 0) {\r
440 Calendar c = Calendar.getInstance();\r
441 if (TEST == false)\r
442 writer.write(" <!-- Adding the following SPD files " + c.getTime() + " -->\n");\r
443 }\r
444 if (VERBOSE > 0)\r
445 System.out.println(" Adding SPD file: " + newSpd);\r
446 if (TEST == false)\r
447 writer.write(" <Filename>" + newSpd + "</Filename>\n");\r
448 cflag++;\r
449 wasModified = true;\r
450 }\r
451 }\r
452 SpdFlag++;\r
453 }\r
454 //\r
455 // Process for .fpd files\r
456 // \r
457 if (line.indexOf("<PlatformList") >= 0) {\r
458 FpdFlag = 1;\r
459 } else {\r
460 if (line.indexOf("</PlatformList") >= 0) {\r
461 FpdFlag = 2;\r
462 }\r
463 }\r
464 if (FpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
465 FpdFile = strStrip(line, "<Filename>");\r
466 FpdFile = strStrip(FpdFile, "</Filename>");\r
467 FpdFile = FpdFile.trim();\r
468 FpdFullFile = workspace + File.separator + FpdFile;\r
469 if (!(new File(FpdFullFile)).exists()) {\r
470 if (VERBOSE > 0)\r
471 System.out.println("WARNING: Removing FPD file: " + FpdFile\r
472 + " from the DB, as it does not exist!");\r
473 wasModified = true;\r
474 continue;\r
475 }\r
476 // Don't add files that are already in the database\r
477 fpdList.remove(FpdFile);\r
478 }\r
479 if (FpdFlag == 2) {\r
480 int cflag = 0;\r
481 for (int i = 0; i < fpdList.size(); i++) {\r
482 newFpd = fpdList.get(i);\r
483 newFpd = newFpd.trim();\r
484 if (newFpd.length() > 0) {\r
485 if (cflag == 0) {\r
486 Calendar c = Calendar.getInstance();\r
487 if (TEST == false)\r
488 writer.write(" <!-- Adding the following FPD files " + c.getTime() + " -->\n");\r
489 }\r
490 if (VERBOSE > 0)\r
491 System.out.println(" Adding FPD file: " + newFpd);\r
492 if (TEST == false)\r
493 writer.write(" <Filename>" + newFpd + "</Filename>\n");\r
494 cflag++;\r
495 wasModified = true;\r
496 }\r
497 }\r
498 FpdFlag++;\r
499 }\r
500\r
501 if (DEBUG > 2) {\r
502 System.out.println(line);\r
503 }\r
504 if (TEST == false) {\r
505 writer.write(line);\r
506 writer.newLine();\r
507 }\r
508 }\r
509 reader.close();\r
510 writer.close();\r
511\r
512 } catch (IOException e) {\r
513 System.out.println("ERROR I/O Exception occured! " + e);\r
514 System.exit(FAIL);\r
515 }\r
516\r
517 if (wasModified) {\r
518 if ((VERBOSE > 0) && (QUIET == false))\r
519 System.out.println("FrameworkDatabase has been UPDATED for this Workspace!\n");\r
520 if (TEST == false)\r
521 copyFile(tmpDbFile, dbFileName);\r
522 } else {\r
523 if ((VERBOSE > 0) && (QUIET == false))\r
524 System.out.println("FrameworkDatabase correct for this Workspace!\n");\r
525 }\r
526\r
527 if (TEST == false) {\r
528 File tmpFile = new File(tmpDbFile);\r
529 if (tmpFile.exists()) {\r
530 tmpFile.delete();\r
531 }\r
532 }\r
533\r
534 return PASS;\r
535 }\r
536\r
537 static String strStrip(String str, String pattern) {\r
538 int ps = 0;\r
539 int pe = 0;\r
540 StringBuffer result = new StringBuffer();\r
541\r
542 while ((pe = str.indexOf(pattern, ps)) >= 0) {\r
543 result.append(str.substring(ps, pe));\r
544 ps = pe + pattern.length();\r
545 }\r
546 result.append(str.substring(ps));\r
547 return result.toString();\r
548 }\r
549\r
550 private String checkDuplicateStrings(String aString, ArrayList<String> aList) {\r
551 // This routine checks a string against an array.\r
552 // If the string is found, it will return an empty string.\r
553 // If the string is not found, it adds the string to the array, and\r
554 // returns the string to the caller.\r
555 for (int lctr = 0; lctr < aList.size(); lctr++) {\r
556 if (DEBUG > 8)\r
557 System.out.println("Comparing: \n" + aString.replace(" ", "").replace("\n", "") + "\nTo: \n"\r
558 + aList.get(lctr).replace(" ", "").replace("\n", "").toString().trim());\r
559 if (aString.replace(" ", "").replace("\n", "").contains(\r
560 aList.get(lctr).replace(" ", "").replace("\n", "")\r
561 .toString().trim())) {\r
562 if ((DEBUG > 3) || (VERBOSE > 3))\r
563 System.out.println("Found a duplicate String, skipping!");\r
564 return "";\r
565 }\r
566 }\r
567 if ((DEBUG > 3) || (VERBOSE > 3))\r
568 System.out.println("Returning UNIQUE String!\n " + aString);\r
569 aList.add(aString);\r
570 return aString;\r
571 }\r
572\r
573 private static class XmlConfig {\r
574 public static XmlCursor setupXmlCursor(XmlCursor cursor) {\r
575 String uri = "http://www.TianoCore.org/2006/Edk2.0";\r
576 cursor.push();\r
577 cursor.toNextToken();\r
578 cursor.insertNamespace("", uri);\r
579 cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");\r
580 cursor.pop();\r
581 return cursor;\r
582\r
583 }\r
584\r
585 public static XmlOptions setupXmlOptions() {\r
586 XmlOptions options = new XmlOptions();\r
587 options.setCharacterEncoding("UTF-8");\r
588 options.setSavePrettyPrint();\r
589 options.setSavePrettyPrintIndent(2);\r
590 return options;\r
591 }\r
592\r
593 }\r
594}\r