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