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.
6 // Copyright (c) 2006, Intel Corporation All rights reserved.
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
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.
18 package org
.tianocore
.DbTools
;
22 // import java.sql.Time;
24 // import java.lang.*;
25 // import java.lang.ExceptionInInitializerError;
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
.*;
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
.*;
42 // FAR Schema Elements
43 // import org.tianocore.FrameworkArchiveManifestDocument.*;
44 // import org.tianocore.FarHeaderDocument.*;
45 // import org.tianocore.FarPackageDocument.*;
46 // import org.tianocore.FarPlatformDocument.*;
48 // FPD Schema Elements
49 import org
.tianocore
.PlatformSurfaceAreaDocument
.*;
50 import org
.tianocore
.PlatformHeaderDocument
.*;
52 // import org.tianocore.FrameworkModulesDocument.*;
53 // import org.tianocore.DynamicPcdBuildDefinitionsDocument.*;
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.*;
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.*;
74 public class UpdateDb
{
76 private final int DEBUG
= 0;
78 private final int PASS
= 0;
80 private final int FAIL
= 1;
86 // Future implementations
87 // private XmlCursor cursor = null;
89 // FrameworkDatabase Component Elements
90 // private FrameworkDatabaseDocument fdbDoc = null;
92 // private FrameworkDatabase fdbInstance = null;
94 // private FdbHeader fdbHeader = null;
96 // private PackageList fdbPackageList = null;
98 // private ArrayList<String> packageList = new ArrayList<String>();
100 // private PlatformList fdbPlatformList = null;
102 // private ArrayList<String> platformList = new ArrayList<String>();
104 // private FarList fdbFarList = null;
106 // private ArrayList<String> farList = new ArrayList<String>();
108 private ArrayList
<String
> spdList
= new ArrayList
<String
>();
110 private ArrayList
<String
> fpdList
= new ArrayList
<String
>();
112 // Platform Components
113 // private PlatformSurfaceAreaDocument fpdDoc = null;
115 // private PlatformSurfaceArea fpd = null;
117 // private PlatformHeader fpdHeader = null;
119 private boolean wasModified
= false;
121 private int VERBOSE
= 0;
123 // The combineMsaFiles routine is the primary routine for creating a
130 private void init() {
134 public int getAllEntries(String dbFilename
, int VERBOSE_FLAG
) {
135 System
.out
.println("Function not yet implemented!");
139 public int getCsvEntries(String dbFilename
, int VERBOSE_FLAG
) {
140 System
.out
.println("Function not yet implemented!");
144 public int fixDatabase(String workspace
, int VERBOSE_FLAG
, boolean INTERACTIVE
, boolean TEST
, boolean QUIET
) {
146 String dbFile
= workspace
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
147 + "FrameworkDatabase.db";
148 String dbBak
= dbFile
+ ".bak";
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
);
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!");
167 public int addItem(String dbFilename
, String item
, int VERBOSE_FLAG
, boolean INTERACTIVE
) {
168 System
.out
.println("Function not yet implemented!");
172 public int delItem(String workspace
, String item
, int VERBOSE_FLAG
, boolean INTERACTIVE
) {
173 System
.out
.println("Function not yet implemented!");
177 public int findItem(String workspace
, int VERBOSE_FLAG
, String findWhat
, String item
) {
178 System
.out
.println("Function not yet implemented!");
182 public int findFars(String workspace
, int VERBOSE_FLAG
) {
183 System
.out
.println("Function not yet implemented!");
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
);
195 FileReader fileReader
= new FileReader(spdFilename
);
196 BufferedReader reader
= new BufferedReader(fileReader
);
197 String PackageName
= 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());
208 } catch (IOException e
) {
209 System
.out
.println("ERROR Reading File: " + Filename
+ e
);
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
);
225 FileReader fileReader
= new FileReader(fpdFilename
);
226 BufferedReader reader
= new BufferedReader(fileReader
);
227 String PlatformName
= 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());
238 } catch (IOException e
) {
239 System
.out
.println("ERROR Reading File: " + Filename
+ e
);
246 // copy from source file to destination file
247 private int copyFile(String src
, String dst
) throws IOException
{
249 File srcFile
= new File(src
);
250 FileReader fileReader
= new FileReader(srcFile
);
251 BufferedReader reader
= new BufferedReader(fileReader
);
253 File dstFile
= new File(dst
);
254 FileWriter fileWriter
= new FileWriter(dstFile
);
255 BufferedWriter writer
= new BufferedWriter(fileWriter
);
258 while ((line
= reader
.readLine()) != null) {
265 } catch (IOException e
) {
266 System
.out
.println("I/O Exception during file copy: " + e
);
272 private int fixDB(String workspace
, String dbFile
, int VERBOSE
, boolean INTERACTIVE
, boolean TEST
, boolean QUIET
) {
273 File wsDir
= new File(workspace
);
275 // Find all .spd and .fpd files in workspace and put them in spdList and fpdList
277 System
.out
.println("SPD File Search ");
278 findSPDFiles(wsDir
, workspace
, VERBOSE
);
282 System
.out
.println(" ");
283 System
.out
.println("FPD File Search ");
285 findFPDFiles(wsDir
, workspace
, VERBOSE
);
287 System
.out
.println(" ");
290 // check database file for possible update
291 retValue
= checkDBForUpdate(workspace
, dbFile
, VERBOSE
, INTERACTIVE
, TEST
, QUIET
);
292 } catch (IOException e
) {
294 System
.out
.println("Error: Updating " + dbFile
+ " file.");
298 if ((VERBOSE
> 0) && (TEST
) && (wasModified
)) {
299 System
.out
.println("FRAMEWORK Database does NOT match the contents of the WORKSPACE");
302 if ((VERBOSE
> 0) && (wasModified
== false) && (QUIET
== false))
303 System
.out
.println("FRAMEWORK Database matches the contents of the WORKSPACE");
308 private void findSPDFiles(File dir
, String workspace
, int VERBOSE
) {
311 if (dir
.isDirectory()) {
313 String
[] subdir
= dir
.list();
316 System
.out
.print(".");
322 System
.out
.println(" ");
327 for (int i
= 0; i
< subdir
.length
; i
++) {
328 findSPDFiles(new File(dir
, subdir
[i
]), workspace
, VERBOSE
);
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());
337 System
.out
.println(" " + str
);
339 System
.out
.print("+");
346 private void findFPDFiles(File dir
, String workspace
, int VERBOSE
) {
349 if (dir
.isDirectory()) {
350 String
[] subdir
= dir
.list();
353 System
.out
.print(".");
359 System
.out
.println(" ");
364 for (int i
= 0; i
< subdir
.length
; i
++) {
365 findFPDFiles(new File(dir
, subdir
[i
]), workspace
, VERBOSE
);
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());
374 System
.out
.println(" " + str
);
376 System
.out
.print("+");
382 private int checkDBForUpdate(String workspace
, String dbFileName
, int VERBOSE
, boolean INTERACTIVE
, boolean TEST
, boolean QUIET
)
386 String SpdFile
= null;
387 String SpdFullFile
= null;
388 String FpdFile
= null;
389 String FpdFullFile
= null;
391 String tmpDbFile
= dbFileName
+ ".tmp";
392 String newSpd
= null;
393 String newFpd
= null;
395 FileReader fileReader
= new FileReader(dbFileName
);
396 BufferedReader reader
= new BufferedReader(fileReader
);
398 FileWriter fileWriter
= new FileWriter(tmpDbFile
);
399 BufferedWriter writer
= new BufferedWriter(fileWriter
);
403 while ((line
= reader
.readLine()) != null) {
404 if (line
.indexOf("Added the following") >= 0) {
409 // Process for .spd files
411 if (line
.indexOf("<PackageList") >= 0) {
414 if (line
.indexOf("</PackageList") >= 0) {
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()) {
425 System
.out
.println("WARNING: Removing SPD file: " + SpdFile
426 + "from the DB, as it does not exist!");
430 // Don't add files that are already in the database
431 spdList
.remove(SpdFile
);
435 for (int i
= 0; i
< spdList
.size(); i
++) {
436 newSpd
= spdList
.get(i
);
437 newSpd
= newSpd
.trim();
438 if (newSpd
.length() > 0) {
440 Calendar c
= Calendar
.getInstance();
442 writer
.write(" <!-- Adding the following SPD files " + c
.getTime() + " -->\n");
445 System
.out
.println(" Adding SPD file: " + newSpd
);
447 writer
.write(" <Filename>" + newSpd
+ "</Filename>\n");
455 // Process for .fpd files
457 if (line
.indexOf("<PlatformList") >= 0) {
460 if (line
.indexOf("</PlatformList") >= 0) {
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()) {
471 System
.out
.println("WARNING: Removing FPD file: " + FpdFile
472 + " from the DB, as it does not exist!");
476 // Don't add files that are already in the database
477 fpdList
.remove(FpdFile
);
481 for (int i
= 0; i
< fpdList
.size(); i
++) {
482 newFpd
= fpdList
.get(i
);
483 newFpd
= newFpd
.trim();
484 if (newFpd
.length() > 0) {
486 Calendar c
= Calendar
.getInstance();
488 writer
.write(" <!-- Adding the following FPD files " + c
.getTime() + " -->\n");
491 System
.out
.println(" Adding FPD file: " + newFpd
);
493 writer
.write(" <Filename>" + newFpd
+ "</Filename>\n");
502 System
.out
.println(line
);
512 } catch (IOException e
) {
513 System
.out
.println("ERROR I/O Exception occured! " + e
);
518 if ((VERBOSE
> 0) && (QUIET
== false))
519 System
.out
.println("FrameworkDatabase has been UPDATED for this Workspace!\n");
521 copyFile(tmpDbFile
, dbFileName
);
523 if ((VERBOSE
> 0) && (QUIET
== false))
524 System
.out
.println("FrameworkDatabase correct for this Workspace!\n");
528 File tmpFile
= new File(tmpDbFile
);
529 if (tmpFile
.exists()) {
537 static String
strStrip(String str
, String pattern
) {
540 StringBuffer result
= new StringBuffer();
542 while ((pe
= str
.indexOf(pattern
, ps
)) >= 0) {
543 result
.append(str
.substring(ps
, pe
));
544 ps
= pe
+ pattern
.length();
546 result
.append(str
.substring(ps
));
547 return result
.toString();
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
++) {
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!");
567 if ((DEBUG
> 3) || (VERBOSE
> 3))
568 System
.out
.println("Returning UNIQUE String!\n " + aString
);
573 private static class XmlConfig
{
574 public static XmlCursor
setupXmlCursor(XmlCursor cursor
) {
575 String uri
= "http://www.TianoCore.org/2006/Edk2.0";
577 cursor
.toNextToken();
578 cursor
.insertNamespace("", uri
);
579 cursor
.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
585 public static XmlOptions
setupXmlOptions() {
586 XmlOptions options
= new XmlOptions();
587 options
.setCharacterEncoding("UTF-8");
588 options
.setSavePrettyPrint();
589 options
.setSavePrettyPrintIndent(2);