]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PackageEditor/src/org/tianocore/packaging/DbFileContents.java
Initial import.
[mirror_edk2.git] / Tools / Source / PackageEditor / src / org / tianocore / packaging / DbFileContents.java
1 /** @file
2 Java class DbFileContents is used to deal with FrameworkDatabase.db file cotents.
3
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
9
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.
12 **/
13 package org.tianocore.packaging;
14
15 import java.io.File;
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;
22
23 import org.apache.xmlbeans.XmlCursor;
24 import org.apache.xmlbeans.XmlObject;
25 import org.apache.xmlbeans.XmlOptions;
26
27 import org.tianocore.*;
28
29 /**
30 This class provides methods for add, remove, query FrameworkDatabase.db file.
31
32 @since PackageEditor 1.0
33 **/
34 public class DbFileContents {
35
36 ///
37 /// return values for various conditions.
38 ///
39 static final int BASE_PACKAGE_NOT_INSTALLED = 1;
40
41 static final int VERSION_NOT_EQUAL = 2;
42
43 static final int GUID_NOT_EQUAL = 3;
44
45 static final int SAME_ALL = 4;
46
47 private File dbFile = null;
48
49 private FrameworkDatabaseDocument fdd = null;
50
51 private FrameworkDatabaseDocument.FrameworkDatabase fddRoot = null;
52
53 private PackageListDocument.PackageList pkgList = null;
54
55 public DbFileContents() {
56 super();
57 // TODO Auto-generated constructor stub
58 }
59
60 /**
61 Parse file f, store its xml data in fdd, store root xml element in fddRoot.
62
63 @param f DB file to parse
64 **/
65 public DbFileContents(File f) {
66 try {
67 dbFile = f;
68 if (fdd == null) {
69 fdd = ((FrameworkDatabaseDocument) XmlObject.Factory.parse(dbFile));
70 }
71 fddRoot = fdd.getFrameworkDatabase();
72 } catch (Exception e) {
73 System.out.println(e.toString());
74 }
75 }
76
77 /**
78 Generate the Package element in FrameworkDatabase.db
79
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
85 **/
86 public void genPackage (String baseName, String ver, String guid, String path, String installDate) {
87 if (getPkgList() == null) {
88 pkgList = fddRoot.addNewPackageList();
89 }
90 PackageListDocument.PackageList.Package p = pkgList.addNewPackage();
91 p.addNewPackageName().setStringValue(baseName);
92 p.addNewGuid().setStringValue(guid);
93 p.addVersion(ver);
94 p.addNewPath().setStringValue(path);
95 p.addNewInstalledDate().setStringValue(installDate);
96 }
97
98 /**
99 Get PackageList
100
101 @return PackageListDocument.PackageList
102 **/
103 public PackageListDocument.PackageList getPkgList() {
104 if (pkgList == null) {
105 pkgList = fddRoot.getPackageList();
106 }
107 return pkgList;
108 }
109
110 /**
111 Remove PackageList and all elements under it.
112 **/
113 public void removePackageList() {
114 XmlObject o = fddRoot.getPackageList();
115 if (o == null)
116 return;
117 XmlCursor cursor = o.newCursor();
118 cursor.removeXml();
119 }
120 /**
121 Get the number of Package elements.
122
123 @return int
124 **/
125 public int getPackageCount () {
126 return fddRoot.getPackageList().getPackageList().size();
127 }
128
129 /**
130 Get all Package contents into String array
131
132 @param pkg Two dimentional array to store Package info.
133 **/
134 public void getPackageList(String[][] pkg) {
135 List<PackageListDocument.PackageList.Package> l = fddRoot.getPackageList().getPackageList();
136 int i = 0;
137 ListIterator li = l.listIterator();
138 while (li.hasNext()) {
139 PackageListDocument.PackageList.Package p = (PackageListDocument.PackageList.Package) li
140 .next();
141 if (p.getPackageNameArray(0)!= null) {
142 pkg[i][0] = p.getPackageNameArray(0).getStringValue();
143 }
144
145 pkg[i][1] = p.getVersionArray(0);
146
147 if (p.getGuidArray(0) != null) {
148 pkg[i][2] = p.getGuidArray(0).getStringValue();
149 }
150 if (p.getPathArray(0) != null) {
151 pkg[i][3] = p.getPathArray(0).getStringValue();
152 }
153 if (p.getInstalledDateArray(0) != null) {
154 pkg[i][4] = p.getInstalledDateArray(0);
155 }
156 i++;
157 }
158 }
159 /**
160 Check whether destDir has been used by one Package
161
162 @param destDir The directory to check.
163 @retval <1> destDir has been used
164 @retval <0> destDir has not been used
165 @return int
166 **/
167 public int checkDir(String destDir) {
168 List<PackageListDocument.PackageList.Package> lp = fddRoot.getPackageList().getPackageList();
169
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)) {
174 return 1;
175 }
176 }
177 return 0;
178 }
179
180 /**
181 Find the package info. and store results into list of same base name or list
182 of same version.
183
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
189 with "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
194 @return int
195 **/
196 public int query(String base, String version, String guid,
197 List<PackageListDocument.PackageList.Package> lpSameBase,
198 List<PackageListDocument.PackageList.Package> lpSameVersion) {
199
200 List<PackageListDocument.PackageList.Package> lp = fddRoot.getPackageList().getPackageList();
201
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)) {
206 lpSameBase.add(p);
207 }
208 }
209
210 if (lpSameBase.size() == 0) {
211 return 0;
212 }
213
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);
218 }
219 }
220
221 if (lpSameVersion.size() == 0) {
222 return VERSION_NOT_EQUAL;
223 }
224
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;
229 }
230 }
231
232 return SAME_ALL;
233
234 }
235
236 /**
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
239 a new entry.
240
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
248 **/
249 public void updatePkgInfo(String name, String version, String guid, String installDir, String newVer, String newGuid)
250 throws IOException {
251 List<PackageListDocument.PackageList.Package> lp = fddRoot.getPackageList().getPackageList();
252
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));
265 saveAs();
266 return;
267
268 }
269 }
270 }
271 }
272
273 addNewPkgInfo(name, newVer, newGuid, installDir);
274 }
275
276 /**
277 Add one new package entry.
278
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
284 **/
285 public void addNewPkgInfo(String name, String version, String guid, String installDir) throws IOException {
286
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);
292
293 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
294 Date date = new Date();
295 p.addNewInstalledDate().setStringValue(format.format(date));
296 saveAs();
297 }
298
299 /**
300 Save the fdd into file with format options
301 **/
302 public void saveAs() {
303 XmlOptions options = new XmlOptions();
304
305 options.setCharacterEncoding("UTF-8");
306 options.setSavePrettyPrint();
307 options.setSavePrettyPrintIndent(2);
308 try {
309 fdd.save(dbFile, options);
310 } catch (IOException e) {
311 e.printStackTrace();
312 }
313 }
314
315 }