1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.collect.extractor;
17
18 import java.util.Map;
19 import java.util.Map.Entry;
20
21 import javax.lang.model.element.AnnotationMirror;
22 import javax.lang.model.element.AnnotationValue;
23 import javax.lang.model.element.Element;
24
25 import org.apache.commons.lang.StringUtils;
26
27 import de.smartics.testdoc.annotations.TestDocHint;
28 import de.smartics.testdoc.core.doc.SortKey;
29
30
31
32
33
34
35
36 class TestDocHintsExtractor
37 {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 static final class Hints
54 {
55
56
57
58 private String sentence;
59
60
61
62
63 public String mainSortKey;
64
65
66
67
68 public String indexSortKey;
69
70 Hints()
71 {
72 }
73
74
75
76
77
78
79 public String getSentence()
80 {
81 return sentence;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public boolean hasSentence()
102 {
103 return StringUtils.isNotBlank(sentence);
104 }
105
106
107
108
109
110
111 public String getMainSortKey()
112 {
113 return mainSortKey;
114 }
115
116
117
118
119
120
121 public String getIndexSortKey()
122 {
123 return indexSortKey;
124 }
125
126
127
128
129
130
131
132
133
134 public SortKey createSortKey(final Hints defaultHints)
135 {
136 final String mainIndexKey =
137 calcIndexKey(getMainSortKey(),
138 (defaultHints != null) ? defaultHints.getMainSortKey() : null);
139 final String indexKey =
140 calcIndexKey(getIndexSortKey(),
141 (defaultHints != null) ? defaultHints.getIndexSortKey() : null);
142 final SortKey sortKey = new SortKey(mainIndexKey, indexKey);
143 return sortKey;
144 }
145
146 private static String calcIndexKey(final String key, final String defaultKey)
147 {
148 if (key == null)
149 {
150 return defaultKey;
151 }
152 return key;
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 Hints readHints(final Element element)
172 {
173 final Hints hints = new Hints();
174 for (final AnnotationMirror annotation : element.getAnnotationMirrors())
175 {
176 if (TestDocHint.class.getName().equals(
177 annotation.getAnnotationType().toString()))
178 {
179 return readHints(hints, annotation);
180 }
181 }
182
183 return hints;
184 }
185
186 private static Hints readHints(final Hints hints,
187 final AnnotationMirror annotation)
188 {
189 final Map<? extends Element, ? extends AnnotationValue> map =
190 annotation.getElementValues();
191 for (final Map.Entry<? extends Element, ? extends AnnotationValue> entry : map
192 .entrySet())
193 {
194 addHint(hints, entry);
195 }
196 return hints;
197 }
198
199 private static void addHint(final Hints hints,
200 final Map.Entry<? extends Element, ? extends AnnotationValue> entry)
201 {
202 final String key = entry.getKey().toString();
203 if (isKey(key, "sentence()"))
204 {
205 final Object value = entry.getValue().getValue();
206 if (value instanceof String)
207 {
208 hints.sentence = (String) value;
209 }
210 }
211 if (isKey(key, "mainSortKey()"))
212 {
213 hints.mainSortKey = calcStringValue(entry);
214 }
215 if (isKey(key, "indexSortKey()"))
216 {
217 hints.indexSortKey = calcStringValue(entry);
218 }
219 }
220
221 private static boolean isKey(final String key, final String suffix)
222 {
223 return key != null && key.trim().endsWith(suffix);
224 }
225
226 private static String calcStringValue(
227 final Entry<? extends Element, ? extends AnnotationValue> entry)
228 {
229 final Object value = entry.getValue().getValue();
230
231 if (value instanceof String)
232 {
233 final String stringValue = (String) value;
234 if (StringUtils.isNotBlank(stringValue))
235 {
236 return stringValue;
237 }
238 }
239 return null;
240 }
241
242
243
244 }