-
Notifications
You must be signed in to change notification settings - Fork 30
/
ClocReport.java
99 lines (85 loc) · 3.05 KB
/
ClocReport.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
package hudson.plugins.sloccount.model.cloc;
import hudson.plugins.sloccount.model.SloccountReportInterface;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
import java.io.Serializable;
/**
* Cloc report parser and the parsed file.
*
* @author Michal Turek
* @since 1.20
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "results")
public class ClocReport implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name = "header", type = ClocHeader.class)
private final ClocHeader header;
@XmlElement(name = "files", type = ClocFiles.class)
private final ClocFiles files;
/**
* Constructor.
* @param header header
* @param files files
*/
public ClocReport(ClocHeader header, ClocFiles files) {
this.header = header;
this.files = files;
}
/**
* This constructor is required by JAXB.
*/
public ClocReport() {
this(null, null);
}
public ClocHeader getHeader() {
return header;
}
public ClocFiles getFiles() {
return files;
}
/**
* Parse one input file.
*
* @param file the file to be parsed
* @return the content of the parsed file in form of a report
* @throws javax.xml.bind.JAXBException if a XML related error occurs
*/
public static ClocReport parse(File file) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(ClocReport.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (ClocReport) unmarshaller.unmarshal(file);
}
/**
* Convert cloc report to SLOCCount report representation used in the plugin.
*
* @param report output report
* @param commentIsCode include comments to the measured lines
* @throws JAXBException if the report has unexpected structure
*/
public void toSloccountReport(SloccountReportInterface report, boolean commentIsCode)
throws JAXBException {
try {
for (ClocFile file : files.getFiles()) {
// Get rid of Microsoft's incompatibility once and forever
String filePath = file.getName().replace('\\', '/');
int begin = filePath.indexOf('/');
int end = filePath.indexOf('/', begin + 1);
String moduleName = (begin != -1 && end != -1) ? filePath.substring(begin + 1, end) : "";
int lineCount = file.getCode();
if (commentIsCode) {
lineCount += file.getComment();
}
report.add(filePath, file.getLanguage(), moduleName, lineCount, file.getComment());
}
} catch (RuntimeException e) {
throw new JAXBException("Broken cloc report file: " + e, e);
}
}
}