]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/Mainfest.java
430cb366523a11fc763d276906f500e4e25d1f0c
[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 result.add(farFileItem);
405 break;
406 }
407 }
408 return result;
409 }
410
411 public List<FarFileItem> getPackageContents(PackageIdentification packageId) {
412 List<FarFileItem> farFileList = new ArrayList<FarFileItem>();
413 Iterator pkgItem = this.fPkgList.iterator();
414 while (pkgItem.hasNext()) {
415 FarPackage pkg = (FarPackage) pkgItem.next();
416 if (pkg.isIdentityPkg(packageId)) {
417 //
418 // Add spd far file to list.
419 //
420 farFileList.add(pkg.getFarFile());
421 //
422 // Add all files in contents to list.
423 //
424 farFileList.addAll(pkg.getContentList());
425 //
426 // Add all farfiles in <FarPlatformList> to list.
427 //
428 // List<FarPlatformItem> plfList = pkg.getFarPlatformList();
429 // Iterator plfItem = plfList.iterator();
430 // while (plfItem.hasNext()) {
431 // farFileList.add(((FarPlatformItem) plfItem.next())
432 // .getFarFile());
433 // }
434
435 Iterator<FarFileItem> ffIter = farFileList.iterator();
436 while (ffIter.hasNext()) {
437 FarFileItem ffItem = ffIter.next();
438 ffItem.setDefaultPath(pkg.getDefaultPath() + File.separatorChar + ffItem.getRelativeFilename());
439 }
440 break;
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() + File.separatorChar + ffItem.getRelativeFilename(),
486 ffItem.getMd5Value()));
487 //
488 // Add all files in contents to list.
489 //
490 Iterator contentsItem = pkg.getContentList().iterator();
491 while (contentsItem.hasNext()) {
492 ffItem = (FarFileItem) contentsItem.next();
493 farFileList.add(new FarFileItem(pkg.getDefaultPath() + File.separator + ffItem.getRelativeFilename(),
494 ffItem.getMd5Value()));
495 }
496 //
497 // Add all farfiles in <FarPlatformList> to list.
498 //
499 List<FarPlatformItem> plfList = pkg.getFarPlatformList();
500 Iterator plfItem = plfList.iterator();
501 while (plfItem.hasNext()) {
502 ffItem = ((FarPlatformItem) plfItem.next()).getFarFile();
503 farFileList.add(new FarFileItem(pkg.getDefaultPath() + File.separator + ffItem.getRelativeFilename(),
504 ffItem.getMd5Value()));
505 }
506 }
507 return farFileList;
508 }
509
510 public String getPackageDefaultPath(PackageIdentification packageId) {
511 Iterator pkgItr = this.fPkgList.iterator();
512 while (pkgItr.hasNext()) {
513 FarPackage farPackage = (FarPackage) pkgItr.next();
514 if (farPackage.isIdentityPkg(packageId)) {
515 return farPackage.getDefaultPath();
516 }
517 }
518 return null;
519 }
520
521 // public void setPackageInstallPath(PackageIdentification packageId, String path) {
522 // Iterator<FarPackage> pkgItr = this.fPkgList.iterator();
523 // while (pkgItr.hasNext()) {
524 // FarPackage farPackage = pkgItr.next();
525 // if (farPackage.isIdentityPkg(packageId)) {
526 // farPackage.setDefaultPath(path);
527 // return ;
528 // }
529 // }
530 // }
531 //
532 // public void setPlatformInstallPath(PlatformIdentification platformId, String path) {
533 // Iterator<FarPlatformItem> plfItr = this.fPlfList.iterator();
534 // while (plfItr.hasNext()) {
535 // FarPlatformItem farPlatform = plfItr.next();
536 // if (farPlatform.i.isIdentity(platformId)) {
537 // farPackage.setDefaultPath(path);
538 // return ;
539 // }
540 // }
541 // }
542
543 public List<FarFileItem> getAllFileItem() {
544 //
545 // The farFileName in this list are all abosulte path.
546 //
547 List<FarFileItem> ffiList = new ArrayList<FarFileItem>();
548 //
549 // Add far files in <FarPackageList> to list.
550 //
551 ffiList = this.getPackageContents();
552
553 //
554 // Add far files in <FarPlatformList> to list
555 //
556 NodeList elementList = this.mainfestDoc.getElementsByTagName(farPlatformList);
557 for (int i = 0; i < elementList.getLength(); i++) {
558 //
559 // Get <farPlatform> node list.
560 //
561 Node item = elementList.item(i);
562 NodeList plfElements = item.getChildNodes();
563 for (int j = 0; j < plfElements.getLength(); j++) {
564 //
565 // Get each <FarPlatform> content.
566 //
567 NodeList plfChildNode = plfElements.item(i).getChildNodes();
568 Node tempNode = null;
569 for (int t = 0; t < plfChildNode.getLength(); t++) {
570 tempNode = plfChildNode.item(t);
571 //
572 // Get child node value and set to platformIdentification.
573 //
574 if (tempNode.getNodeName().equalsIgnoreCase(farPackage_FarfileName)) {
575 NamedNodeMap farAttr = tempNode.getAttributes();
576 //
577 // Change relative path to absolute one
578 //
579 FarFileItem farFile = new FarFileItem(tempNode.getTextContent(),
580 farAttr.getNamedItem(farFileName_Md5sum).getTextContent());
581 ffiList.add(farFile);
582 }
583 }
584 }
585 }
586 return ffiList;
587 }
588
589 public void hibernateToFile() throws Exception {
590 //
591 // create mainfest root node
592 //
593 Element rootNode = this.mainfestDoc.createElement("FrameworkArchiveManifest");
594 this.mainfestDoc.appendChild(rootNode);
595
596 //
597 // create mainfest header node
598 //
599 Element headerNode = this.mainfestDoc.createElement(farHeader);
600 rootNode.appendChild(headerNode);
601 //
602 // Add FarHeader to headerNode.
603 //
604 Element farName = this.mainfestDoc.createElement(farHeader_FarName);
605 farName.setTextContent(this.fhInfo.getFarName());
606 headerNode.appendChild(farName);
607
608 Element gv = this.mainfestDoc.createElement(guidValue);
609 gv.setTextContent(this.fhInfo.getGuidValue());
610 headerNode.appendChild(gv);
611
612 Element ver = this.mainfestDoc.createElement(version);
613 ver.setTextContent(this.fhInfo.getVersion());
614 headerNode.appendChild(ver);
615
616 Element abstra = this.mainfestDoc.createElement(farHeader_Abstract);
617 abstra.setTextContent(this.fhInfo.getAbstractStr());
618 headerNode.appendChild(abstra);
619
620 Element descr = this.mainfestDoc.createElement(farHeader_Description);
621 descr.setTextContent(this.fhInfo.getDescription());
622 headerNode.appendChild(descr);
623
624 Element copyright = this.mainfestDoc.createElement(farHeader_CopyRight);
625 copyright.setTextContent(this.fhInfo.getCopyright());
626 headerNode.appendChild(copyright);
627
628 Element license = this.mainfestDoc.createElement(farHeader_License);
629 license.setTextContent(this.fhInfo.getLicense());
630 headerNode.appendChild(license);
631
632 Element spec = this.mainfestDoc.createElement(farHeader_Specification);
633 spec.setTextContent(this.fhInfo.getSpecification());
634 System.out.println(this.fhInfo.getSpecification());
635 headerNode.appendChild(spec);
636
637 //
638 // create mainfest FarPackageList node
639 //
640 Element pkgListNode = this.mainfestDoc.createElement(farPackageList);
641 rootNode.appendChild(pkgListNode);
642
643 //
644 // Save each item in farPackage list to <FarPackage>.
645 //
646 Iterator pkgItem = this.fPkgList.iterator();
647 while (pkgItem.hasNext()) {
648 pkgToFarPkgNode(pkgListNode, (FarPackage) pkgItem.next());
649
650 }
651
652 //
653 // create mainfest FarPlatformList node
654 //
655 Element plfListNode = this.mainfestDoc.createElement(farPlatformList);
656 rootNode.appendChild(plfListNode);
657
658 //
659 // Save farPakcage list info to <FarPackageList> node
660 //
661 Iterator plfItem = this.fPlfList.iterator();
662 while (plfItem.hasNext()) {
663 FarPlatformItem plfIterator = (FarPlatformItem) plfItem.next();
664 PlfToPlatformNode(plfListNode, plfIterator);
665 }
666
667 //
668 // Write the DOM document to the file
669 //
670 Transformer xformer = TransformerFactory.newInstance().newTransformer();
671 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
672 xformer.setOutputProperty(OutputKeys.INDENT, "yes");
673
674 //
675 // Prepare the DOM document for writing
676 //
677 Source source = new DOMSource(this.mainfestDoc);
678 //
679 // Prepare the output file, get the Mainifest file name from <FarHeader>
680 // /<FarName>.
681 //
682 this.mfFile = new File(Workspace.getCurrentWorkspace() + File.separatorChar + mfFileName);
683 //
684 // generate all directory path
685 //
686 Result result = new StreamResult(this.mfFile);
687 xformer.transform(source, result);
688 }
689
690 public void pkgToFarPkgNode(Element parentNode, FarPackage pkgItem) {
691 Element pkgNode = this.mainfestDoc.createElement(farPackageList_FarPackage);
692 //
693 // Add <FarFileName>
694 //
695 ffiToFfNode(pkgNode, pkgItem.getFarFile());
696 //
697 // Add <GuidValue>
698 //
699 setStrItemToNode(pkgNode, pkgItem.getGuidValue(), guidValue);
700 //
701 // Add <Version>
702 //
703 setStrItemToNode(pkgNode, pkgItem.getVersion(), version);
704 //
705 // Add <DefaultPath>
706 //
707 setStrItemToNode(pkgNode, pkgItem.getDefaultPath(), farPackage_DefaultPath);
708
709 //
710 // Add <Contents>
711 //
712 Element contentNode = this.mainfestDoc.createElement(contents);
713 Iterator iterator = pkgItem.getContentList().iterator();
714 while (iterator.hasNext()) {
715 ffiToFfNode(contentNode, (FarFileItem) iterator.next());
716 }
717 pkgNode.appendChild(contentNode);
718 parentNode.appendChild(pkgNode);
719 }
720
721 public void PlfToPlatformNode(Element parentNode, FarPlatformItem fplItem) {
722 Element fplNode = this.mainfestDoc.createElement(farPlatformList_FarPlatform);
723 //
724 // Add <FarFileName>
725 //
726 ffiToFfNode(fplNode, fplItem.getFarFile());
727 //
728 // Add <GuidValue>
729 //
730 setStrItemToNode(fplNode, fplItem.getGuidValue(), guidValue);
731 //
732 // Add <Version>
733 //
734 setStrItemToNode(fplNode, fplItem.getVersion(), version);
735 //
736 // Add to <PlatformList>
737 //
738 parentNode.appendChild(fplNode);
739
740 }
741
742 public void ffiToFfNode(Element parentNode, FarFileItem ffi) {
743 Element farFileName = this.mainfestDoc.createElement(farPackage_FarfileName);
744 farFileName.setTextContent(ffi.getRelativeFilename());
745 System.out.println(farFileName.getTextContent());
746 System.out.println(ffi.getRelativeFilename());
747 farFileName.setAttribute(farFileName_Md5sum, ffi.getMd5Value());
748 parentNode.appendChild(farFileName);
749 }
750
751 public void setStrItemToNode(Element parentNode, String strValue, String strName) {
752 Element node = this.mainfestDoc.createElement(strName);
753 node.setTextContent(strValue);
754 parentNode.appendChild(node);
755 }
756
757 private void parseMainfest() {
758
759 //
760 // Parse header
761 //
762 parseMfHeader();
763 //
764 // parse <farPackageList>
765 //
766 parseHeaderFarPackageList();
767
768 //
769 // parse <farPlatformList>
770 //
771 NodeList ele = this.mainfestDoc.getElementsByTagName(farPlatformList);
772 Node plfNode;
773 if (ele.getLength() > 0) {
774 //
775 // Only have one <FarPlatformList> node under mainfest root node.
776 //
777 plfNode = ele.item(0);
778 parseFarPlatformList(plfNode, this.fPlfList);
779 }
780 }
781
782 private void parseMfHeader() {
783 Node headerNode;
784 NodeList ele = this.mainfestDoc.getElementsByTagName(farHeader);
785 if (ele.getLength() > 0) {
786 //
787 // For mainfest file only have one <FarHeader>
788 //
789 headerNode = ele.item(0);
790 } else {
791 return;
792 }
793 NodeList childList = headerNode.getChildNodes();
794 Node node = null;
795 String nodeName = null;
796 for (int i = 0; i < childList.getLength(); i++) {
797 node = childList.item(i);
798 nodeName = node.getNodeName();
799 if (nodeName.equalsIgnoreCase(farHeader_FarName)) {
800 String nodeValue = node.getTextContent();
801 this.fhInfo.setFarName(nodeValue);
802 } else if (nodeName.equalsIgnoreCase(guidValue)) {
803 this.fhInfo.setGuidValue(node.getTextContent());
804 } else if (nodeName.equalsIgnoreCase(version)) {
805 this.fhInfo.setVersion(node.getTextContent());
806 } else if (nodeName.equalsIgnoreCase(farHeader_Abstract)) {
807 this.fhInfo.setAbstractStr(node.getTextContent());
808 } else if (nodeName.equalsIgnoreCase(farHeader_Description)) {
809 this.fhInfo.setDescription(node.getTextContent());
810 } else if (nodeName.equalsIgnoreCase(farHeader_CopyRight)) {
811 this.fhInfo.setCopyright(node.getTextContent());
812 } else if (nodeName.equalsIgnoreCase(farHeader_License)) {
813 this.fhInfo.setLicense(node.getTextContent());
814 } else if (nodeName.equalsIgnoreCase(farHeader_Specification)) {
815 this.fhInfo.setSpecification(node.getTextContent());
816 }
817 }
818 }
819
820 public void parseHeaderFarPackageList() {
821 Node farPkgNode;
822 NodeList ele = this.mainfestDoc.getElementsByTagName(farPackageList);
823 if (ele.getLength() > 0) {
824 //
825 // For mainfest file only have one <FarHeader>
826 //
827 farPkgNode = ele.item(0);
828 } else {
829 return;
830 }
831 NodeList fpnList = farPkgNode.getChildNodes();
832 for (int i = 0; i < fpnList.getLength(); i++) {
833 if (fpnList.item(i).getNodeType() == Node.TEXT_NODE) {
834 continue;
835 }
836 FarPackage fpItem = new FarPackage();
837 parseFarPackage(fpnList.item(i), fpItem);
838 this.fPkgList.add(fpItem);
839 }
840 }
841
842 public void parseFarPackage(Node farPkgNode, FarPackage fpItem) {
843 NodeList childList = farPkgNode.getChildNodes();
844 Node childNode;
845 String childName;
846 for (int i = 0; i < childList.getLength(); i++) {
847 childNode = childList.item(i);
848 childName = childNode.getNodeName();
849 if (childName.equalsIgnoreCase(farPackage_FarfileName)) {
850 fpItem.setFarFile(parseFarFile(childNode));
851 } else if (childName.equalsIgnoreCase(guidValue)) {
852 fpItem.setGuidValue(childNode.getTextContent());
853 } else if (childName.equalsIgnoreCase(version)) {
854 fpItem.setVersion(childNode.getTextContent());
855 } else if (childName.equalsIgnoreCase(farPackage_DefaultPath)) {
856 fpItem.setDefaultPath(childNode.getTextContent());
857 } else if (childName.equalsIgnoreCase(farPlatformList)) {
858 List<FarPlatformItem> plfList = new ArrayList<FarPlatformItem>();
859 parseFarPlatformList(childNode, plfList);
860 fpItem.setFarPlatformList(plfList);
861 } else if (childName.equalsIgnoreCase(contents)) {
862 List<FarFileItem> ffList = new ArrayList<FarFileItem>();
863 parseContents(childNode, ffList);
864 fpItem.setContentList(ffList);
865 }
866 }
867 }
868
869 /**
870 *
871 * @param fpfListNode
872 * @param plfList
873 */
874 public void parseFarPlatformList(Node fpfListNode, List<FarPlatformItem> plfList) {
875 //
876 // Get <FarPlatform> list.
877 //
878 NodeList child = fpfListNode.getChildNodes();
879 Node farPlfNode;
880 for (int i = 0; i < child.getLength(); i++) {
881 if (child.item(i).getNodeType() == Node.TEXT_NODE) {
882 continue;
883 }
884 farPlfNode = child.item(i);
885 plfList.add(parseFarPlatform(farPlfNode));
886 }
887 }
888
889 /**
890 *
891 * @param fpfNode
892 * @return
893 */
894 public FarPlatformItem parseFarPlatform(Node fpfNode) {
895 //
896 // New FarPlatformItem.
897 //
898 FarPlatformItem fplItem = new FarPlatformItem();
899 //
900 // Get <FarPlatform> elements;
901 //
902 NodeList childList = fpfNode.getChildNodes();
903 Node child;
904 String nodeName;
905 for (int i = 0; i < childList.getLength(); i++) {
906 child = childList.item(i);
907 nodeName = child.getNodeName();
908 if (nodeName.equalsIgnoreCase(farPackage_FarfileName)) {
909 fplItem.setFarFile(parseFarFile(child));
910 } else if (nodeName.equalsIgnoreCase(guidValue)) {
911 fplItem.setGuidValue(child.getTextContent());
912 } else if (nodeName.equalsIgnoreCase(version)) {
913 fplItem.setVersion(child.getTextContent());
914 }
915 }
916
917 return fplItem;
918 }
919
920 public void parseContents(Node contentsNode, List<FarFileItem> ffList) {
921 NodeList contentList = contentsNode.getChildNodes();
922 Node contentNode;
923 for (int i = 0; i < contentList.getLength(); i++) {
924 if (contentList.item(i).getNodeType() == Node.TEXT_NODE) {
925 continue;
926 }
927 contentNode = contentList.item(i);
928 //
929 // Parse each <FarFileName>.
930 //
931 ffList.add(parseFarFile(contentNode));
932 }
933 }
934
935 public FarFileItem parseFarFile(Node farFileNode) {
936 String ffName = farFileNode.getTextContent();
937 NamedNodeMap attr = farFileNode.getAttributes();
938 FarFileItem ffItem = new FarFileItem(ffName, attr.getNamedItem(farFileName_Md5sum).getTextContent());
939 return ffItem;
940 }
941
942 public boolean isFilter(File file, Set<String> fileter) {
943 Iterator<String> iter = fileter.iterator();
944 while (iter.hasNext()) {
945 Pattern pattern = Pattern.compile(iter.next());
946 Matcher matcher = pattern.matcher(file.getName());
947
948 if (matcher.find()) {
949 return true;
950 }
951 }
952 return false;
953 }
954
955 }