1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.reports;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21
22 import javax.annotation.concurrent.NotThreadSafe;
23
24 import com.google.common.collect.ArrayListMultimap;
25
26 import de.smartics.properties.api.core.context.alias.AliasException;
27 import de.smartics.properties.api.core.context.alias.AliasTraverser;
28 import de.smartics.properties.api.core.context.alias.PropertyAliasMapping;
29 import de.smartics.properties.api.core.domain.SourceInfo;
30 import de.smartics.properties.report.app.ReportException;
31 import de.smartics.properties.report.data.PropertyReport;
32 import de.smartics.properties.report.data.PropertyReportItem;
33 import de.smartics.properties.report.data.ReportProblem;
34
35
36
37
38 @NotThreadSafe
39 public abstract class AbstractPropertyReport implements PropertyReport
40 {
41
42
43
44
45
46
47
48
49
50
51 private final List<ReportProblem> problems = new ArrayList<ReportProblem>();
52
53
54
55
56 private final PropertyAliasMapping aliasMapping = new PropertyAliasMapping();
57
58
59
60
61 private final ArrayListMultimap<String, PropertyReportItem> keyUniquenessMap =
62 ArrayListMultimap.create();
63
64
65
66
67
68
69
70
71 protected AbstractPropertyReport()
72 {
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public final List<ReportProblem> getProblems()
89 {
90 return Collections.unmodifiableList(problems);
91 }
92
93
94
95
96
97
98
99
100 @Override
101 public final void addProblem(final ReportProblem problem)
102 {
103 problems.add(problem);
104 }
105
106
107
108
109
110
111 public final boolean hasProblems()
112 {
113 return !problems.isEmpty();
114 }
115
116
117
118
119
120
121 @Override
122 public final void traverseAliases(final AliasTraverser traverser)
123 {
124 aliasMapping.traverse(traverser);
125 }
126
127
128
129
130
131
132
133
134
135
136
137 @Override
138 public void handle(final PropertyReportItem item) throws ReportException
139
140 {
141 register(item);
142 }
143
144
145
146
147
148
149
150 protected final void register(final PropertyReportItem item)
151 {
152 final String name = item.getName();
153
154 checkForUniqueness(item, name);
155
156 registerAlias(item, name);
157 }
158
159 private void checkForUniqueness(final PropertyReportItem item,
160 final String name)
161 {
162 keyUniquenessMap.put(name, item);
163 final List<PropertyReportItem> items = keyUniquenessMap.get(name);
164 if (items.size() > 1)
165 {
166 final StringBuilder buffer = new StringBuilder(1024);
167 buffer.append("Duplicate key '" + name + "' found:");
168 for (final PropertyReportItem duplicate : items)
169 {
170 buffer.append("\n -> ")
171 .append(duplicate.getSourceInfo().getElementId()).append('/')
172 .append(duplicate.getDescriptor().getKey());
173 }
174 final String message = buffer.toString();
175 final ReportProblem problem = new ReportProblem(message);
176 addProblem(problem);
177 }
178 }
179
180 private void registerAlias(final PropertyReportItem item, final String name)
181 {
182
183
184 final SourceInfo sourceInfo = item.getSourceInfo();
185 if (sourceInfo != null)
186 {
187 final String elementId = sourceInfo.getElementId();
188 try
189 {
190 aliasMapping.add(elementId, name);
191 }
192 catch (final AliasException e)
193 {
194 addProblem(new ReportProblem("Cannot register alias '" + elementId
195 + "'.", e));
196 }
197 }
198 }
199
200
201
202 }