/
DebtModelXMLExporter.java
332 lines (289 loc) · 11.3 KB
/
DebtModelXMLExporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.debt;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.debt.DebtCharacteristic;
import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.xml.sax.InputSource;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.collect.Lists.newArrayList;
/**
* Export characteristics and rule debt definitions to XML
*/
public class DebtModelXMLExporter implements ServerComponent {
private static final String ROOT = "sqale";
private static final String DEFAULT_INDENT = "2";
public static final String CHARACTERISTIC = "chc";
public static final String CHARACTERISTIC_KEY = "key";
public static final String CHARACTERISTIC_NAME = "name";
public static final String PROPERTY = "prop";
public static final String PROPERTY_KEY = "key";
public static final String PROPERTY_VALUE = "val";
public static final String PROPERTY_TEXT_VALUE = "txt";
public static final String REPOSITORY_KEY = "rule-repo";
public static final String RULE_KEY = "rule-key";
public static final String PROPERTY_FUNCTION = "remediationFunction";
public static final String PROPERTY_COEFFICIENT = "remediationFactor";
public static final String PROPERTY_OFFSET = "offset";
protected String export(DebtModel debtModel, List<RuleDebt> allRules) {
StringBuilder sb = new StringBuilder();
sb.append("<" + ROOT + ">");
for (DebtCharacteristic characteristic : debtModel.rootCharacteristics()) {
processCharacteristic(debtModel, characteristic, allRules, sb);
}
sb.append("</" + ROOT + ">");
String xml = sb.toString();
xml = prettyFormatXml(xml);
return xml;
}
private void processCharacteristic(DebtModel debtModel, DebtCharacteristic characteristic, List<RuleDebt> allRules, StringBuilder xml) {
xml.append("<" + CHARACTERISTIC + ">");
if (StringUtils.isNotBlank(characteristic.key())) {
xml.append("<" + CHARACTERISTIC_KEY + ">");
xml.append(StringEscapeUtils.escapeXml(characteristic.key()));
xml.append("</" + CHARACTERISTIC_KEY + "><" + CHARACTERISTIC_NAME + ">");
xml.append(StringEscapeUtils.escapeXml(characteristic.name()));
xml.append("</" + CHARACTERISTIC_NAME + ">");
}
if (characteristic.isSub()) {
List<RuleDebt> rules = rules(allRules, characteristic.key());
for (RuleDebt ruleDto : rules) {
processRule(ruleDto, xml);
}
} else {
for (DebtCharacteristic child : debtModel.subCharacteristics(characteristic.key())) {
processCharacteristic(debtModel, child, allRules, xml);
}
}
xml.append("</" + CHARACTERISTIC + ">");
}
private void processRule(RuleDebt rule, StringBuilder xml) {
xml.append("<" + CHARACTERISTIC + ">");
xml.append("<" + REPOSITORY_KEY + ">");
xml.append(StringEscapeUtils.escapeXml(rule.ruleKey().repository()));
xml.append("</" + REPOSITORY_KEY + "><" + RULE_KEY + ">");
xml.append(StringEscapeUtils.escapeXml(rule.ruleKey().rule()));
xml.append("</" + RULE_KEY + ">");
String coefficient = rule.coefficient();
String offset = rule.offset();
processProperty(PROPERTY_FUNCTION, null, rule.function(), xml);
if (coefficient != null) {
String[] values = getValues(coefficient);
processProperty(PROPERTY_COEFFICIENT, values[0], values[1], xml);
}
if (offset != null) {
String[] values = getValues(offset);
processProperty(PROPERTY_OFFSET, values[0], values[1], xml);
}
xml.append("</" + CHARACTERISTIC + ">");
}
private static String[] getValues(String factorOrOffset) {
String[] result = new String[2];
Pattern pattern = Pattern.compile("(\\d+)(\\w+)");
Matcher matcher = pattern.matcher(factorOrOffset);
if (matcher.find()) {
String value = matcher.group(1);
String unit = matcher.group(2);
result[0] = value;
result[1] = unit;
}
return result;
}
private void processProperty(String key, @Nullable String val, String text, StringBuilder xml) {
xml.append("<" + PROPERTY + "><" + PROPERTY_KEY + ">");
xml.append(StringEscapeUtils.escapeXml(key));
xml.append("</" + PROPERTY_KEY + ">");
if (val != null) {
xml.append("<" + PROPERTY_VALUE + ">");
xml.append(val);
xml.append("</" + PROPERTY_VALUE + ">");
}
if (StringUtils.isNotEmpty(text)) {
xml.append("<" + PROPERTY_TEXT_VALUE + ">");
xml.append(StringEscapeUtils.escapeXml(text));
xml.append("</" + PROPERTY_TEXT_VALUE + ">");
}
xml.append("</" + PROPERTY + ">");
}
private String prettyFormatXml(String xml) {
try {
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8))));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), Charsets.UTF_8);
} catch (TransformerException ignored) {
// Ignore, raw XML will be returned
}
return xml;
}
private List<RuleDebt> rules(List<RuleDebt> rules, final String parentKey) {
return newArrayList(Iterables.filter(rules, new Predicate<RuleDebt>() {
@Override
public boolean apply(RuleDebt input) {
return parentKey.equals(input.subCharacteristicKey());
}
}));
}
public static class DebtModel {
private Multimap<String, DebtCharacteristic> characteristicsByKey;
public DebtModel() {
characteristicsByKey = ArrayListMultimap.create();
}
public DebtModel addRootCharacteristic(DebtCharacteristic characteristic) {
characteristicsByKey.put(null, characteristic);
return this;
}
public DebtModel addSubCharacteristic(DebtCharacteristic subCharacteristic, String characteristicKey) {
characteristicsByKey.put(characteristicKey, subCharacteristic);
return this;
}
/**
* @return root characteristics sorted by order
*/
public List<DebtCharacteristic> rootCharacteristics() {
return sortByOrder(newArrayList(characteristicsByKey.get(null)));
}
/**
* @return root characteristics sorted by name
*/
public List<DebtCharacteristic> subCharacteristics(String characteristicKey) {
return sortByName(newArrayList(characteristicsByKey.get(characteristicKey)));
}
@CheckForNull
public DebtCharacteristic characteristicByKey(final String key) {
return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
@Override
public boolean apply(DebtCharacteristic input) {
return key.equals(input.key());
}
}, null);
}
public DebtCharacteristic characteristicById(final Integer id) {
return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
@Override
public boolean apply(DebtCharacteristic input) {
return id.equals(((DefaultDebtCharacteristic) input).id());
}
});
}
private List<DebtCharacteristic> sortByOrder(List<DebtCharacteristic> characteristics) {
Collections.sort(characteristics, new Ordering<DebtCharacteristic>() {
@Override
public int compare(@Nullable DebtCharacteristic left, @Nullable DebtCharacteristic right) {
if (left == null || left.order() == null || right == null || right.order() == null) {
return -1;
}
return left.order() - right.order();
}
});
return characteristics;
}
private List<DebtCharacteristic> sortByName(List<DebtCharacteristic> characteristics) {
Collections.sort(characteristics, new Ordering<DebtCharacteristic>() {
@Override
public int compare(@Nullable DebtCharacteristic left, @Nullable DebtCharacteristic right) {
if (left == null || right == null) {
return -1;
}
return StringUtils.defaultString(left.name()).compareTo(StringUtils.defaultString(right.name()));
}
});
return characteristics;
}
}
public static class RuleDebt {
private RuleKey ruleKey;
private String subCharacteristicKey;
private String function;
private String coefficient;
private String offset;
public RuleKey ruleKey() {
return ruleKey;
}
public RuleDebt setRuleKey(RuleKey ruleKey) {
this.ruleKey = ruleKey;
return this;
}
public String subCharacteristicKey() {
return subCharacteristicKey;
}
public RuleDebt setSubCharacteristicKey(String subCharacteristicKey) {
this.subCharacteristicKey = subCharacteristicKey;
return this;
}
public String function() {
return function;
}
public RuleDebt setFunction(String function) {
this.function = function;
return this;
}
@CheckForNull
public String coefficient() {
return coefficient;
}
public RuleDebt setCoefficient(@Nullable String coefficient) {
this.coefficient = coefficient;
return this;
}
@CheckForNull
public String offset() {
return offset;
}
public RuleDebt setOffset(@Nullable String offset) {
this.offset = offset;
return this;
}
@Override
public String toString() {
return "RuleDebt{" +
"ruleKey=" + ruleKey +
", subCharacteristicKey='" + subCharacteristicKey + '\'' +
", function=" + function +
", coefficient='" + coefficient + '\'' +
", offset='" + offset + '\'' +
'}';
}
}
}