1 package net.sourceforge.argval.packageinfo.impl;
2
3
4 import java.util.Iterator;
5 import java.util.Set;
6 import java.util.TreeSet;
7
8 import net.sourceforge.argval.packageinfo.PackageInfo;
9 import net.sourceforge.argval.packageinfo.PackageInfoVisitor;
10
11 import org.jdom.Document;
12 import org.jdom.Element;
13 import org.jdom.Text;
14
15
16
17
18
19
20
21
22
23
24 public class DomPackageVisitor implements PackageInfoVisitor {
25 private Document doc;
26 private boolean isIncludePackageNameActive = true;
27
28
29
30
31
32
33
34 public DomPackageVisitor() {
35 super();
36 doc = new Document();
37 doc.addContent(new Element("packages"));
38 }
39
40
41
42
43
44
45
46
47 public DomPackageVisitor(boolean isIncludePackageNameActive) {
48 this();
49 this.isIncludePackageNameActive = isIncludePackageNameActive;
50 }
51
52
53
54
55
56 public void visit(PackageInfo packageInfo) {
57 Element packagesElement = doc.getRootElement();
58 Element packageElement = new Element("package");
59 packagesElement.addContent(packageElement);
60 packageElement.addContent(
61 createElementTitleVendorVersion("specification",
62 packageInfo.getSpecificationTitle(),
63 packageInfo.getSpecificationVendor(),
64 packageInfo.getSpecificationVersion()));
65 packageElement.addContent(
66 createElementTitleVendorVersion("implementation",
67 packageInfo.getImplementationTitle(),
68 packageInfo.getImplementationVendor(),
69 packageInfo.getImplementationVersion()));
70
71 if (isIncludePackageNameActive) {
72 for (Iterator<String> nameIter = convert(packageInfo.getNameSet()).iterator(); nameIter.hasNext();) {
73 packageElement.addContent(createElementWithText("name", nameIter.next().toString()));
74 }
75 }
76 }
77
78
79
80
81
82
83
84 public Document getDocument() {
85 return doc;
86 }
87
88
89 private Element createElementTitleVendorVersion(String tag, String title, String vendor, String version) {
90 Element element = new Element(tag);
91 element.addContent(createElementWithText("title", title));
92 element.addContent(createElementWithText("vendor", vendor));
93 element.addContent(createElementWithText("version", version));
94 return element;
95 }
96
97
98 private Element createElementWithText(String tag, String value) {
99 Element element = new Element(tag);
100 element.setContent(new Text(value));
101 return element;
102 }
103
104
105 private TreeSet<String> convert(Set<?> originalSet) {
106 TreeSet<String> newSet = new TreeSet<String>();
107 for (Object object : originalSet) {
108 newSet.add(object.toString());
109 }
110 return newSet;
111 }
112
113 }