1 package net.sourceforge.argval.packageinfo.impl;
2
3
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.TreeSet;
8
9 import net.sourceforge.argval.collection.CollectionFilter;
10 import net.sourceforge.argval.collection.CollectionUtil;
11 import net.sourceforge.argval.collection.CollectionUtilConfig;
12 import net.sourceforge.argval.collection.CollectionUtilConfigFactory;
13 import net.sourceforge.argval.collection.Filter;
14 import net.sourceforge.argval.packageinfo.PackageInfo;
15 import net.sourceforge.argval.packageinfo.PackageInfoVisitor;
16 import net.sourceforge.argval.utils.StringUtil;
17
18
19
20
21
22
23
24
25
26
27
28 final public class SimplePackageInfoVisitor implements PackageInfoVisitor {
29 public final static int MAX_PACKAGE_NAME_DEPTH = Integer.MAX_VALUE;
30
31 private static final String PACKAGE_NAME_SEPARATOR = ".";
32 private final static String CONFIG_PACKAGE_NAME = "package name";
33
34 private StringBuffer strBuf = new StringBuffer();
35 private int packageNameDepth = MAX_PACKAGE_NAME_DEPTH;
36
37
38
39 private CollectionFilter collectionFilter = null;
40 private CollectionUtilConfigFactory configFactory = null;
41
42
43
44
45
46
47 public SimplePackageInfoVisitor(CollectionUtilConfigFactory configFactory, CollectionFilter collectionFilter) {
48 super();
49 this.configFactory = configFactory;
50 this.collectionFilter = collectionFilter;
51 configFactory.create(CONFIG_PACKAGE_NAME, PACKAGE_NAME_SEPARATOR);
52 }
53 public SimplePackageInfoVisitor() {
54 this(new CollectionUtilConfigFactory(), new CollectionFilter());
55 }
56
57
58
59
60
61
62
63
64 public SimplePackageInfoVisitor(boolean isIncludePackageNameActive) {
65 this((isIncludePackageNameActive) ? MAX_PACKAGE_NAME_DEPTH : 0);
66 }
67 public SimplePackageInfoVisitor(int packageNameDepth) {
68 this(packageNameDepth, null);
69 }
70 public SimplePackageInfoVisitor(Filter filter) {
71 this(MAX_PACKAGE_NAME_DEPTH, filter);
72 }
73 public SimplePackageInfoVisitor(int packageNameDepth, Filter filter) {
74 this();
75 this.packageNameDepth = packageNameDepth;
76 this.collectionFilter = new CollectionFilter(filter);
77 }
78
79
80
81
82
83
84
85
86 public void visit(PackageInfo packageInfo) {
87 if (PackageInfo.UNKNOWN_PACKAGE_TITLE.equals(packageInfo.getTitle())) {
88
89 if (packageNameDepth > 0) {
90
91 strBuf.append("Loaded packages, from which no Specification/Implementation title (and version) is known");
92 }
93 }
94 else {
95 strBuf.append(packageInfo.getTitle());
96 }
97 if (packageInfo.getImplementationVersion() != null) {
98 strBuf.append(" [").append(packageInfo.getImplementationVersion()).append("]");
99 }
100 strBuf.append(StringUtil.LINE_SEPARATOR);
101 if (packageNameDepth > 0) {
102
103 Set<String> compactNameSet = (packageNameDepth == MAX_PACKAGE_NAME_DEPTH)
104 ? convert(packageInfo.getNameSet())
105 : chop(convert(packageInfo.getNameSet()), configFactory.getConfig(CONFIG_PACKAGE_NAME), packageNameDepth);
106
107
108 Set<String> filteredSet = collectionFilter.filter(compactNameSet);
109
110
111 for (Iterator<String> nameIter = filteredSet.iterator(); nameIter.hasNext(); ) {
112 strBuf.append(" ").append(nameIter.next()).append(StringUtil.LINE_SEPARATOR);
113 }
114 }
115 strBuf.append(StringUtil.LINE_SEPARATOR);
116 }
117
118
119 private TreeSet<String> convert(Set<?> originalSet) {
120 TreeSet<String> newSet = new TreeSet<String>();
121 for (Object object : originalSet) {
122 newSet.add(object.toString());
123 }
124 return newSet;
125 }
126
127
128
129 private static TreeSet<String> chop(Set<String> nameSet, CollectionUtilConfig config, int maxDepth) {
130 TreeSet<String> compactNameSet = new TreeSet<String>();
131 for (Iterator<String> iter = nameSet.iterator(); iter.hasNext(); ) {
132 String name = (String)iter.next();
133
134 List<String> nameList = CollectionUtil.stringToList(name, PACKAGE_NAME_SEPARATOR);
135
136 while (nameList.size() > maxDepth) {
137
138 nameList.remove(nameList.size() - 1);
139 }
140 String compactName = CollectionUtil.toString(nameList, config);
141 compactNameSet.add(compactName);
142
143 }
144 return compactNameSet;
145 }
146
147
148
149 public String toString() {
150 return strBuf.toString();
151 }
152
153 }