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
, 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!");
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
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
209 // copy from source file to destination file
210 private int copyFile(String src
, String dst
) throws IOException
{
212 File srcFile
= new File(src
);
213 FileReader fileReader
= new FileReader(srcFile
);
214 BufferedReader reader
= new BufferedReader(fileReader
);
216 File dstFile
= new File(dst
);
217 FileWriter fileWriter
= new FileWriter(dstFile
);
218 BufferedWriter writer
= new BufferedWriter(fileWriter
);
221 while ((line
= reader
.readLine()) != null) {
228 } catch (IOException e
) {
229 System
.out
.println("I/O Exception during file copy: " + e
);
235 private int fixDB(String workspace
, String dbFile
, int VERBOSE
, boolean TEST
, boolean QUIET
) {
236 File wsDir
= new File(workspace
);
238 // Find all .spd and .fpd files in workspace and put them in spdList and fpdList
240 System
.out
.println("SPD File Search ");
241 findSPDFiles(wsDir
, workspace
, VERBOSE
);
245 System
.out
.println(" ");
246 System
.out
.println("FPD File Search ");
248 findFPDFiles(wsDir
, workspace
, VERBOSE
);
250 System
.out
.println(" ");
253 // check database file for possible update
254 retValue
= checkDBForUpdate(workspace
, dbFile
, VERBOSE
, TEST
, QUIET
);
255 } catch (IOException e
) {
257 System
.out
.println("Error: Updating " + dbFile
+ " file.");
261 if ((VERBOSE
> 0) && (TEST
) && (wasModified
)) {
262 System
.out
.println("FRAMEWORK Database does NOT match the contents of the WORKSPACE");
265 if ((VERBOSE
> 0) && (wasModified
== false) && (QUIET
== false))
266 System
.out
.println("FRAMEWORK Database matches the contents of the WORKSPACE");
271 private void findSPDFiles(File dir
, String workspace
, int VERBOSE
) {
274 if (dir
.isDirectory()) {
276 String
[] subdir
= dir
.list();
279 System
.out
.print(".");
285 System
.out
.println(" ");
290 for (int i
= 0; i
< subdir
.length
; i
++) {
291 findSPDFiles(new File(dir
, subdir
[i
]), workspace
, VERBOSE
);
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());
300 System
.out
.println(" " + str
);
302 System
.out
.print("+");
309 private void findFPDFiles(File dir
, String workspace
, int VERBOSE
) {
312 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 findFPDFiles(new File(dir
, subdir
[i
]), workspace
, VERBOSE
);
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());
337 System
.out
.println(" " + str
);
339 System
.out
.print("+");
345 private int checkDBForUpdate(String workspace
, String dbFileName
, int VERBOSE
, boolean TEST
, boolean QUIET
)
349 String SpdFile
= null;
350 String SpdFullFile
= null;
351 String FpdFile
= null;
352 String FpdFullFile
= null;
354 String tmpDbFile
= dbFileName
+ ".tmp";
355 String newSpd
= null;
356 String newFpd
= null;
358 FileReader fileReader
= new FileReader(dbFileName
);
359 BufferedReader reader
= new BufferedReader(fileReader
);
361 FileWriter fileWriter
= new FileWriter(tmpDbFile
);
362 BufferedWriter writer
= new BufferedWriter(fileWriter
);
366 while ((line
= reader
.readLine()) != null) {
367 if (line
.indexOf("Added the following") >= 0) {
372 // Process for .spd files
374 if (line
.indexOf("<PackageList") >= 0) {
377 if (line
.indexOf("</PackageList") >= 0) {
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()) {
388 System
.out
.println("WARNING: Removing SPD file: " + SpdFile
389 + "from the DB, as it does not exist!");
393 // Don't add files that are already in the database
394 spdList
.remove(SpdFile
);
398 for (int i
= 0; i
< spdList
.size(); i
++) {
399 newSpd
= spdList
.get(i
);
400 newSpd
= newSpd
.trim();
401 if (newSpd
.length() > 0) {
403 Calendar c
= Calendar
.getInstance();
405 writer
.write(" <!-- Adding the following SPD files " + c
.getTime() + " -->\n");
408 System
.out
.println(" Adding SPD file: " + newSpd
);
410 writer
.write(" <Filename>" + newSpd
+ "</Filename>\n");
418 // Process for .fpd files
420 if (line
.indexOf("<PlatformList") >= 0) {
423 if (line
.indexOf("</PlatformList") >= 0) {
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()) {
434 System
.out
.println("WARNING: Removing FPD file: " + FpdFile
435 + " from the DB, as it does not exist!");
439 // Don't add files that are already in the database
440 fpdList
.remove(FpdFile
);
444 for (int i
= 0; i
< fpdList
.size(); i
++) {
445 newFpd
= fpdList
.get(i
);
446 newFpd
= newFpd
.trim();
447 if (newFpd
.length() > 0) {
449 Calendar c
= Calendar
.getInstance();
451 writer
.write(" <!-- Adding the following FPD files " + c
.getTime() + " -->\n");
454 System
.out
.println(" Adding FPD file: " + newFpd
);
456 writer
.write(" <Filename>" + newFpd
+ "</Filename>\n");
465 System
.out
.println(line
);
475 } catch (IOException e
) {
476 System
.out
.println("ERROR I/O Exception occured! " + e
);
481 if ((VERBOSE
> 0) && (QUIET
== false))
482 System
.out
.println("FrameworkDatabase has been UPDATED for this Workspace!\n");
484 copyFile(tmpDbFile
, dbFileName
);
486 if ((VERBOSE
> 0) && (QUIET
== false))
487 System
.out
.println("FrameworkDatabase correct for this Workspace!\n");
491 File tmpFile
= new File(tmpDbFile
);
492 if (tmpFile
.exists()) {
500 static String
strStrip(String str
, String pattern
) {
503 StringBuffer result
= new StringBuffer();
505 while ((pe
= str
.indexOf(pattern
, ps
)) >= 0) {
506 result
.append(str
.substring(ps
, pe
));
507 ps
= pe
+ pattern
.length();
509 result
.append(str
.substring(ps
));
510 return result
.toString();
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
++) {
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!");
530 if ((DEBUG
> 3) || (VERBOSE
> 3))
531 System
.out
.println("Returning UNIQUE String!\n " + aString
);
536 private static class XmlConfig
{
537 public static XmlCursor
setupXmlCursor(XmlCursor cursor
) {
538 String uri
= "http://www.TianoCore.org/2006/Edk2.0";
540 cursor
.toNextToken();
541 cursor
.insertNamespace("", uri
);
542 cursor
.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
548 public static XmlOptions
setupXmlOptions() {
549 XmlOptions options
= new XmlOptions();
550 options
.setCharacterEncoding("UTF-8");
551 options
.setSavePrettyPrint();
552 options
.setSavePrettyPrintIndent(2);