Added quotes around the classpath option in script files
[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
161 result = fixDB(workspace, dbFile, VERBOSE_FLAG, TEST, QUIET);\r
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
188 System.out.println("The following Package (SPD) files are in the workspace!"); \r
189 File wsDir = new File(workspace);\r
190 findSPDFiles(wsDir, workspace, VERBOSE);\r
191 for (int i = 0; i < spdList.size(); i++) {\r
192 System.out.println(" " + spdList.get(i).trim());\r
193 // TODO: get the PackageName from the file\r
194 }\r
195 return FAIL;\r
196 }\r
197\r
198 public int findFpds(String workspace, int VERBOSE_FLAG) {\r
199 System.out.println("The following Platform (FPD) files are in the workspace!");\r
200 File wsDir = new File(workspace);\r
201 findFPDFiles(wsDir, workspace, VERBOSE);\r
202 for (int i = 0; i < fpdList.size(); i++) {\r
203 System.out.println(" " + fpdList.get(i).trim());\r
204 // TODO: get the PlatformName from the file\r
205 }\r
206 return FAIL;\r
207 }\r
208\r
209 // copy from source file to destination file\r
210 private int copyFile(String src, String dst) throws IOException {\r
211 try {\r
212 File srcFile = new File(src);\r
213 FileReader fileReader = new FileReader(srcFile);\r
214 BufferedReader reader = new BufferedReader(fileReader);\r
215\r
216 File dstFile = new File(dst);\r
217 FileWriter fileWriter = new FileWriter(dstFile);\r
218 BufferedWriter writer = new BufferedWriter(fileWriter);\r
219\r
220 String line = null;\r
221 while ((line = reader.readLine()) != null) {\r
222 writer.write(line);\r
223 writer.newLine();\r
224 }\r
225\r
226 reader.close();\r
227 writer.close();\r
228 } catch (IOException e) {\r
229 System.out.println("I/O Exception during file copy: " + e);\r
230 }\r
231\r
232 return PASS;\r
233 }\r
234\r
235 private int fixDB(String workspace, String dbFile, int VERBOSE, boolean TEST, boolean QUIET) {\r
236 File wsDir = new File(workspace);\r
237 int retValue = PASS;\r
238 // Find all .spd and .fpd files in workspace and put them in spdList and fpdList\r
239 if (VERBOSE > 0)\r
240 System.out.println("SPD File Search ");\r
241 findSPDFiles(wsDir, workspace, VERBOSE);\r
242 dCtr = 0;\r
243 lCtr = 0;\r
244 if (VERBOSE > 0) {\r
245 System.out.println(" ");\r
246 System.out.println("FPD File Search ");\r
247 }\r
248 findFPDFiles(wsDir, workspace, VERBOSE);\r
249 if (VERBOSE > 0)\r
250 System.out.println(" ");\r
251\r
252 try {\r
253 // check database file for possible update\r
254 retValue = checkDBForUpdate(workspace, dbFile, VERBOSE, TEST, QUIET);\r
255 } catch (IOException e) {\r
256 if (QUIET == false)\r
257 System.out.println("Error: Updating " + dbFile + " file.");\r
258 return FAIL;\r
259 }\r
260\r
261 if ((VERBOSE > 0) && (TEST) && (wasModified)) {\r
262 System.out.println("FRAMEWORK Database does NOT match the contents of the WORKSPACE");\r
263 retValue = FAIL;\r
264 }\r
265 if ((VERBOSE > 0) && (wasModified == false) && (QUIET == false))\r
266 System.out.println("FRAMEWORK Database matches the contents of the WORKSPACE");\r
267\r
268 return retValue;\r
269 }\r
270\r
271 private void findSPDFiles(File dir, String workspace, int VERBOSE) {\r
272 String str;\r
273\r
274 if (dir.isDirectory()) {\r
275 dCtr++;\r
276 String[] subdir = dir.list();\r
277 if (dCtr >= 10) {\r
278 if (VERBOSE > 2)\r
279 System.out.print(".");\r
280 dCtr = 0;\r
281 lCtr++;\r
282 }\r
283 if (lCtr > 79) {\r
284 if (VERBOSE > 2)\r
285 System.out.println(" ");\r
286 lCtr = 0;\r
287 dCtr = 0;\r
288 }\r
289\r
290 for (int i = 0; i < subdir.length; i++) {\r
291 findSPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
292 }\r
293 } else {\r
294 if (dir.toString().toLowerCase().endsWith(".spd")) {\r
295 str = dir.getPath().replace('\\', '/');\r
296 workspace = workspace.replace('\\', '/');\r
297 str = strStrip(str, workspace + "/");\r
298 spdList.add(str.toString());\r
299 if (VERBOSE == 2)\r
300 System.out.println(" " + str);\r
301 if (VERBOSE > 2)\r
302 System.out.print("+");\r
303 lCtr++;\r
304 }\r
305 }\r
306\r
307 }\r
308\r
309 private void findFPDFiles(File dir, String workspace, int VERBOSE) {\r
310 String str;\r
311\r
312 if (dir.isDirectory()) {\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 findFPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
329 }\r
330 } else {\r
331 if (dir.toString().toLowerCase().endsWith(".fpd")) {\r
332 str = dir.getPath().replace('\\', '/');\r
333 workspace = workspace.replace('\\', '/');\r
334 str = strStrip(str, workspace + "/");\r
335 fpdList.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 private int checkDBForUpdate(String workspace, String dbFileName, int VERBOSE, boolean TEST, boolean QUIET)\r
346 throws IOException {\r
347 int SpdFlag = 0;\r
348 int FpdFlag = 0;\r
349 String SpdFile = null;\r
350 String SpdFullFile = null;\r
351 String FpdFile = null;\r
352 String FpdFullFile = null;\r
353\r
354 String tmpDbFile = dbFileName + ".tmp";\r
355 String newSpd = null;\r
356 String newFpd = null;\r
357\r
358 FileReader fileReader = new FileReader(dbFileName);\r
359 BufferedReader reader = new BufferedReader(fileReader);\r
360\r
361 FileWriter fileWriter = new FileWriter(tmpDbFile);\r
362 BufferedWriter writer = new BufferedWriter(fileWriter);\r
363\r
364 String line = null;\r
365 try {\r
366 while ((line = reader.readLine()) != null) {\r
367 if (line.indexOf("Added the following") >= 0) {\r
368 wasModified = true;\r
369 continue;\r
370 }\r
371 //\r
372 // Process for .spd files\r
373 //\r
374 if (line.indexOf("<PackageList") >= 0) {\r
375 SpdFlag = 1;\r
376 } else {\r
377 if (line.indexOf("</PackageList") >= 0) {\r
378 SpdFlag = 2;\r
379 }\r
380 }\r
381 if (SpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
382 SpdFile = strStrip(line, "<Filename>");\r
383 SpdFile = strStrip(SpdFile, "</Filename>");\r
384 SpdFile = SpdFile.trim();\r
385 SpdFullFile = workspace + File.separator + SpdFile;\r
386 if (!(new File(SpdFullFile)).exists()) {\r
387 if (VERBOSE > 0)\r
388 System.out.println("WARNING: Removing SPD file: " + SpdFile\r
389 + "from the DB, as it does not exist!");\r
390 wasModified = true;\r
391 continue;\r
392 }\r
393 // Don't add files that are already in the database\r
394 spdList.remove(SpdFile);\r
395 }\r
396 if (SpdFlag == 2) {\r
397 int cflag = 0;\r
398 for (int i = 0; i < spdList.size(); i++) {\r
399 newSpd = spdList.get(i);\r
400 newSpd = newSpd.trim();\r
401 if (newSpd.length() > 0) {\r
402 if (cflag == 0) {\r
403 Calendar c = Calendar.getInstance();\r
404 if (TEST == false)\r
405 writer.write(" <!-- Adding the following SPD files " + c.getTime() + " -->\n");\r
406 }\r
407 if (VERBOSE > 0)\r
408 System.out.println(" Adding SPD file: " + newSpd);\r
409 if (TEST == false)\r
410 writer.write(" <Filename>" + newSpd + "</Filename>\n");\r
411 cflag++;\r
412 wasModified = true;\r
413 }\r
414 }\r
415 SpdFlag++;\r
416 }\r
417 //\r
418 // Process for .fpd files\r
419 // \r
420 if (line.indexOf("<PlatformList") >= 0) {\r
421 FpdFlag = 1;\r
422 } else {\r
423 if (line.indexOf("</PlatformList") >= 0) {\r
424 FpdFlag = 2;\r
425 }\r
426 }\r
427 if (FpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
428 FpdFile = strStrip(line, "<Filename>");\r
429 FpdFile = strStrip(FpdFile, "</Filename>");\r
430 FpdFile = FpdFile.trim();\r
431 FpdFullFile = workspace + File.separator + FpdFile;\r
432 if (!(new File(FpdFullFile)).exists()) {\r
433 if (VERBOSE > 0)\r
434 System.out.println("WARNING: Removing FPD file: " + FpdFile\r
435 + " from the DB, as it does not exist!");\r
436 wasModified = true;\r
437 continue;\r
438 }\r
439 // Don't add files that are already in the database\r
440 fpdList.remove(FpdFile);\r
441 }\r
442 if (FpdFlag == 2) {\r
443 int cflag = 0;\r
444 for (int i = 0; i < fpdList.size(); i++) {\r
445 newFpd = fpdList.get(i);\r
446 newFpd = newFpd.trim();\r
447 if (newFpd.length() > 0) {\r
448 if (cflag == 0) {\r
449 Calendar c = Calendar.getInstance();\r
450 if (TEST == false)\r
451 writer.write(" <!-- Adding the following FPD files " + c.getTime() + " -->\n");\r
452 }\r
453 if (VERBOSE > 0)\r
454 System.out.println(" Adding FPD file: " + newFpd);\r
455 if (TEST == false)\r
456 writer.write(" <Filename>" + newFpd + "</Filename>\n");\r
457 cflag++;\r
458 wasModified = true;\r
459 }\r
460 }\r
461 FpdFlag++;\r
462 }\r
463\r
464 if (DEBUG > 2) {\r
465 System.out.println(line);\r
466 }\r
467 if (TEST == false) {\r
468 writer.write(line);\r
469 writer.newLine();\r
470 }\r
471 }\r
472 reader.close();\r
473 writer.close();\r
474\r
475 } catch (IOException e) {\r
476 System.out.println("ERROR I/O Exception occured! " + e);\r
477 System.exit(FAIL);\r
478 }\r
479\r
480 if (wasModified) {\r
481 if ((VERBOSE > 0) && (QUIET == false))\r
482 System.out.println("FrameworkDatabase has been UPDATED for this Workspace!\n");\r
483 if (TEST == false)\r
484 copyFile(tmpDbFile, dbFileName);\r
485 } else {\r
486 if ((VERBOSE > 0) && (QUIET == false))\r
487 System.out.println("FrameworkDatabase correct for this Workspace!\n");\r
488 }\r
489\r
490 if (TEST == false) {\r
491 File tmpFile = new File(tmpDbFile);\r
492 if (tmpFile.exists()) {\r
493 tmpFile.delete();\r
494 }\r
495 }\r
496\r
497 return PASS;\r
498 }\r
499\r
500 static String strStrip(String str, String pattern) {\r
501 int ps = 0;\r
502 int pe = 0;\r
503 StringBuffer result = new StringBuffer();\r
504\r
505 while ((pe = str.indexOf(pattern, ps)) >= 0) {\r
506 result.append(str.substring(ps, pe));\r
507 ps = pe + pattern.length();\r
508 }\r
509 result.append(str.substring(ps));\r
510 return result.toString();\r
511 }\r
512\r
513 private String checkDuplicateStrings(String aString, ArrayList<String> aList) {\r
514 // This routine checks a string against an array.\r
515 // If the string is found, it will return an empty string.\r
516 // If the string is not found, it adds the string to the array, and\r
517 // returns the string to the caller.\r
518 for (int lctr = 0; lctr < aList.size(); lctr++) {\r
519 if (DEBUG > 8)\r
520 System.out.println("Comparing: \n" + aString.replace(" ", "").replace("\n", "") + "\nTo: \n"\r
521 + aList.get(lctr).replace(" ", "").replace("\n", "").toString().trim());\r
522 if (aString.replace(" ", "").replace("\n", "").contains(\r
523 aList.get(lctr).replace(" ", "").replace("\n", "")\r
524 .toString().trim())) {\r
525 if ((DEBUG > 3) || (VERBOSE > 3))\r
526 System.out.println("Found a duplicate String, skipping!");\r
527 return "";\r
528 }\r
529 }\r
530 if ((DEBUG > 3) || (VERBOSE > 3))\r
531 System.out.println("Returning UNIQUE String!\n " + aString);\r
532 aList.add(aString);\r
533 return aString;\r
534 }\r
535\r
536 private static class XmlConfig {\r
537 public static XmlCursor setupXmlCursor(XmlCursor cursor) {\r
538 String uri = "http://www.TianoCore.org/2006/Edk2.0";\r
539 cursor.push();\r
540 cursor.toNextToken();\r
541 cursor.insertNamespace("", uri);\r
542 cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");\r
543 cursor.pop();\r
544 return cursor;\r
545\r
546 }\r
547\r
548 public static XmlOptions setupXmlOptions() {\r
549 XmlOptions options = new XmlOptions();\r
550 options.setCharacterEncoding("UTF-8");\r
551 options.setSavePrettyPrint();\r
552 options.setSavePrettyPrintIndent(2);\r
553 return options;\r
554 }\r
555\r
556 }\r
557}\r