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