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