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