--- /dev/null
+// @file\r
+// This Class processes multiple MSA files and merges them into a single, \r
+// merged MSA file. It will optionally add the merged MSA file into a package.\r
+//\r
+//\r
+// Copyright (c) 2006, Intel Corporation All rights reserved.\r
+//\r
+// This program and the accompanying materials are licensed and made\r
+// available under the terms and conditions of the BSD License which\r
+// accompanies this distribution. The full text of the license may \r
+// be found at http://opensource.org/licenses/bsd-license.php\r
+//\r
+// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+//\r
+//\r
+\r
+package org.tianocore.DbTools;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+// import java.sql.Time;\r
+\r
+// import java.lang.*;\r
+// import java.lang.ExceptionInInitializerError;\r
+\r
+// import org.apache.xmlbeans.*;\r
+import org.apache.xmlbeans.XmlCursor;\r
+// import org.apache.xmlbeans.XmlObject;\r
+import org.apache.xmlbeans.XmlOptions;\r
+// import org.apache.xmlbeans.XmlException;\r
+import org.tianocore.*;\r
+\r
+// import org.tianocore.*;\r
+// FrameworkDatabase.db Schema Elements\r
+import org.tianocore.FrameworkDatabaseDocument.*;\r
+import org.tianocore.FdbHeaderDocument.*;\r
+import org.tianocore.PackageListDocument.*;\r
+import org.tianocore.PlatformListDocument.*;\r
+import org.tianocore.FarListDocument.*;\r
+\r
+// FAR Schema Elements\r
+// import org.tianocore.FrameworkArchiveManifestDocument.*;\r
+// import org.tianocore.FarHeaderDocument.*;\r
+// import org.tianocore.FarPackageDocument.*;\r
+// import org.tianocore.FarPlatformDocument.*;\r
+\r
+// FPD Schema Elements\r
+import org.tianocore.PlatformSurfaceAreaDocument.*;\r
+import org.tianocore.PlatformHeaderDocument.*;\r
+\r
+// import org.tianocore.FrameworkModulesDocument.*;\r
+// import org.tianocore.DynamicPcdBuildDefinitionsDocument.*;\r
+\r
+// SPD Schema Elements\r
+// import org.tianocore.PackageSurfaceAreaDocument.*;\r
+// import org.tianocore.SpdHeaderDocument.*;\r
+// import org.tianocore.LibraryClassDeclarationsDocument.*;\r
+// import org.tianocore.GuidDeclarationsDocument.*;\r
+// import org.tianocore.ProtocolDeclarationsDocument.*;\r
+// import org.tianocore.PpiDeclarationsDocument.*;\r
+// import org.tianocore.PcdDeclarationsDocument.*;\r
+\r
+// MSA Schema Elements\r
+// import org.tianocore.ModuleSurfaceAreaDocument.*;\r
+// import org.tianocore.MsaHeaderDocument.*;\r
+// import org.tianocore.LicenseDocument.*;\r
+// import org.tianocore.LibraryClassDefinitionsDocument.*;\r
+// import org.tianocore.PackageDependenciesDocument.*;\r
+// import org.tianocore.ProtocolsDocument.*;\r
+// import org.tianocore.PPIsDocument.*;\r
+// import org.tianocore.PcdCodedDocument.*;\r
+\r
+public class UpdateDb {\r
+\r
+ private final int DEBUG = 0;\r
+\r
+ private final int PASS = 0;\r
+\r
+ private final int FAIL = 1;\r
+\r
+ private int dCtr = 0;\r
+\r
+ private int lCtr = 0;\r
+\r
+ // Future implementations\r
+ // private XmlCursor cursor = null;\r
+\r
+ // FrameworkDatabase Component Elements\r
+ // private FrameworkDatabaseDocument fdbDoc = null;\r
+\r
+ // private FrameworkDatabase fdbInstance = null;\r
+\r
+ // private FdbHeader fdbHeader = null;\r
+\r
+ // private PackageList fdbPackageList = null;\r
+\r
+ // private ArrayList<String> packageList = new ArrayList<String>();\r
+\r
+ // private PlatformList fdbPlatformList = null;\r
+\r
+ // private ArrayList<String> platformList = new ArrayList<String>();\r
+\r
+ // private FarList fdbFarList = null;\r
+\r
+ // private ArrayList<String> farList = new ArrayList<String>();\r
+\r
+ private ArrayList<String> spdList = new ArrayList<String>();\r
+\r
+ private ArrayList<String> fpdList = new ArrayList<String>();\r
+\r
+ // Platform Components\r
+ // private PlatformSurfaceAreaDocument fpdDoc = null;\r
+\r
+ // private PlatformSurfaceArea fpd = null;\r
+\r
+ // private PlatformHeader fpdHeader = null;\r
+\r
+ private boolean wasModified = false;\r
+\r
+ private int VERBOSE = 0;\r
+\r
+ // The combineMsaFiles routine is the primary routine for creating a \r
+ // Merged MSA file.\r
+\r
+ public UpdateDb() {\r
+ init();\r
+ }\r
+\r
+ private void init() {\r
+\r
+ }\r
+\r
+ public int getAllEntries(String dbFilename, int VERBOSE_FLAG) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int getCsvEntries(String dbFilename, int VERBOSE_FLAG) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int fixDatabase(String workspace, int VERBOSE_FLAG, boolean INTERACTIVE, boolean TEST, boolean QUIET) {\r
+ int result = PASS;\r
+ String dbFile = workspace + File.separator + "Tools" + File.separator + "Conf" + File.separator\r
+ + "FrameworkDatabase.db";\r
+ String dbBak = dbFile + ".bak";\r
+\r
+ try {\r
+ // Make a backup file for frameworkDatabase\r
+ if (VERBOSE_FLAG > 3)\r
+ System.out.println("Creating Backup file: " + dbBak);\r
+ copyFile(dbFile, dbBak);\r
+ } catch (IOException e) {\r
+ System.out.println("Error: Cannot make backup file " + dbBak);\r
+ return FAIL;\r
+ }\r
+\r
+ // Datebase update for SPD and FPD files\r
+ result = fixDB(workspace, dbFile, VERBOSE_FLAG, TEST, QUIET);\r
+ if ((result == 0) && (TEST) && (QUIET == false))\r
+ System.out.println("Workspace is consistent with current database!");\r
+ return PASS;\r
+ }\r
+\r
+ public int addItem(String dbFilename, String item, int VERBOSE_FLAG, boolean INTERACTIVE) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int delItem(String workspace, String item, int VERBOSE_FLAG, boolean INTERACTIVE) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int findItem(String workspace, int VERBOSE_FLAG, String findWhat, String item) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int findFars(String workspace, int VERBOSE_FLAG) {\r
+ System.out.println("Function not yet implemented!");\r
+ return FAIL;\r
+ }\r
+\r
+ public int findSpds(String workspace, int VERBOSE_FLAG) {\r
+ System.out.println("The following Package (SPD) files are in the workspace!"); \r
+ File wsDir = new File(workspace);\r
+ findSPDFiles(wsDir, workspace, VERBOSE);\r
+ for (int i = 0; i < spdList.size(); i++) {\r
+ System.out.println(" " + spdList.get(i).trim());\r
+ // TODO: get the PackageName from the file\r
+ }\r
+ return FAIL;\r
+ }\r
+\r
+ public int findFpds(String workspace, int VERBOSE_FLAG) {\r
+ System.out.println("The following Platform (FPD) files are in the workspace!");\r
+ File wsDir = new File(workspace);\r
+ findFPDFiles(wsDir, workspace, VERBOSE);\r
+ for (int i = 0; i < fpdList.size(); i++) {\r
+ System.out.println(" " + fpdList.get(i).trim());\r
+ // TODO: get the PlatformName from the file\r
+ }\r
+ return FAIL;\r
+ }\r
+\r
+ // copy from source file to destination file\r
+ private int copyFile(String src, String dst) throws IOException {\r
+ try {\r
+ File srcFile = new File(src);\r
+ FileReader fileReader = new FileReader(srcFile);\r
+ BufferedReader reader = new BufferedReader(fileReader);\r
+\r
+ File dstFile = new File(dst);\r
+ FileWriter fileWriter = new FileWriter(dstFile);\r
+ BufferedWriter writer = new BufferedWriter(fileWriter);\r
+\r
+ String line = null;\r
+ while ((line = reader.readLine()) != null) {\r
+ writer.write(line);\r
+ writer.newLine();\r
+ }\r
+\r
+ reader.close();\r
+ writer.close();\r
+ } catch (IOException e) {\r
+ System.out.println("I/O Exception during file copy: " + e);\r
+ }\r
+\r
+ return PASS;\r
+ }\r
+\r
+ private int fixDB(String workspace, String dbFile, int VERBOSE, boolean TEST, boolean QUIET) {\r
+ File wsDir = new File(workspace);\r
+ int retValue = PASS;\r
+ // Find all .spd and .fpd files in workspace and put them in spdList and fpdList\r
+ if (VERBOSE > 0)\r
+ System.out.println("SPD File Search ");\r
+ findSPDFiles(wsDir, workspace, VERBOSE);\r
+ dCtr = 0;\r
+ lCtr = 0;\r
+ if (VERBOSE > 0) {\r
+ System.out.println(" ");\r
+ System.out.println("FPD File Search ");\r
+ }\r
+ findFPDFiles(wsDir, workspace, VERBOSE);\r
+ if (VERBOSE > 0)\r
+ System.out.println(" ");\r
+\r
+ try {\r
+ // check database file for possible update\r
+ retValue = checkDBForUpdate(workspace, dbFile, VERBOSE, TEST, QUIET);\r
+ } catch (IOException e) {\r
+ if (QUIET == false)\r
+ System.out.println("Error: Updating " + dbFile + " file.");\r
+ return FAIL;\r
+ }\r
+\r
+ if ((VERBOSE > 0) && (TEST) && (wasModified)) {\r
+ System.out.println("FRAMEWORK Database does NOT match the contents of the WORKSPACE");\r
+ retValue = FAIL;\r
+ }\r
+ if ((VERBOSE > 0) && (wasModified == false) && (QUIET == false))\r
+ System.out.println("FRAMEWORK Database matches the contents of the WORKSPACE");\r
+\r
+ return retValue;\r
+ }\r
+\r
+ private void findSPDFiles(File dir, String workspace, int VERBOSE) {\r
+ String str;\r
+\r
+ if (dir.isDirectory()) {\r
+ dCtr++;\r
+ String[] subdir = dir.list();\r
+ if (dCtr >= 10) {\r
+ if (VERBOSE > 2)\r
+ System.out.print(".");\r
+ dCtr = 0;\r
+ lCtr++;\r
+ }\r
+ if (lCtr > 79) {\r
+ if (VERBOSE > 2)\r
+ System.out.println(" ");\r
+ lCtr = 0;\r
+ dCtr = 0;\r
+ }\r
+\r
+ for (int i = 0; i < subdir.length; i++) {\r
+ findSPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
+ }\r
+ } else {\r
+ if (dir.toString().toLowerCase().endsWith(".spd")) {\r
+ str = dir.getPath().replace('\\', '/');\r
+ workspace = workspace.replace('\\', '/');\r
+ str = strStrip(str, workspace + "/");\r
+ spdList.add(str.toString());\r
+ if (VERBOSE == 2)\r
+ System.out.println(" " + str);\r
+ if (VERBOSE > 2)\r
+ System.out.print("+");\r
+ lCtr++;\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ private void findFPDFiles(File dir, String workspace, int VERBOSE) {\r
+ String str;\r
+\r
+ if (dir.isDirectory()) {\r
+ String[] subdir = dir.list();\r
+ if (dCtr >= 10) {\r
+ if (VERBOSE > 2)\r
+ System.out.print(".");\r
+ dCtr = 0;\r
+ lCtr++;\r
+ }\r
+ if (lCtr > 79) {\r
+ if (VERBOSE > 2)\r
+ System.out.println(" ");\r
+ lCtr = 0;\r
+ dCtr = 0;\r
+ }\r
+\r
+ for (int i = 0; i < subdir.length; i++) {\r
+ findFPDFiles(new File(dir, subdir[i]), workspace, VERBOSE);\r
+ }\r
+ } else {\r
+ if (dir.toString().toLowerCase().endsWith(".fpd")) {\r
+ str = dir.getPath().replace('\\', '/');\r
+ workspace = workspace.replace('\\', '/');\r
+ str = strStrip(str, workspace + "/");\r
+ fpdList.add(str.toString());\r
+ if (VERBOSE == 2)\r
+ System.out.println(" " + str);\r
+ if (VERBOSE > 2)\r
+ System.out.print("+");\r
+ lCtr++;\r
+ }\r
+ }\r
+ }\r
+\r
+ private int checkDBForUpdate(String workspace, String dbFileName, int VERBOSE, boolean TEST, boolean QUIET)\r
+ throws IOException {\r
+ int SpdFlag = 0;\r
+ int FpdFlag = 0;\r
+ String SpdFile = null;\r
+ String SpdFullFile = null;\r
+ String FpdFile = null;\r
+ String FpdFullFile = null;\r
+\r
+ String tmpDbFile = dbFileName + ".tmp";\r
+ String newSpd = null;\r
+ String newFpd = null;\r
+\r
+ FileReader fileReader = new FileReader(dbFileName);\r
+ BufferedReader reader = new BufferedReader(fileReader);\r
+\r
+ FileWriter fileWriter = new FileWriter(tmpDbFile);\r
+ BufferedWriter writer = new BufferedWriter(fileWriter);\r
+\r
+ String line = null;\r
+ try {\r
+ while ((line = reader.readLine()) != null) {\r
+ if (line.indexOf("Added the following") >= 0) {\r
+ wasModified = true;\r
+ continue;\r
+ }\r
+ //\r
+ // Process for .spd files\r
+ //\r
+ if (line.indexOf("<PackageList") >= 0) {\r
+ SpdFlag = 1;\r
+ } else {\r
+ if (line.indexOf("</PackageList") >= 0) {\r
+ SpdFlag = 2;\r
+ }\r
+ }\r
+ if (SpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
+ SpdFile = strStrip(line, "<Filename>");\r
+ SpdFile = strStrip(SpdFile, "</Filename>");\r
+ SpdFile = SpdFile.trim();\r
+ SpdFullFile = workspace + File.separator + SpdFile;\r
+ if (!(new File(SpdFullFile)).exists()) {\r
+ if (VERBOSE > 0)\r
+ System.out.println("WARNING: Removing SPD file: " + SpdFile\r
+ + "from the DB, as it does not exist!");\r
+ wasModified = true;\r
+ continue;\r
+ }\r
+ // Don't add files that are already in the database\r
+ spdList.remove(SpdFile);\r
+ }\r
+ if (SpdFlag == 2) {\r
+ int cflag = 0;\r
+ for (int i = 0; i < spdList.size(); i++) {\r
+ newSpd = spdList.get(i);\r
+ newSpd = newSpd.trim();\r
+ if (newSpd.length() > 0) {\r
+ if (cflag == 0) {\r
+ Calendar c = Calendar.getInstance();\r
+ if (TEST == false)\r
+ writer.write(" <!-- Adding the following SPD files " + c.getTime() + " -->\n");\r
+ }\r
+ if (VERBOSE > 0)\r
+ System.out.println(" Adding SPD file: " + newSpd);\r
+ if (TEST == false)\r
+ writer.write(" <Filename>" + newSpd + "</Filename>\n");\r
+ cflag++;\r
+ wasModified = true;\r
+ }\r
+ }\r
+ SpdFlag++;\r
+ }\r
+ //\r
+ // Process for .fpd files\r
+ // \r
+ if (line.indexOf("<PlatformList") >= 0) {\r
+ FpdFlag = 1;\r
+ } else {\r
+ if (line.indexOf("</PlatformList") >= 0) {\r
+ FpdFlag = 2;\r
+ }\r
+ }\r
+ if (FpdFlag == 1 && line.indexOf("Filename") >= 0) {\r
+ FpdFile = strStrip(line, "<Filename>");\r
+ FpdFile = strStrip(FpdFile, "</Filename>");\r
+ FpdFile = FpdFile.trim();\r
+ FpdFullFile = workspace + File.separator + FpdFile;\r
+ if (!(new File(FpdFullFile)).exists()) {\r
+ if (VERBOSE > 0)\r
+ System.out.println("WARNING: Removing FPD file: " + FpdFile\r
+ + " from the DB, as it does not exist!");\r
+ wasModified = true;\r
+ continue;\r
+ }\r
+ // Don't add files that are already in the database\r
+ fpdList.remove(FpdFile);\r
+ }\r
+ if (FpdFlag == 2) {\r
+ int cflag = 0;\r
+ for (int i = 0; i < fpdList.size(); i++) {\r
+ newFpd = fpdList.get(i);\r
+ newFpd = newFpd.trim();\r
+ if (newFpd.length() > 0) {\r
+ if (cflag == 0) {\r
+ Calendar c = Calendar.getInstance();\r
+ if (TEST == false)\r
+ writer.write(" <!-- Adding the following FPD files " + c.getTime() + " -->\n");\r
+ }\r
+ if (VERBOSE > 0)\r
+ System.out.println(" Adding FPD file: " + newFpd);\r
+ if (TEST == false)\r
+ writer.write(" <Filename>" + newFpd + "</Filename>\n");\r
+ cflag++;\r
+ wasModified = true;\r
+ }\r
+ }\r
+ FpdFlag++;\r
+ }\r
+\r
+ if (DEBUG > 2) {\r
+ System.out.println(line);\r
+ }\r
+ if (TEST == false) {\r
+ writer.write(line);\r
+ writer.newLine();\r
+ }\r
+ }\r
+ reader.close();\r
+ writer.close();\r
+\r
+ } catch (IOException e) {\r
+ System.out.println("ERROR I/O Exception occured! " + e);\r
+ System.exit(FAIL);\r
+ }\r
+\r
+ if (wasModified) {\r
+ if ((VERBOSE > 0) && (QUIET == false))\r
+ System.out.println("FrameworkDatabase has been UPDATED for this Workspace!\n");\r
+ if (TEST == false)\r
+ copyFile(tmpDbFile, dbFileName);\r
+ } else {\r
+ if ((VERBOSE > 0) && (QUIET == false))\r
+ System.out.println("FrameworkDatabase correct for this Workspace!\n");\r
+ }\r
+\r
+ if (TEST == false) {\r
+ File tmpFile = new File(tmpDbFile);\r
+ if (tmpFile.exists()) {\r
+ tmpFile.delete();\r
+ }\r
+ }\r
+\r
+ return PASS;\r
+ }\r
+\r
+ static String strStrip(String str, String pattern) {\r
+ int ps = 0;\r
+ int pe = 0;\r
+ StringBuffer result = new StringBuffer();\r
+\r
+ while ((pe = str.indexOf(pattern, ps)) >= 0) {\r
+ result.append(str.substring(ps, pe));\r
+ ps = pe + pattern.length();\r
+ }\r
+ result.append(str.substring(ps));\r
+ return result.toString();\r
+ }\r
+\r
+ private String checkDuplicateStrings(String aString, ArrayList<String> aList) {\r
+ // This routine checks a string against an array.\r
+ // If the string is found, it will return an empty string.\r
+ // If the string is not found, it adds the string to the array, and\r
+ // returns the string to the caller.\r
+ for (int lctr = 0; lctr < aList.size(); lctr++) {\r
+ if (DEBUG > 8)\r
+ System.out.println("Comparing: \n" + aString.replace(" ", "").replace("\n", "") + "\nTo: \n"\r
+ + aList.get(lctr).replace(" ", "").replace("\n", "").toString().trim());\r
+ if (aString.replace(" ", "").replace("\n", "").contains(\r
+ aList.get(lctr).replace(" ", "").replace("\n", "")\r
+ .toString().trim())) {\r
+ if ((DEBUG > 3) || (VERBOSE > 3))\r
+ System.out.println("Found a duplicate String, skipping!");\r
+ return "";\r
+ }\r
+ }\r
+ if ((DEBUG > 3) || (VERBOSE > 3))\r
+ System.out.println("Returning UNIQUE String!\n " + aString);\r
+ aList.add(aString);\r
+ return aString;\r
+ }\r
+\r
+ private static class XmlConfig {\r
+ public static XmlCursor setupXmlCursor(XmlCursor cursor) {\r
+ String uri = "http://www.TianoCore.org/2006/Edk2.0";\r
+ cursor.push();\r
+ cursor.toNextToken();\r
+ cursor.insertNamespace("", uri);\r
+ cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");\r
+ cursor.pop();\r
+ return cursor;\r
+\r
+ }\r
+\r
+ public static XmlOptions setupXmlOptions() {\r
+ XmlOptions options = new XmlOptions();\r
+ options.setCharacterEncoding("UTF-8");\r
+ options.setSavePrettyPrint();\r
+ options.setSavePrettyPrintIndent(2);\r
+ return options;\r
+ }\r
+\r
+ }\r
+}\r