]>
Commit | Line | Data |
---|---|---|
5a24e806 | 1 | /** @file\r |
2 | \r | |
3 | The file is used to create Far's mainfest file\r | |
4 | \r | |
5 | Copyright (c) 2006, Intel Corporation\r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | package org.tianocore.frameworkwizard.far;\r | |
16 | \r | |
17 | import java.io.File;\r | |
18 | import java.io.FileInputStream;\r | |
19 | import java.io.IOException;\r | |
20 | import java.io.InputStream;\r | |
21 | import java.util.ArrayList;\r | |
22 | import java.util.Iterator;\r | |
23 | import java.util.LinkedHashSet;\r | |
24 | import java.util.List;\r | |
25 | import java.util.Set;\r | |
26 | import java.util.regex.Matcher;\r | |
27 | import java.util.regex.Pattern;\r | |
28 | \r | |
29 | import javax.xml.parsers.DocumentBuilder;\r | |
30 | import javax.xml.parsers.DocumentBuilderFactory;\r | |
31 | import javax.xml.transform.OutputKeys;\r | |
32 | import javax.xml.transform.Result;\r | |
33 | import javax.xml.transform.Source;\r | |
34 | import javax.xml.transform.Transformer;\r | |
35 | import javax.xml.transform.TransformerFactory;\r | |
36 | import javax.xml.transform.dom.DOMSource;\r | |
37 | import javax.xml.transform.stream.StreamResult;\r | |
38 | \r | |
39 | import org.apache.xmlbeans.XmlException;\r | |
40 | import org.tianocore.frameworkwizard.common.Tools;\r | |
41 | import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r | |
42 | import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r | |
43 | import org.tianocore.frameworkwizard.workspace.Workspace;\r | |
44 | import org.w3c.dom.Document;\r | |
45 | import org.w3c.dom.Element;\r | |
46 | import org.w3c.dom.NamedNodeMap;\r | |
47 | import org.w3c.dom.Node;\r | |
48 | import org.w3c.dom.NodeList;\r | |
49 | \r | |
50 | public class Mainfest implements ManifestInterface {\r | |
51 | // /\r | |
52 | // / mainfest document\r | |
53 | // /\r | |
54 | Document mainfestDoc = null;\r | |
55 | \r | |
56 | // /\r | |
57 | // / Mainfest file element name\r | |
58 | // /\r | |
59 | final static String mfFileName = "FarMainfest.MF";\r | |
5a24e806 | 60 | //\r |
61 | // Header\r | |
62 | //\r | |
63 | final static String farHeader = "FarHeader";\r | |
64 | \r | |
65 | final static String farHeader_FarName = "FarName";\r | |
66 | \r | |
67 | final static String farHeader_Abstract = "Abstract";\r | |
68 | \r | |
69 | final static String farHeader_Description = "Description";\r | |
70 | \r | |
71 | final static String farHeader_CopyRight = "CopyRight";\r | |
72 | \r | |
73 | final static String farHeader_License = "Licese";\r | |
74 | \r | |
75 | final static String farHeader_Specification = "Specification";\r | |
76 | \r | |
77 | //\r | |
78 | // Package list\r | |
79 | //\r | |
80 | final static String farPackageList = "FarPackageList";\r | |
81 | \r | |
82 | final static String farPlatformList = "FarPlatformList";\r | |
83 | \r | |
84 | final static String contents = "Contents";\r | |
85 | \r | |
86 | final static String userExtensions = "UserExtensions";\r | |
87 | \r | |
88 | //\r | |
89 | // Common node name\r | |
90 | //\r | |
91 | \r | |
92 | final static String guidValue = "GuidValue";\r | |
93 | \r | |
94 | final static String version = "Version";\r | |
95 | \r | |
96 | //\r | |
97 | // FarPackage node name\r | |
98 | //\r | |
99 | final static String farPackageList_FarPackage = "FarPackage";\r | |
100 | \r | |
101 | final static String farPackage_FarfileName = "FarFileName";\r | |
102 | \r | |
103 | final static String farPackage_DefaultPath = "DefaultPath";\r | |
104 | \r | |
105 | final static String farPlatformList_FarPlatform = "FarPlatform";\r | |
106 | \r | |
107 | final static String farFileName_FarGuid = "FarGuid";\r | |
108 | \r | |
109 | final static String farFileName_Md5sum = "Md5sum";\r | |
110 | \r | |
111 | //\r | |
112 | // mainfest header information.\r | |
113 | //\r | |
114 | FarHeader fhInfo = new FarHeader();\r | |
115 | \r | |
116 | //\r | |
117 | // FarPackage list\r | |
118 | //\r | |
119 | List<FarPackage> fPkgList = new ArrayList<FarPackage>();\r | |
120 | \r | |
121 | //\r | |
122 | // FarPlatform list\r | |
123 | //\r | |
124 | List<FarPlatformItem> fPlfList = new ArrayList<FarPlatformItem>();\r | |
125 | \r | |
126 | Set<File> files = new LinkedHashSet<File>();\r | |
127 | \r | |
128 | //\r | |
129 | // Mainfest file\r | |
130 | //\r | |
131 | File mfFile = null;\r | |
132 | \r | |
133 | public FarHeader getHeader() {\r | |
1898a0c3 | 134 | return fhInfo;\r |
5a24e806 | 135 | }\r |
1898a0c3 | 136 | \r |
5a24e806 | 137 | public Mainfest() throws Exception {\r |
138 | DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();\r | |
139 | DocumentBuilder dombuilder = domfac.newDocumentBuilder();\r | |
140 | Document document = dombuilder.newDocument();\r | |
141 | this.mainfestDoc = document;\r | |
142 | \r | |
143 | }\r | |
144 | \r | |
145 | public Mainfest(InputStream mainfestInputStream) throws Exception {\r | |
146 | DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();\r | |
147 | try {\r | |
148 | if (mainfestInputStream != null) {\r | |
149 | DocumentBuilder dombuilder = domfac.newDocumentBuilder();\r | |
150 | this.mainfestDoc = dombuilder.parse(mainfestInputStream);\r | |
151 | parseMainfest();\r | |
152 | }\r | |
153 | \r | |
154 | } catch (Exception e) {\r | |
155 | //\r | |
156 | // Will change to throw Wizader exception.\r | |
157 | //\r | |
158 | throw new Exception(e.getMessage());\r | |
159 | }\r | |
160 | }\r | |
161 | \r | |
162 | public void setFarHeader(FarHeader fHeader) {\r | |
163 | this.fhInfo = fHeader;\r | |
164 | }\r | |
165 | \r | |
1898a0c3 | 166 | public void createManifest(List<PackageIdentification> pkgList,\r |
167 | List<PlatformIdentification> plfList, Set<String> fileFilter) throws Exception {\r | |
5a24e806 | 168 | \r |
169 | //\r | |
170 | // Add Package and it's contents to FarPackageList.\r | |
171 | //\r | |
172 | Iterator<PackageIdentification> pkgItem = pkgList.iterator();\r | |
173 | while (pkgItem.hasNext()) {\r | |
174 | PackageIdentification pkgId = pkgItem.next();\r | |
175 | \r | |
176 | //\r | |
177 | // Add package and it's contents to <FarPackageList>.\r | |
178 | //\r | |
179 | addPkgToPkgList(pkgId, fileFilter);\r | |
180 | }\r | |
181 | \r | |
182 | //\r | |
183 | // Add FarPlatform to this.farPlatformList.\r | |
184 | //\r | |
185 | Iterator<PlatformIdentification> plfItem = plfList.iterator();\r | |
186 | while (plfItem.hasNext()) {\r | |
187 | PlatformIdentification plfId = plfItem.next();\r | |
188 | \r | |
189 | //\r | |
190 | // Add platform to Mainifest.\r | |
191 | //\r | |
192 | addPlatformIdToFarPlatformItemList(plfId);\r | |
193 | }\r | |
194 | }\r | |
195 | \r | |
196 | public void setManifestFile(File mainfestFile) throws Exception {\r | |
197 | DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();\r | |
198 | DocumentBuilder dombuilder = domfac.newDocumentBuilder();\r | |
199 | InputStream is = new FileInputStream(mainfestFile);\r | |
200 | this.mainfestDoc = dombuilder.parse(is);\r | |
201 | }\r | |
202 | \r | |
203 | public void addPkgToPkgList(PackageIdentification packageId, Set<String> fileFilter) throws Exception {\r | |
204 | files.add(packageId.getSpdFile());\r | |
1898a0c3 | 205 | \r |
5a24e806 | 206 | FarPackage farPackage = new FarPackage();\r |
207 | //\r | |
208 | // Add SPD file to FarPackage\r | |
209 | //\r | |
210 | File spdFile = new File(packageId.getPath());\r | |
211 | \r | |
212 | FarFileItem ffItem = new FarFileItem(spdFile.getName(), FarMd5.md5(spdFile));\r | |
213 | farPackage.setFarFile(ffItem);\r | |
1898a0c3 | 214 | \r |
5a24e806 | 215 | //\r |
216 | // Add package guid value.\r | |
217 | //\r | |
218 | farPackage.setGuidValue(packageId.getGuid());\r | |
1898a0c3 | 219 | \r |
5a24e806 | 220 | //\r |
221 | // Add package version\r | |
222 | //\r | |
223 | farPackage.setVersion(packageId.getVersion());\r | |
1898a0c3 | 224 | \r |
5a24e806 | 225 | //\r |
226 | // Add DefaultPat: Package absoulte path - Workspace absolut\r | |
227 | // path.\r | |
228 | //\r | |
229 | String pkgDir = Tools.getFilePathOnly(packageId.getPath());\r | |
230 | File rootDir = new File(pkgDir);\r | |
231 | farPackage.setDefaultPath(Tools.getRelativePath(rootDir.getPath(), Workspace.getCurrentWorkspace()));\r | |
232 | \r | |
233 | //\r | |
234 | // Get package root dir contains.\r | |
235 | //\r | |
236 | Set<File> fileSet = new LinkedHashSet<File>();\r | |
237 | Set<File> fpdFileSet = new LinkedHashSet<File>();\r | |
238 | \r | |
239 | //\r | |
240 | // Get all files and fpd files\r | |
241 | //\r | |
242 | recursiveDirectory(fileSet, fpdFileSet, rootDir, fileFilter);\r | |
243 | \r | |
244 | //\r | |
245 | // Remove current package's SPD file\r | |
246 | //\r | |
247 | fileSet.remove(packageId.getSpdFile());\r | |
1898a0c3 | 248 | \r |
5a24e806 | 249 | files.addAll(fileSet);\r |
250 | \r | |
251 | Iterator<File> iter = fileSet.iterator();\r | |
252 | List<FarFileItem> contents = new ArrayList<FarFileItem>();\r | |
253 | while (iter.hasNext()) {\r | |
254 | File normalFile = iter.next();\r | |
255 | String fileRelativePath = Tools.getRelativePath(normalFile.getPath(), pkgDir);\r | |
256 | ffItem = new FarFileItem(fileRelativePath, FarMd5.md5(normalFile));\r | |
257 | contents.add(ffItem);\r | |
258 | }\r | |
1898a0c3 | 259 | \r |
5a24e806 | 260 | farPackage.setContentList(contents);\r |
1898a0c3 | 261 | \r |
262 | // List<FarPlatformItem> fpfList = new ArrayList<FarPlatformItem>();\r | |
263 | //\r | |
264 | // iter = fpdFileSet.iterator();\r | |
265 | //\r | |
266 | // while (iter.hasNext()) {\r | |
267 | // File fpdFile = iter.next();\r | |
268 | // PlatformIdentification platformId = new PlatformIdentification(wsTool\r | |
269 | // .getId(fpdFile.getPath(), OpenFile.openFpdFile(fpdFile\r | |
270 | // .getPath())));\r | |
271 | // addPlatformIdToFarPlatformItemList(fpfList, platformId);\r | |
272 | // }\r | |
273 | // farPackage.setFarPlatformList(fpfList);\r | |
5a24e806 | 274 | fPkgList.add(farPackage);\r |
275 | }\r | |
276 | \r | |
1898a0c3 | 277 | private void recursiveDirectory(Set<File> files, Set<File> fpds, File dir,\r |
278 | Set<String> fileFilter) {\r | |
5a24e806 | 279 | if (isFilter(dir, fileFilter)) {\r |
280 | return;\r | |
281 | }\r | |
282 | File[] allFilesInDir = dir.listFiles();\r | |
283 | for (int i = 0; i < allFilesInDir.length; i++) {\r | |
284 | if (allFilesInDir[i].isFile()) {\r | |
285 | if (isFilter(allFilesInDir[i], fileFilter)) {\r | |
286 | continue;\r | |
287 | }\r | |
1898a0c3 | 288 | // if (allFilesInDir[i].getPath().toLowerCase().endsWith(".fpd")) {\r |
289 | // fpds.add(allFilesInDir[i]);\r | |
290 | // } else {\r | |
291 | files.add(allFilesInDir[i]);\r | |
292 | // }\r | |
5a24e806 | 293 | } else {\r |
294 | recursiveDirectory(files, fpds, allFilesInDir[i], fileFilter);\r | |
295 | }\r | |
296 | }\r | |
297 | }\r | |
298 | \r | |
299 | public void addPlatformIdToFarPlatformItemList(PlatformIdentification platformId) throws Exception {\r | |
300 | files.add(platformId.getFpdFile());\r | |
1898a0c3 | 301 | \r |
5a24e806 | 302 | FarPlatformItem fpfItem = new FarPlatformItem();\r |
303 | FarFileItem ffItem;\r | |
304 | String fpfPath = platformId.getPath();\r | |
305 | File fpfFile = new File(fpfPath);\r | |
306 | //\r | |
307 | // Add farFileName\r | |
308 | //\r | |
1898a0c3 | 309 | ffItem = new FarFileItem(Tools.getRelativePath(fpfFile.getPath(),Workspace.getCurrentWorkspace()), FarMd5.md5(fpfFile));\r |
5a24e806 | 310 | fpfItem.setFarFile(ffItem);\r |
311 | \r | |
312 | //\r | |
313 | // Add guid value\r | |
314 | //\r | |
315 | fpfItem.setGuidValue(platformId.getGuid());\r | |
316 | \r | |
317 | //\r | |
318 | // Add version\r | |
319 | //\r | |
320 | fpfItem.setVersion(platformId.getVersion());\r | |
321 | \r | |
322 | fPlfList.add(fpfItem);\r | |
323 | }\r | |
324 | \r | |
325 | /**\r | |
326 | * \r | |
327 | * @param\r | |
328 | * @return Set<PackageIdentification> list of PackageIdentification.\r | |
329 | */\r | |
330 | public List<PackageIdentification> getPackageList() throws Exception {\r | |
331 | //\r | |
332 | // PackageIdentification set.\r | |
333 | //\r | |
334 | List<PackageIdentification> pkgList = new ArrayList<PackageIdentification>();\r | |
335 | //\r | |
336 | // \r | |
337 | //\r | |
338 | Iterator pkgItem = this.fPkgList.iterator();\r | |
339 | while (pkgItem.hasNext()) {\r | |
340 | FarPackage fPkg = (FarPackage) pkgItem.next();\r | |
341 | //\r | |
342 | // Get package information from SPD file and create a package\r | |
343 | // identification.\r | |
344 | //\r | |
345 | \r | |
1898a0c3 | 346 | PackageIdentification pkgId = new PackageIdentification(fPkg.getFarFile().getRelativeFilename(), fPkg.getGuidValue(), fPkg.getVersion());\r |
347 | pkgId.setPath(Workspace.getCurrentWorkspace() + File.separatorChar + fPkg.getDefaultPath() + File.separatorChar + fPkg.getFarFile().getRelativeFilename());\r | |
348 | // wsTool.getId(\r | |
349 | // Workspace.getCurrentWorkspace() + File.separatorChar\r | |
350 | // + fPkg.getDefaultPath(), OpenFile\r | |
351 | // .openFpdFile(Workspace.getCurrentWorkspace()\r | |
352 | // + File.separatorChar\r | |
353 | // + fPkg.getDefaultPath()\r | |
354 | // + File.separatorChar\r | |
355 | // + fPkg.getFarFile().getRelativeFilename()));\r | |
5a24e806 | 356 | pkgList.add(pkgId);\r |
357 | }\r | |
358 | return pkgList;\r | |
359 | }\r | |
360 | \r | |
361 | /**\r | |
362 | * \r | |
363 | */\r | |
1898a0c3 | 364 | public List<PlatformIdentification> getPlatformList() throws Exception,\r |
365 | IOException, XmlException {\r | |
5a24e806 | 366 | //\r |
367 | // PlatformIdentification set.\r | |
368 | //\r | |
369 | List<PlatformIdentification> plfList = new ArrayList<PlatformIdentification>();\r | |
370 | Iterator plfItem = this.fPlfList.iterator();\r | |
371 | while (plfItem.hasNext()) {\r | |
372 | FarPlatformItem fpfItem = (FarPlatformItem) plfItem.next();\r | |
1898a0c3 | 373 | File file = new File(Workspace.getCurrentWorkspace()\r |
374 | + File.separatorChar \r | |
375 | + fpfItem.getFarFile().getRelativeFilename());\r | |
5a24e806 | 376 | //\r |
377 | // Set platformIdentificaiton's path as absolutly path (include\r | |
378 | // workspace and FPD relatively path)\r | |
379 | //\r | |
1898a0c3 | 380 | PlatformIdentification plfId = new PlatformIdentification (fpfItem.getFarFile().getRelativeFilename(), fpfItem.getGuidValue(),fpfItem.getVersion(),\r |
381 | file.getPath());\r | |
382 | \r | |
383 | // (PlatformIdentification) wsTool\r | |
384 | // .getId(file.getParent(), OpenFile.openFpdFile(Workspace\r | |
385 | // .getCurrentWorkspace()\r | |
386 | // + File.separatorChar\r | |
387 | // + fpfItem.getFarFile().getRelativeFilename()));\r | |
5a24e806 | 388 | plfList.add(plfId);\r |
389 | }\r | |
390 | return plfList;\r | |
391 | }\r | |
392 | \r | |
393 | public List<FarFileItem> getPlatformContents(PlatformIdentification platformId) {\r | |
394 | List<FarFileItem> result = new ArrayList<FarFileItem>();\r | |
395 | Iterator<FarPlatformItem> iter = this.fPlfList.iterator();\r | |
1898a0c3 | 396 | \r |
5a24e806 | 397 | while (iter.hasNext()) {\r |
398 | FarPlatformItem item = iter.next();\r | |
399 | if (item.isIdentityPlf(platformId)) {\r | |
400 | FarFileItem farFileItem = item.getFarFile();\r | |
401 | farFileItem.setDefaultPath(farFileItem.getRelativeFilename());\r | |
1898a0c3 | 402 | farFileItem.setRelativeFilename(Tools.getFileNameOnly(farFileItem.getRelativeFilename()));\r |
5a24e806 | 403 | result.add(farFileItem);\r |
1898a0c3 | 404 | break ;\r |
5a24e806 | 405 | }\r |
406 | }\r | |
407 | return result;\r | |
408 | }\r | |
1898a0c3 | 409 | \r |
5a24e806 | 410 | public List<FarFileItem> getPackageContents(PackageIdentification packageId) {\r |
411 | List<FarFileItem> farFileList = new ArrayList<FarFileItem>();\r | |
412 | Iterator pkgItem = this.fPkgList.iterator();\r | |
413 | while (pkgItem.hasNext()) {\r | |
414 | FarPackage pkg = (FarPackage) pkgItem.next();\r | |
415 | if (pkg.isIdentityPkg(packageId)) {\r | |
416 | //\r | |
417 | // Add spd far file to list.\r | |
418 | //\r | |
419 | farFileList.add(pkg.getFarFile());\r | |
420 | //\r | |
421 | // Add all files in contents to list.\r | |
422 | //\r | |
423 | farFileList.addAll(pkg.getContentList());\r | |
424 | //\r | |
425 | // Add all farfiles in <FarPlatformList> to list.\r | |
426 | //\r | |
1898a0c3 | 427 | // List<FarPlatformItem> plfList = pkg.getFarPlatformList();\r |
428 | // Iterator plfItem = plfList.iterator();\r | |
429 | // while (plfItem.hasNext()) {\r | |
430 | // farFileList.add(((FarPlatformItem) plfItem.next())\r | |
431 | // .getFarFile());\r | |
432 | // }\r | |
433 | \r | |
5a24e806 | 434 | Iterator<FarFileItem> ffIter = farFileList.iterator();\r |
1898a0c3 | 435 | while(ffIter.hasNext()){\r |
436 | FarFileItem ffItem = ffIter.next();\r | |
5a24e806 | 437 | ffItem.setDefaultPath(pkg.getDefaultPath() + File.separatorChar + ffItem.getRelativeFilename());\r |
438 | }\r | |
439 | break;\r | |
440 | }\r | |
441 | }\r | |
442 | \r | |
1898a0c3 | 443 | \r |
5a24e806 | 444 | return farFileList;\r |
445 | }\r | |
446 | \r | |
447 | /**\r | |
448 | * \r | |
449 | * @param pkgId\r | |
450 | * @return String: return string represent jar file entry; \r | |
451 | */\r | |
1898a0c3 | 452 | public String[] getPackgeSpd(PackageIdentification pkgId){\r |
5a24e806 | 453 | Iterator pkgItem = this.fPkgList.iterator();\r |
454 | String[] entryStr = new String[2];\r | |
455 | while (pkgItem.hasNext()) {\r | |
456 | FarPackage pkg = (FarPackage) pkgItem.next();\r | |
457 | if (pkg.isIdentityPkg(pkgId)) {\r | |
1898a0c3 | 458 | entryStr[0] = pkg.getFarFile().getRelativeFilename();\r |
459 | entryStr[1] = pkg.getDefaultPath() ;\r | |
5a24e806 | 460 | return entryStr;\r |
461 | }\r | |
462 | }\r | |
463 | return null;\r | |
1898a0c3 | 464 | } \r |
465 | \r | |
5a24e806 | 466 | public List<FarFileItem> getPackageContents() {\r |
467 | //\r | |
468 | // In this farFilelist,all FarFileItem's relativeFileName should be\r | |
469 | // set as absolutely path.\r | |
470 | //\r | |
471 | List<FarFileItem> farFileList = new ArrayList<FarFileItem>();\r | |
472 | Iterator pkgItem = this.fPkgList.iterator();\r | |
473 | FarFileItem ffItem = null;\r | |
474 | \r | |
475 | while (pkgItem.hasNext()) {\r | |
476 | FarPackage pkg = (FarPackage) pkgItem.next();\r | |
477 | \r | |
478 | //\r | |
479 | // Add spd far file to list.\r | |
480 | //\r | |
481 | ffItem = pkg.getFarFile();\r | |
482 | //\r | |
483 | // Set farFileItem relativeFileName = absolutePath + file Name.\r | |
484 | //\r | |
1898a0c3 | 485 | farFileList.add(new FarFileItem(pkg.getDefaultPath()\r |
486 | + File.separatorChar + ffItem.getRelativeFilename(), ffItem\r | |
487 | .getMd5Value()));\r | |
5a24e806 | 488 | //\r |
489 | // Add all files in contents to list.\r | |
490 | //\r | |
491 | Iterator contentsItem = pkg.getContentList().iterator();\r | |
492 | while (contentsItem.hasNext()) {\r | |
493 | ffItem = (FarFileItem) contentsItem.next();\r | |
1898a0c3 | 494 | farFileList.add(new FarFileItem(pkg.getDefaultPath()\r |
495 | + File.separator + ffItem.getRelativeFilename(), ffItem\r | |
496 | .getMd5Value()));\r | |
5a24e806 | 497 | }\r |
498 | //\r | |
499 | // Add all farfiles in <FarPlatformList> to list.\r | |
500 | //\r | |
501 | List<FarPlatformItem> plfList = pkg.getFarPlatformList();\r | |
502 | Iterator plfItem = plfList.iterator();\r | |
503 | while (plfItem.hasNext()) {\r | |
504 | ffItem = ((FarPlatformItem) plfItem.next()).getFarFile();\r | |
1898a0c3 | 505 | farFileList.add(new FarFileItem(pkg.getDefaultPath()\r |
506 | + File.separator + ffItem.getRelativeFilename(), ffItem\r | |
507 | .getMd5Value()));\r | |
5a24e806 | 508 | }\r |
509 | }\r | |
510 | return farFileList;\r | |
511 | }\r | |
512 | \r | |
513 | public String getPackageDefaultPath(PackageIdentification packageId) {\r | |
514 | Iterator pkgItr = this.fPkgList.iterator();\r | |
515 | while (pkgItr.hasNext()) {\r | |
516 | FarPackage farPackage = (FarPackage) pkgItr.next();\r | |
517 | if (farPackage.isIdentityPkg(packageId)) {\r | |
518 | return farPackage.getDefaultPath();\r | |
519 | }\r | |
520 | }\r | |
521 | return null;\r | |
522 | }\r | |
1898a0c3 | 523 | \r |
524 | // public void setPackageInstallPath(PackageIdentification packageId, String path) {\r | |
525 | // Iterator<FarPackage> pkgItr = this.fPkgList.iterator();\r | |
526 | // while (pkgItr.hasNext()) {\r | |
527 | // FarPackage farPackage = pkgItr.next();\r | |
528 | // if (farPackage.isIdentityPkg(packageId)) {\r | |
529 | // farPackage.setDefaultPath(path);\r | |
530 | // return ;\r | |
531 | // }\r | |
532 | // }\r | |
533 | // }\r | |
534 | //\r | |
535 | // public void setPlatformInstallPath(PlatformIdentification platformId, String path) {\r | |
536 | // Iterator<FarPlatformItem> plfItr = this.fPlfList.iterator();\r | |
537 | // while (plfItr.hasNext()) {\r | |
538 | // FarPlatformItem farPlatform = plfItr.next();\r | |
539 | // if (farPlatform.i.isIdentity(platformId)) {\r | |
540 | // farPackage.setDefaultPath(path);\r | |
541 | // return ;\r | |
542 | // }\r | |
543 | // }\r | |
544 | // }\r | |
545 | \r | |
5a24e806 | 546 | public List<FarFileItem> getAllFileItem() {\r |
547 | //\r | |
548 | // The farFileName in this list are all abosulte path.\r | |
549 | //\r | |
550 | List<FarFileItem> ffiList = new ArrayList<FarFileItem>();\r | |
551 | //\r | |
552 | // Add far files in <FarPackageList> to list.\r | |
553 | //\r | |
554 | ffiList = this.getPackageContents();\r | |
555 | \r | |
556 | //\r | |
557 | // Add far files in <FarPlatformList> to list\r | |
558 | //\r | |
1898a0c3 | 559 | NodeList elementList = this.mainfestDoc\r |
560 | .getElementsByTagName(farPlatformList);\r | |
5a24e806 | 561 | for (int i = 0; i < elementList.getLength(); i++) {\r |
562 | //\r | |
563 | // Get <farPlatform> node list.\r | |
564 | //\r | |
565 | Node item = elementList.item(i);\r | |
566 | NodeList plfElements = item.getChildNodes();\r | |
567 | for (int j = 0; j < plfElements.getLength(); j++) {\r | |
568 | //\r | |
569 | // Get each <FarPlatform> content.\r | |
570 | //\r | |
571 | NodeList plfChildNode = plfElements.item(i).getChildNodes();\r | |
572 | Node tempNode = null;\r | |
573 | for (int t = 0; t < plfChildNode.getLength(); t++) {\r | |
574 | tempNode = plfChildNode.item(t);\r | |
575 | //\r | |
576 | // Get child node value and set to platformIdentification.\r | |
577 | //\r | |
1898a0c3 | 578 | if (tempNode.getNodeName().equalsIgnoreCase(\r |
579 | farPackage_FarfileName)) {\r | |
5a24e806 | 580 | NamedNodeMap farAttr = tempNode.getAttributes();\r |
581 | //\r | |
582 | // Change relative path to absolute one\r | |
583 | //\r | |
1898a0c3 | 584 | FarFileItem farFile = new FarFileItem(tempNode\r |
585 | .getTextContent(), farAttr.getNamedItem(\r | |
586 | farFileName_Md5sum).getTextContent());\r | |
5a24e806 | 587 | ffiList.add(farFile);\r |
588 | }\r | |
589 | }\r | |
590 | }\r | |
591 | }\r | |
592 | return ffiList;\r | |
593 | }\r | |
594 | \r | |
595 | public void hibernateToFile() throws Exception {\r | |
596 | //\r | |
597 | // create mainfest root node\r | |
598 | //\r | |
1898a0c3 | 599 | Element rootNode = this.mainfestDoc\r |
600 | .createElement("FrameworkArchiveManifest");\r | |
5a24e806 | 601 | this.mainfestDoc.appendChild(rootNode);\r |
602 | \r | |
603 | //\r | |
604 | // create mainfest header node\r | |
605 | //\r | |
606 | Element headerNode = this.mainfestDoc.createElement(farHeader);\r | |
607 | rootNode.appendChild(headerNode);\r | |
608 | //\r | |
609 | // Add FarHeader to headerNode.\r | |
610 | //\r | |
611 | Element farName = this.mainfestDoc.createElement(farHeader_FarName);\r | |
612 | farName.setTextContent(this.fhInfo.getFarName());\r | |
613 | headerNode.appendChild(farName);\r | |
614 | \r | |
615 | Element gv = this.mainfestDoc.createElement(guidValue);\r | |
616 | gv.setTextContent(this.fhInfo.getGuidValue());\r | |
617 | headerNode.appendChild(gv);\r | |
618 | \r | |
619 | Element ver = this.mainfestDoc.createElement(version);\r | |
620 | ver.setTextContent(this.fhInfo.getVersion());\r | |
621 | headerNode.appendChild(ver);\r | |
622 | \r | |
623 | Element abstra = this.mainfestDoc.createElement(farHeader_Abstract);\r | |
624 | abstra.setTextContent(this.fhInfo.getAbstractStr());\r | |
625 | headerNode.appendChild(abstra);\r | |
626 | \r | |
627 | Element descr = this.mainfestDoc.createElement(farHeader_Description);\r | |
628 | descr.setTextContent(this.fhInfo.getDescription());\r | |
629 | headerNode.appendChild(descr);\r | |
630 | \r | |
631 | Element copyright = this.mainfestDoc.createElement(farHeader_CopyRight);\r | |
632 | copyright.setTextContent(this.fhInfo.getCopyright());\r | |
633 | headerNode.appendChild(copyright);\r | |
634 | \r | |
635 | Element license = this.mainfestDoc.createElement(farHeader_License);\r | |
636 | license.setTextContent(this.fhInfo.getLicense());\r | |
637 | headerNode.appendChild(license);\r | |
638 | \r | |
639 | Element spec = this.mainfestDoc.createElement(farHeader_Specification);\r | |
640 | spec.setTextContent(this.fhInfo.getSpecification());\r | |
641 | System.out.println(this.fhInfo.getSpecification());\r | |
642 | headerNode.appendChild(spec);\r | |
643 | \r | |
644 | //\r | |
645 | // create mainfest FarPackageList node\r | |
646 | //\r | |
647 | Element pkgListNode = this.mainfestDoc.createElement(farPackageList);\r | |
648 | rootNode.appendChild(pkgListNode);\r | |
649 | \r | |
650 | //\r | |
651 | // Save each item in farPackage list to <FarPackage>.\r | |
652 | //\r | |
653 | Iterator pkgItem = this.fPkgList.iterator();\r | |
654 | while (pkgItem.hasNext()) {\r | |
655 | pkgToFarPkgNode(pkgListNode, (FarPackage) pkgItem.next());\r | |
656 | \r | |
657 | }\r | |
658 | \r | |
659 | //\r | |
660 | // create mainfest FarPlatformList node\r | |
661 | //\r | |
662 | Element plfListNode = this.mainfestDoc.createElement(farPlatformList);\r | |
663 | rootNode.appendChild(plfListNode);\r | |
664 | \r | |
665 | //\r | |
666 | // Save farPakcage list info to <FarPackageList> node\r | |
667 | //\r | |
668 | Iterator plfItem = this.fPlfList.iterator();\r | |
669 | while (plfItem.hasNext()) {\r | |
670 | FarPlatformItem plfIterator = (FarPlatformItem) plfItem.next();\r | |
671 | PlfToPlatformNode(plfListNode, plfIterator);\r | |
672 | }\r | |
673 | \r | |
674 | //\r | |
675 | // Write the DOM document to the file\r | |
676 | //\r | |
677 | Transformer xformer = TransformerFactory.newInstance().newTransformer();\r | |
1898a0c3 | 678 | xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",\r |
679 | "2");\r | |
5a24e806 | 680 | xformer.setOutputProperty(OutputKeys.INDENT, "yes");\r |
681 | \r | |
682 | //\r | |
683 | // Prepare the DOM document for writing\r | |
684 | //\r | |
685 | Source source = new DOMSource(this.mainfestDoc);\r | |
686 | //\r | |
687 | // Prepare the output file, get the Mainifest file name from <FarHeader>\r | |
688 | // /<FarName>.\r | |
689 | //\r | |
1898a0c3 | 690 | this.mfFile = new File(Workspace.getCurrentWorkspace()\r |
691 | + File.separatorChar + mfFileName);\r | |
5a24e806 | 692 | //\r |
693 | // generate all directory path\r | |
694 | //\r | |
695 | Result result = new StreamResult(this.mfFile);\r | |
696 | xformer.transform(source, result);\r | |
697 | }\r | |
698 | \r | |
699 | public void pkgToFarPkgNode(Element parentNode, FarPackage pkgItem) {\r | |
1898a0c3 | 700 | Element pkgNode = this.mainfestDoc\r |
701 | .createElement(farPackageList_FarPackage);\r | |
5a24e806 | 702 | //\r |
703 | // Add <FarFileName>\r | |
704 | //\r | |
705 | ffiToFfNode(pkgNode, pkgItem.getFarFile());\r | |
706 | //\r | |
707 | // Add <GuidValue>\r | |
708 | //\r | |
709 | setStrItemToNode(pkgNode, pkgItem.getGuidValue(), guidValue);\r | |
710 | //\r | |
711 | // Add <Version>\r | |
712 | //\r | |
713 | setStrItemToNode(pkgNode, pkgItem.getVersion(), version);\r | |
714 | //\r | |
715 | // Add <DefaultPath>\r | |
716 | //\r | |
1898a0c3 | 717 | setStrItemToNode(pkgNode, pkgItem.getDefaultPath(),\r |
718 | farPackage_DefaultPath);\r | |
5a24e806 | 719 | \r |
720 | //\r | |
721 | // Add <Contents>\r | |
722 | //\r | |
723 | Element contentNode = this.mainfestDoc.createElement(contents);\r | |
724 | Iterator iterator = pkgItem.getContentList().iterator();\r | |
725 | while (iterator.hasNext()) {\r | |
726 | ffiToFfNode(contentNode, (FarFileItem) iterator.next());\r | |
727 | }\r | |
728 | pkgNode.appendChild(contentNode);\r | |
729 | parentNode.appendChild(pkgNode);\r | |
730 | }\r | |
731 | \r | |
732 | public void PlfToPlatformNode(Element parentNode, FarPlatformItem fplItem) {\r | |
1898a0c3 | 733 | Element fplNode = this.mainfestDoc\r |
734 | .createElement(farPlatformList_FarPlatform);\r | |
5a24e806 | 735 | //\r |
736 | // Add <FarFileName>\r | |
737 | //\r | |
738 | ffiToFfNode(fplNode, fplItem.getFarFile());\r | |
739 | //\r | |
740 | // Add <GuidValue>\r | |
741 | //\r | |
742 | setStrItemToNode(fplNode, fplItem.getGuidValue(), guidValue);\r | |
743 | //\r | |
744 | // Add <Version>\r | |
745 | //\r | |
746 | setStrItemToNode(fplNode, fplItem.getVersion(), version);\r | |
747 | //\r | |
748 | // Add to <PlatformList>\r | |
749 | //\r | |
750 | parentNode.appendChild(fplNode);\r | |
751 | \r | |
752 | }\r | |
753 | \r | |
754 | public void ffiToFfNode(Element parentNode, FarFileItem ffi) {\r | |
1898a0c3 | 755 | Element farFileName = this.mainfestDoc\r |
756 | .createElement(farPackage_FarfileName);\r | |
5a24e806 | 757 | farFileName.setTextContent(ffi.getRelativeFilename());\r |
758 | System.out.println(farFileName.getTextContent());\r | |
1898a0c3 | 759 | System.out.println(ffi.getRelativeFilename()); \r |
5a24e806 | 760 | farFileName.setAttribute(farFileName_Md5sum, ffi.getMd5Value());\r |
761 | parentNode.appendChild(farFileName);\r | |
762 | }\r | |
763 | \r | |
1898a0c3 | 764 | public void setStrItemToNode(Element parentNode, String strValue,\r |
765 | String strName) {\r | |
5a24e806 | 766 | Element node = this.mainfestDoc.createElement(strName);\r |
767 | node.setTextContent(strValue);\r | |
768 | parentNode.appendChild(node);\r | |
769 | }\r | |
770 | \r | |
771 | private void parseMainfest() {\r | |
772 | \r | |
773 | //\r | |
774 | // Parse header\r | |
775 | //\r | |
776 | parseMfHeader();\r | |
777 | //\r | |
778 | // parse <farPackageList>\r | |
779 | //\r | |
780 | parseHeaderFarPackageList();\r | |
781 | \r | |
782 | //\r | |
783 | // parse <farPlatformList>\r | |
784 | //\r | |
785 | NodeList ele = this.mainfestDoc.getElementsByTagName(farPlatformList);\r | |
786 | Node plfNode;\r | |
787 | if (ele.getLength() > 0) {\r | |
788 | //\r | |
789 | // Only have one <FarPlatformList> node under mainfest root node.\r | |
790 | //\r | |
791 | plfNode = ele.item(0);\r | |
792 | parseFarPlatformList(plfNode, this.fPlfList);\r | |
793 | }\r | |
794 | }\r | |
795 | \r | |
796 | private void parseMfHeader() {\r | |
797 | Node headerNode;\r | |
798 | NodeList ele = this.mainfestDoc.getElementsByTagName(farHeader);\r | |
799 | if (ele.getLength() > 0) {\r | |
800 | //\r | |
801 | // For mainfest file only have one <FarHeader>\r | |
802 | //\r | |
803 | headerNode = ele.item(0);\r | |
804 | } else {\r | |
805 | return;\r | |
806 | }\r | |
1898a0c3 | 807 | NodeList childList = headerNode.getChildNodes();\r |
5a24e806 | 808 | Node node = null;\r |
809 | String nodeName = null;\r | |
810 | for (int i = 0; i < childList.getLength(); i++) {\r | |
811 | node = childList.item(i);\r | |
812 | nodeName = node.getNodeName();\r | |
1898a0c3 | 813 | if (nodeName.equalsIgnoreCase(farHeader_FarName)) {\r |
5a24e806 | 814 | String nodeValue = node.getTextContent();\r |
815 | this.fhInfo.setFarName(nodeValue);\r | |
816 | } else if (nodeName.equalsIgnoreCase(guidValue)) {\r | |
817 | this.fhInfo.setGuidValue(node.getTextContent());\r | |
818 | } else if (nodeName.equalsIgnoreCase(version)) {\r | |
819 | this.fhInfo.setVersion(node.getTextContent());\r | |
820 | } else if (nodeName.equalsIgnoreCase(farHeader_Abstract)) {\r | |
821 | this.fhInfo.setAbstractStr(node.getTextContent());\r | |
822 | } else if (nodeName.equalsIgnoreCase(farHeader_Description)) {\r | |
823 | this.fhInfo.setDescription(node.getTextContent());\r | |
824 | } else if (nodeName.equalsIgnoreCase(farHeader_CopyRight)) {\r | |
825 | this.fhInfo.setCopyright(node.getTextContent());\r | |
826 | } else if (nodeName.equalsIgnoreCase(farHeader_License)) {\r | |
827 | this.fhInfo.setLicense(node.getTextContent());\r | |
828 | } else if (nodeName.equalsIgnoreCase(farHeader_Specification)) {\r | |
829 | this.fhInfo.setSpecification(node.getTextContent());\r | |
830 | }\r | |
831 | }\r | |
832 | }\r | |
833 | \r | |
834 | public void parseHeaderFarPackageList() {\r | |
835 | Node farPkgNode;\r | |
836 | NodeList ele = this.mainfestDoc.getElementsByTagName(farPackageList);\r | |
837 | if (ele.getLength() > 0) {\r | |
838 | //\r | |
839 | // For mainfest file only have one <FarHeader>\r | |
840 | //\r | |
841 | farPkgNode = ele.item(0);\r | |
842 | } else {\r | |
843 | return;\r | |
844 | }\r | |
845 | NodeList fpnList = farPkgNode.getChildNodes();\r | |
846 | for (int i = 0; i < fpnList.getLength(); i++) {\r | |
1898a0c3 | 847 | if (fpnList.item(i).getNodeType()== Node.TEXT_NODE){\r |
5a24e806 | 848 | continue;\r |
849 | }\r | |
850 | FarPackage fpItem = new FarPackage();\r | |
851 | parseFarPackage(fpnList.item(i), fpItem);\r | |
852 | this.fPkgList.add(fpItem);\r | |
853 | }\r | |
854 | }\r | |
855 | \r | |
856 | public void parseFarPackage(Node farPkgNode, FarPackage fpItem) {\r | |
857 | NodeList childList = farPkgNode.getChildNodes();\r | |
858 | Node childNode;\r | |
859 | String childName;\r | |
860 | for (int i = 0; i < childList.getLength(); i++) {\r | |
861 | childNode = childList.item(i);\r | |
862 | childName = childNode.getNodeName();\r | |
863 | if (childName.equalsIgnoreCase(farPackage_FarfileName)) {\r | |
864 | fpItem.setFarFile(parseFarFile(childNode));\r | |
865 | } else if (childName.equalsIgnoreCase(guidValue)) {\r | |
866 | fpItem.setGuidValue(childNode.getTextContent());\r | |
867 | } else if (childName.equalsIgnoreCase(version)) {\r | |
868 | fpItem.setVersion(childNode.getTextContent());\r | |
869 | } else if (childName.equalsIgnoreCase(farPackage_DefaultPath)) {\r | |
870 | fpItem.setDefaultPath(childNode.getTextContent());\r | |
871 | } else if (childName.equalsIgnoreCase(farPlatformList)) {\r | |
872 | List<FarPlatformItem> plfList = new ArrayList<FarPlatformItem>();\r | |
873 | parseFarPlatformList(childNode, plfList);\r | |
874 | fpItem.setFarPlatformList(plfList);\r | |
875 | } else if (childName.equalsIgnoreCase(contents)) {\r | |
876 | List<FarFileItem> ffList = new ArrayList<FarFileItem>();\r | |
877 | parseContents(childNode, ffList);\r | |
878 | fpItem.setContentList(ffList);\r | |
879 | }\r | |
880 | }\r | |
881 | }\r | |
882 | \r | |
883 | /**\r | |
884 | * \r | |
885 | * @param fpfListNode\r | |
886 | * @param plfList\r | |
887 | */\r | |
1898a0c3 | 888 | public void parseFarPlatformList(Node fpfListNode,\r |
889 | List<FarPlatformItem> plfList) {\r | |
5a24e806 | 890 | //\r |
891 | // Get <FarPlatform> list.\r | |
892 | //\r | |
893 | NodeList child = fpfListNode.getChildNodes();\r | |
894 | Node farPlfNode;\r | |
895 | for (int i = 0; i < child.getLength(); i++) {\r | |
1898a0c3 | 896 | if (child.item(i).getNodeType()== Node.TEXT_NODE){\r |
5a24e806 | 897 | continue;\r |
898 | }\r | |
899 | farPlfNode = child.item(i);\r | |
900 | plfList.add(parseFarPlatform(farPlfNode));\r | |
901 | }\r | |
902 | }\r | |
903 | \r | |
904 | /**\r | |
905 | * \r | |
906 | * @param fpfNode\r | |
907 | * @return\r | |
908 | */\r | |
909 | public FarPlatformItem parseFarPlatform(Node fpfNode) {\r | |
910 | //\r | |
911 | // New FarPlatformItem.\r | |
912 | //\r | |
913 | FarPlatformItem fplItem = new FarPlatformItem();\r | |
914 | //\r | |
915 | // Get <FarPlatform> elements;\r | |
916 | //\r | |
917 | NodeList childList = fpfNode.getChildNodes();\r | |
918 | Node child;\r | |
919 | String nodeName;\r | |
920 | for (int i = 0; i < childList.getLength(); i++) {\r | |
921 | child = childList.item(i);\r | |
922 | nodeName = child.getNodeName();\r | |
923 | if (nodeName.equalsIgnoreCase(farPackage_FarfileName)) {\r | |
924 | fplItem.setFarFile(parseFarFile(child));\r | |
925 | } else if (nodeName.equalsIgnoreCase(guidValue)) {\r | |
926 | fplItem.setGuidValue(child.getTextContent());\r | |
927 | } else if (nodeName.equalsIgnoreCase(version)) {\r | |
928 | fplItem.setVersion(child.getTextContent());\r | |
929 | }\r | |
930 | }\r | |
931 | \r | |
932 | return fplItem;\r | |
933 | }\r | |
934 | \r | |
935 | public void parseContents(Node contentsNode, List<FarFileItem> ffList) {\r | |
936 | NodeList contentList = contentsNode.getChildNodes();\r | |
937 | Node contentNode;\r | |
938 | for (int i = 0; i < contentList.getLength(); i++) {\r | |
1898a0c3 | 939 | if (contentList.item(i).getNodeType()== Node.TEXT_NODE){\r |
5a24e806 | 940 | continue;\r |
941 | }\r | |
942 | contentNode = contentList.item(i);\r | |
943 | //\r | |
944 | // Parse each <FarFileName>.\r | |
945 | //\r | |
946 | ffList.add(parseFarFile(contentNode));\r | |
947 | }\r | |
948 | }\r | |
949 | \r | |
950 | public FarFileItem parseFarFile(Node farFileNode) {\r | |
951 | String ffName = farFileNode.getTextContent();\r | |
952 | NamedNodeMap attr = farFileNode.getAttributes();\r | |
1898a0c3 | 953 | FarFileItem ffItem = new FarFileItem(ffName, attr.getNamedItem(\r |
954 | farFileName_Md5sum).getTextContent());\r | |
5a24e806 | 955 | return ffItem;\r |
956 | }\r | |
957 | \r | |
958 | public boolean isFilter(File file, Set<String> fileter) {\r | |
1898a0c3 | 959 | Iterator<String> iter = fileter.iterator();\r |
960 | while (iter.hasNext()) {\r | |
961 | Pattern pattern = Pattern.compile(iter.next());\r | |
962 | Matcher matcher = pattern.matcher(file.getName());\r | |
963 | \r | |
964 | if(matcher.find()) {\r | |
965 | return true;\r | |
966 | }\r | |
5a24e806 | 967 | }\r |
5a24e806 | 968 | return false;\r |
969 | }\r | |
970 | \r | |
971 | }\r |