/
SloccountResult.java
223 lines (181 loc) · 6.53 KB
/
SloccountResult.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
package hudson.plugins.sloccount;
import hudson.model.AbstractBuild;
import hudson.model.ModelObject;
import hudson.plugins.sloccount.model.File;
import hudson.plugins.sloccount.model.FileFilter;
import hudson.plugins.sloccount.model.Language;
import hudson.plugins.sloccount.model.SloccountLanguageStatistics;
import hudson.plugins.sloccount.model.SloccountParser;
import hudson.plugins.sloccount.model.SloccountReport;
import hudson.plugins.sloccount.model.SloccountReportStatistics;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author lordofthepigs
*/
public class SloccountResult implements Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private SloccountReport report;
private final AbstractBuild<?,?> owner;
/** The statistics. */
private SloccountReportStatistics statistics;
/** The encoding that was active at the time of the build. */
private final String encoding;
public SloccountResult(SloccountReportStatistics statistics, String encoding,
SloccountReport report, AbstractBuild<?,?> owner){
this.statistics = statistics;
this.encoding = encoding;
this.report = report;
this.owner = owner;
}
public SloccountReport getReport() {
lazyLoad();
return report;
}
public AbstractBuild<?,?> getOwner() {
return owner;
}
/**
* Get the statistics.
*
* @return the statistics, always non-null value
*/
public SloccountReportStatistics getStatistics() {
convertLegacyData();
return statistics;
}
/**
* Convert legacy data in format of sloccount plugin version 1.10
* to the new one that uses statistics.
*
* If statistics are null for any reason, the object will be created.
* Statistics will be always non-null after this method is called (side
* effect).
*/
private void convertLegacyData() {
if(statistics != null) {
return;
}
List<SloccountLanguageStatistics> languages = new LinkedList<SloccountLanguageStatistics>();
if(report != null) {
for(Language language : report.getLanguages()){
languages.add(new SloccountLanguageStatistics(language.getName(),
language.getLineCount(), language.getFileCount()));
}
}
statistics = new SloccountReportStatistics(languages);
}
/**
* Lazy load report data if they are not already loaded.
*/
private void lazyLoad() {
if(report != null) {
return;
}
java.io.File destDir = new java.io.File(owner.getRootDir(),
SloccountPublisher.BUILD_SUBDIR);
if (!destDir.exists()) {
report = new SloccountReport();
return;
}
String realEncoding = (encoding != null && !encoding.isEmpty())
? encoding : SloccountPublisher.DEFAULT_ENCODING;
SloccountParser parser = new SloccountParser(realEncoding, null, null);
report = parser.parseFiles(destDir.listFiles());
}
/**
* Check that the result is empty.
*
* @return true if the result is empty, otherwise false
*/
public boolean isEmpty() {
if(statistics != null) {
return statistics.getLineCount() <= 0;
}
// Legacy data in format of sloccount plugin version 1.10 and less
if(report != null) {
return report.getLineCount() <= 0;
}
return true;
}
public SloccountResult getLanguageResult(String language){
SloccountReport filtered = new SloccountReport(this.getReport(), new LanguageFileFilter(language));
return new BreadCrumbResult(filtered, this.owner, language);
}
/**
* Get result for a specific module.
*
* @param module the module
* @return the result
*/
public SloccountResult getModuleResult(String module){
SloccountReport filtered = new SloccountReport(this.getReport(),
new ModuleFileFilter(module));
return new BreadCrumbResult(filtered, owner, module);
}
public SloccountResult getFolderResult(String jumbledFolder){
String folder = jumbledFolder.replace("|", SloccountReport.DIRECTORY_SEPARATOR);
SloccountReport filtered = new SloccountReport(this.getReport(), new FolderFileFilter(folder));
return new BreadCrumbResult(filtered, this.owner, folder);
}
private static class LanguageFileFilter implements FileFilter, Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private String language;
public LanguageFileFilter(String language){
this.language = language;
}
public boolean include(File file) {
return file.getLanguage().equals(this.language);
}
}
/**
* File filter for modules.
*
* @author Michal Turek
*/
private static class ModuleFileFilter implements FileFilter, Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
/** The module name. */
private String module;
/**
* Constructor.
*
* @param module the module
*/
public ModuleFileFilter(String module){
this.module = module;
}
public boolean include(File file) {
return file.getModule().equals(module);
}
}
private static class FolderFileFilter implements FileFilter, Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private String folder;
public FolderFileFilter(String folder){
this.folder = folder;
}
public boolean include(File file) {
String fileFolder = SloccountReport.extractFolder(file.getName());
return this.folder.equals(fileFolder);
}
}
private static class BreadCrumbResult extends SloccountResult implements ModelObject, Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private String displayName = null;
public BreadCrumbResult(SloccountReport report, AbstractBuild<?,?> owner, String displayName){
super(null, null, report, owner);
this.displayName = displayName;
}
public String getDisplayName() {
return this.displayName;
}
}
}