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