]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/workspace/WorkspaceTools.java
1. Support to Create/Update/Delete/Install far file
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / workspace / WorkspaceTools.java
index e4800816cb6264b9e8ab66d188addc5a4fc6c1c4..dda8844495e07b054947554a0aac52a34e713343 100644 (file)
  **/\r
 package org.tianocore.frameworkwizard.workspace;\r
 \r
+import java.io.File;\r
 import java.io.IOException;\r
+import java.util.List;\r
 import java.util.Vector;\r
 \r
+import org.apache.xmlbeans.XmlCursor;\r
 import org.apache.xmlbeans.XmlException;\r
 import org.tianocore.DbPathAndFilename;\r
-import org.tianocore.FrameworkDatabaseDocument;\r
+import org.tianocore.FrameworkDatabaseDocument.FrameworkDatabase;\r
 import org.tianocore.IndustryStdIncludesDocument.IndustryStdIncludes;\r
 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
 import org.tianocore.MsaFilesDocument.MsaFiles;\r
@@ -35,6 +38,8 @@ import org.tianocore.frameworkwizard.common.SaveFile;
 import org.tianocore.frameworkwizard.common.Tools;\r
 import org.tianocore.frameworkwizard.common.Identifications.Identification;\r
 import org.tianocore.frameworkwizard.common.Identifications.OpenFile;\r
+import org.tianocore.frameworkwizard.far.FarHeader;\r
+import org.tianocore.frameworkwizard.far.FarIdentification;\r
 import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;\r
 import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
 import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
@@ -43,7 +48,7 @@ public class WorkspaceTools {
     //\r
     // Define class members\r
     //\r
-    private FrameworkDatabaseDocument.FrameworkDatabase fdb = null;\r
+    private FrameworkDatabase fdb = null;\r
 \r
     private Vector<ModuleIdentification> vModuleList = new Vector<ModuleIdentification>();\r
 \r
@@ -56,20 +61,147 @@ public class WorkspaceTools {
      Open Framework Database file\r
      \r
      */\r
-    private void openFrameworkDb() {\r
+    private FrameworkDatabase openFrameworkDb() {\r
         String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
         strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
         try {\r
             fdb = OpenFile.openFrameworkDb(strFrameworkDbFilePath);\r
         } catch (XmlException e) {\r
             Log.err("Open Framework Database " + strFrameworkDbFilePath, e.getMessage());\r
-            return;\r
+            return null;\r
         } catch (Exception e) {\r
             Log.err("Open Framework Database " + strFrameworkDbFilePath, "Invalid file type");\r
-            return;\r
+            return null;\r
         }\r
+        return fdb;\r
     }\r
 \r
+    public void addFarToDb(List<String> packageList, List<String> platformList, FarHeader far) {\r
+      FrameworkDatabase fdb = openFrameworkDb();\r
+      \r
+      for (int i = 0; i < packageList.size(); i++) {\r
+        DbPathAndFilename item = DbPathAndFilename.Factory.newInstance();\r
+        item.setFarGuid(far.getGuidValue());\r
+        item.setStringValue(packageList.get(i));\r
+        fdb.getPackageList().getFilenameList().add(item);\r
+      }\r
+      \r
+      for (int i = 0; i < platformList.size(); i++) {\r
+        DbPathAndFilename item = DbPathAndFilename.Factory.newInstance();\r
+        item.setFarGuid(far.getGuidValue());\r
+        item.setStringValue(platformList.get(i));\r
+        fdb.getPlatformList().getFilenameList().add(item);\r
+      }\r
+      \r
+      DbPathAndFilename farItem = DbPathAndFilename.Factory.newInstance();\r
+      farItem.setFarGuid(far.getGuidValue());\r
+      farItem.setStringValue(far.getFarName());\r
+      fdb.getFarList().getFilenameList().add(farItem);\r
+      \r
+      String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
+      strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
+      \r
+      try {\r
+        SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);\r
+      }\r
+      catch (Exception e) {\r
+        e.printStackTrace();\r
+      }\r
+    }\r
+    \r
+    \r
+    public void removeFarFromDb(FarIdentification far) {\r
+        FrameworkDatabase fdb = openFrameworkDb();\r
+        //\r
+        // Remove Packages\r
+        //\r
+        XmlCursor cursor = fdb.getPackageList().newCursor();\r
+        cursor.toFirstChild();\r
+        do {\r
+          DbPathAndFilename item = (DbPathAndFilename)cursor.getObject();\r
+          \r
+          if (item.getFarGuid() != null && item.getFarGuid().equalsIgnoreCase(far.getGuid())) {\r
+            cursor.removeXml();\r
+          }\r
+        } while (cursor.toNextSibling());\r
+        cursor.dispose();\r
+        \r
+        //\r
+        // Remove Platforms\r
+        //\r
+        cursor = fdb.getPlatformList().newCursor();\r
+        cursor.toFirstChild();\r
+        do {\r
+          DbPathAndFilename item = (DbPathAndFilename)cursor.getObject();\r
+          if (item.getFarGuid() != null && item.getFarGuid().equalsIgnoreCase(far.getGuid())) {\r
+            cursor.removeXml();\r
+          }\r
+        } while (cursor.toNextSibling());\r
+        \r
+        //\r
+        // Remove Far\r
+        //\r
+        cursor = fdb.getFarList().newCursor();\r
+        cursor.toFirstChild();\r
+        do {\r
+          DbPathAndFilename item = (DbPathAndFilename)cursor.getObject();\r
+          if (item.getFarGuid() != null && item.getFarGuid().equalsIgnoreCase(far.getGuid())) {\r
+            cursor.removeXml();\r
+          }\r
+        } while (cursor.toNextSibling());\r
+        cursor.dispose();\r
+        \r
+        String strFrameworkDbFilePath = Workspace.getCurrentWorkspace() + Workspace.getStrWorkspaceDatabaseFile();\r
+        strFrameworkDbFilePath = Tools.convertPathToCurrentOsType(strFrameworkDbFilePath);\r
+        try {\r
+          SaveFile.saveDbFile(strFrameworkDbFilePath, fdb);\r
+        }\r
+        catch (Exception e) {\r
+          e.printStackTrace();\r
+        }\r
+    }\r
+    \r
+    public String getPackageFarGuid(PackageIdentification packageId) {\r
+      openFrameworkDb();\r
+      \r
+      for (int index = 0; index < fdb.getPackageList().getFilenameList().size(); index++) {\r
+        DbPathAndFilename item = fdb.getPackageList().getFilenameArray(index);\r
+        String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
+                      + item.getStringValue();\r
+        File tempFile = new File(path);\r
+        if (tempFile.getPath().equalsIgnoreCase(packageId.getSpdFile().getPath())) {\r
+          return fdb.getPackageList().getFilenameArray(index).getFarGuid();\r
+        }\r
+      }\r
+      \r
+      return null;\r
+    }\r
+    \r
+    public String getPlatformFarGuid(PlatformIdentification platformId) {\r
+      openFrameworkDb();\r
+      \r
+      for (int index = 0; index < fdb.getPlatformList().getFilenameList().size(); index++) {\r
+        DbPathAndFilename item = fdb.getPlatformList().getFilenameArray(index);\r
+        String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
+                      + item.getStringValue();\r
+        File tempFile = new File(path);\r
+        if (tempFile.getPath().equalsIgnoreCase(platformId.getFpdFile().getPath())) {\r
+          return fdb.getPlatformList().getFilenameArray(index).getFarGuid();\r
+        }\r
+      }\r
+      \r
+      return null;\r
+    }\r
+    \r
+    public String getModuleFarGuid(ModuleIdentification moduleId) {\r
+      PackageIdentification packageId = getPackageIdByModuleId(moduleId);\r
+      if (packageId != null) {\r
+          return getPackageFarGuid(packageId);\r
+      }\r
+      else {\r
+        return null;\r
+      }\r
+    }\r
     /**\r
      Get all modules' paths from one package\r
      \r
@@ -159,7 +291,79 @@ public class WorkspaceTools {
         Tools.sortPackages(vPackageList, DataType.SORT_TYPE_ASCENDING);\r
         return vPackageList;\r
     }\r
+\r
+    public Vector<FarIdentification> getAllFars() {\r
+      openFrameworkDb();\r
+      Vector<FarIdentification> v = new Vector<FarIdentification>();\r
+      for (int index = 0; index < fdb.getFarList().getFilenameList().size(); index++) {\r
+        DbPathAndFilename item = fdb.getFarList().getFilenameList().get(index);\r
+        FarIdentification far = new FarIdentification(item.getFarGuid(), item.getMd5Sum(), item.getStringValue());\r
+        v.addElement(far);\r
+      }\r
+      return v;\r
+    }\r
     \r
+    public Vector<PackageIdentification> getPackagesByFar(FarIdentification far) {\r
+      Identification id = null;\r
+      openFrameworkDb();\r
+      Vector<PackageIdentification> v = new Vector<PackageIdentification>();\r
+      \r
+      for (int index = 0; index < fdb.getPackageList().getFilenameList().size(); index++) {\r
+        DbPathAndFilename item = fdb.getPackageList().getFilenameArray(index);\r
+        String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
+                      + item.getStringValue();\r
+        path = Tools.convertPathToCurrentOsType(path);\r
+        \r
+        if (item.getFarGuid() != null && item.getFarGuid().equalsIgnoreCase(far.getGuid())) {\r
+        \r
+          try {\r
+            id = getId(path, OpenFile.openSpdFile(path));\r
+            v.addElement(new PackageIdentification(id));\r
+          } catch (IOException e) {\r
+            Log.err("Open Package Surface Area " + path, e.getMessage());\r
+            e.printStackTrace();\r
+          } catch (XmlException e) {\r
+            Log.err("Open Package Surface Area " + path, e.getMessage());\r
+            e.printStackTrace();\r
+          } catch (Exception e) {\r
+            Log.err("Open Package Surface Area " + path, "Invalid file type");\r
+            e.printStackTrace();\r
+          }\r
+        }\r
+      }\r
+      return v;\r
+    }\r
+    \r
+    public Vector<PlatformIdentification> getPlatformsByFar(FarIdentification far) {\r
+      Identification id = null;\r
+      openFrameworkDb();\r
+      Vector<PlatformIdentification> v = new Vector<PlatformIdentification>();\r
+      \r
+      for (int index = 0; index < fdb.getPlatformList().getFilenameList().size(); index++) {\r
+        DbPathAndFilename item = fdb.getPlatformList().getFilenameArray(index);\r
+        String path = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR\r
+                      + item.getStringValue();\r
+        path = Tools.convertPathToCurrentOsType(path);\r
+        \r
+        if (item.getFarGuid() != null && item.getFarGuid().equalsIgnoreCase(far.getGuid())) {\r
+          try {\r
+            id = getId(path, OpenFile.openFpdFile(path));\r
+            v.addElement(new PlatformIdentification(id));\r
+          } catch (IOException e) {\r
+            Log.err("Open Platform Surface Area " + path, e.getMessage());\r
+            e.printStackTrace();\r
+          } catch (XmlException e) {\r
+            Log.err("Open Platform Surface Area " + path, e.getMessage());\r
+            e.printStackTrace();\r
+          } catch (Exception e) {\r
+            Log.err("Open Platform Surface Area " + path, "Invalid file type");\r
+            e.printStackTrace();\r
+          }\r
+        }\r
+    }\r
+      return v;\r
+    }\r
+\r
     /**\r
      Get all module basic information from a package\r
     \r