2 Java class DbFileContents is used to deal with FrameworkDatabase.db file cotents.
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.packaging
;
16 import java
.io
.IOException
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
19 import java
.util
.ListIterator
;
20 import java
.util
.Date
;
21 import java
.text
.SimpleDateFormat
;
23 import org
.apache
.xmlbeans
.XmlCursor
;
24 import org
.apache
.xmlbeans
.XmlObject
;
25 import org
.apache
.xmlbeans
.XmlOptions
;
27 import org
.tianocore
.*;
30 This class provides methods for add, remove, query FrameworkDatabase.db file.
32 @since PackageEditor 1.0
34 public class DbFileContents
{
37 /// return values for various conditions.
39 static final int BASE_PACKAGE_NOT_INSTALLED
= 1;
41 static final int VERSION_NOT_EQUAL
= 2;
43 static final int GUID_NOT_EQUAL
= 3;
45 static final int SAME_ALL
= 4;
47 private File dbFile
= null;
49 private FrameworkDatabaseDocument fdd
= null;
51 private FrameworkDatabaseDocument
.FrameworkDatabase fddRoot
= null;
53 private PackageListDocument
.PackageList pkgList
= null;
55 public DbFileContents() {
57 // TODO Auto-generated constructor stub
61 Parse file f, store its xml data in fdd, store root xml element in fddRoot.
63 @param f DB file to parse
65 public DbFileContents(File f
) {
69 fdd
= ((FrameworkDatabaseDocument
) XmlObject
.Factory
.parse(dbFile
));
71 fddRoot
= fdd
.getFrameworkDatabase();
72 } catch (Exception e
) {
73 System
.out
.println(e
.toString());
78 Generate the Package element in FrameworkDatabase.db
80 @param baseName Base name of package
81 @param ver Version of package
82 @param guid GUID of package
83 @param path Where the package installed
84 @param installDate When the package installed
86 public void genPackage (String baseName
, String ver
, String guid
, String path
, String installDate
) {
87 if (getPkgList() == null) {
88 pkgList
= fddRoot
.addNewPackageList();
90 PackageListDocument
.PackageList
.Package p
= pkgList
.addNewPackage();
91 p
.addNewPackageName().setStringValue(baseName
);
92 p
.addNewGuid().setStringValue(guid
);
94 p
.addNewPath().setStringValue(path
);
95 p
.addNewInstalledDate().setStringValue(installDate
);
101 @return PackageListDocument.PackageList
103 public PackageListDocument
.PackageList
getPkgList() {
104 if (pkgList
== null) {
105 pkgList
= fddRoot
.getPackageList();
111 Remove PackageList and all elements under it.
113 public void removePackageList() {
114 XmlObject o
= fddRoot
.getPackageList();
117 XmlCursor cursor
= o
.newCursor();
121 Get the number of Package elements.
125 public int getPackageCount () {
126 return fddRoot
.getPackageList().getPackageList().size();
130 Get all Package contents into String array
132 @param pkg Two dimentional array to store Package info.
134 public void getPackageList(String
[][] pkg
) {
135 List
<PackageListDocument
.PackageList
.Package
> l
= fddRoot
.getPackageList().getPackageList();
137 ListIterator li
= l
.listIterator();
138 while (li
.hasNext()) {
139 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) li
141 if (p
.getPackageNameArray(0)!= null) {
142 pkg
[i
][0] = p
.getPackageNameArray(0).getStringValue();
145 pkg
[i
][1] = p
.getVersionArray(0);
147 if (p
.getGuidArray(0) != null) {
148 pkg
[i
][2] = p
.getGuidArray(0).getStringValue();
150 if (p
.getPathArray(0) != null) {
151 pkg
[i
][3] = p
.getPathArray(0).getStringValue();
153 if (p
.getInstalledDateArray(0) != null) {
154 pkg
[i
][4] = p
.getInstalledDateArray(0);
160 Check whether destDir has been used by one Package
162 @param destDir The directory to check.
163 @retval <1> destDir has been used
164 @retval <0> destDir has not been used
167 public int checkDir(String destDir
) {
168 List
<PackageListDocument
.PackageList
.Package
> lp
= fddRoot
.getPackageList().getPackageList();
170 ListIterator lpi
= lp
.listIterator();
171 while (lpi
.hasNext()) {
172 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) lpi
.next();
173 if (p
.getPathArray(0).getStringValue().equals(destDir
)) {
181 Find the package info. and store results into list of same base name or list
184 @param base The base name of package
185 @param version The version of package
186 @param guid the GUID of package
187 @param lpSameBase The list to store package info with the same base name with "base"
188 @param lpSameVersion The list to store package info from lpSameBase and same version
190 @retval <0> No package installed has base name "base"
191 @retval <VERSION_NOT_EQUAL> At least one package installed with "base" but no "version"
192 @retval <GUID_NOT_EQUAL> At least one package installed with "base" and "version" but no "guid"
193 @retval <SAME_ALL> One installed package has the same base, version and guid
196 public int query(String base
, String version
, String guid
,
197 List
<PackageListDocument
.PackageList
.Package
> lpSameBase
,
198 List
<PackageListDocument
.PackageList
.Package
> lpSameVersion
) {
200 List
<PackageListDocument
.PackageList
.Package
> lp
= fddRoot
.getPackageList().getPackageList();
202 ListIterator lpi
= lp
.listIterator();
203 while (lpi
.hasNext()) {
204 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) lpi
.next();
205 if (p
.getPackageNameArray(0).getStringValue().equals(base
)) {
210 if (lpSameBase
.size() == 0) {
214 for (ListIterator li
= lpSameBase
.listIterator(); li
.hasNext();) {
215 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) li
.next();
216 if (p
.getVersionArray(0).equals(version
)) {
217 lpSameVersion
.add(p
);
221 if (lpSameVersion
.size() == 0) {
222 return VERSION_NOT_EQUAL
;
225 for (ListIterator li
= lpSameVersion
.listIterator(); li
.hasNext();) {
226 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) li
.next();
227 if (!p
.getGuidArray(0).getStringValue().equals(guid
)) {
228 return GUID_NOT_EQUAL
;
237 Update package info (name, version, guid) with installDir, newVer, newGuid.
238 And update install date with current date. if no package info available, add
241 @param name Original base name
242 @param version Original version
243 @param guid Original GUID
244 @param installDir original path
245 @param newVer Version value of package to be installed
246 @param newGuid GUID value of package to be installed
247 @throws IOException Exception during file operation
249 public void updatePkgInfo(String name
, String version
, String guid
, String installDir
, String newVer
, String newGuid
)
251 List
<PackageListDocument
.PackageList
.Package
> lp
= fddRoot
.getPackageList().getPackageList();
253 ListIterator lpi
= lp
.listIterator();
254 while (lpi
.hasNext()) {
255 PackageListDocument
.PackageList
.Package p
= (PackageListDocument
.PackageList
.Package
) lpi
.next();
256 if (p
.getPackageNameArray(0).getStringValue().equals(name
)) {
257 if (p
.getVersionArray(0).equals(version
)) {
258 if (p
.getGuidArray(0).getStringValue().equals(guid
)) {
259 p
.setVersionArray(0, newVer
);
260 p
.getGuidArray(0).setStringValue(newGuid
);
261 p
.getPathArray(0).setStringValue(installDir
);
262 SimpleDateFormat format
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
263 Date date
= new Date();
264 p
.setInstalledDateArray(0, format
.format(date
));
273 addNewPkgInfo(name
, newVer
, newGuid
, installDir
);
277 Add one new package entry.
279 @param name Package base name
280 @param version Package version
281 @param guid Package Guid
282 @param installDir Package path
283 @throws IOException Exception during file operation
285 public void addNewPkgInfo(String name
, String version
, String guid
, String installDir
) throws IOException
{
287 PackageListDocument
.PackageList
.Package p
= fddRoot
.getPackageList().addNewPackage();
288 p
.addNewPackageName().setStringValue(name
);
289 p
.addNewGuid().setStringValue(guid
);
290 p
.addNewVersion().setStringValue(version
);
291 p
.addNewPath().setStringValue(installDir
+ "/");
293 SimpleDateFormat format
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
294 Date date
= new Date();
295 p
.addNewInstalledDate().setStringValue(format
.format(date
));
300 Save the fdd into file with format options
302 public void saveAs() {
303 XmlOptions options
= new XmlOptions();
305 options
.setCharacterEncoding("UTF-8");
306 options
.setSavePrettyPrint();
307 options
.setSavePrettyPrintIndent(2);
309 fdd
.save(dbFile
, options
);
310 } catch (IOException e
) {