20
20
import org .slf4j .Logger ;
21
21
import org .slf4j .LoggerFactory ;
22
22
import org .sonar .api .batch .fs .FilePredicate ;
23
+ import org .sonar .api .batch .fs .InputComponent ;
23
24
import org .sonar .api .batch .fs .InputFile ;
24
- import org .sonar .api .batch .fs .InputModule ;
25
+ import org .sonar .api .batch .fs .internal . DefaultInputComponent ;
25
26
import org .sonar .api .batch .sensor .SensorContext ;
26
27
import org .sonar .api .measures .CoreMetrics ;
27
28
import org .w3c .dom .Document ;
@@ -115,23 +116,63 @@ private void parseMeasure(String type, NodeList itemList) {
115
116
116
117
NodeList values = itemElement .getElementsByTagName (VALUE );
117
118
if (FILE_MEASURE .equalsIgnoreCase (type )) {
118
- addComplexityFileMeasures (name , values );
119
+ InputFile inputFile = getFile (name );
120
+ addComplexityFileMeasures (inputFile , values );
119
121
} else if (FUNCTION_MEASURE .equalsIgnoreCase (type )) {
120
- addComplexityFunctionMeasures (new SwiftFunction (name ), values );
122
+ addComplexityFunctionMeasures (new SwiftFunction (0 , name ), values );
121
123
}
122
124
}
123
125
}
124
126
}
125
127
126
- private void addComplexityFileMeasures (String fileName , NodeList values ) {
127
- LOGGER .debug ("File measures for {}" ,fileName );
128
+ private InputFile getFile (String fileName ){
128
129
FilePredicate fp = context .fileSystem ().predicates ().hasRelativePath (fileName );
129
130
if (!context .fileSystem ().hasFiles (fp )) {
130
131
LOGGER .warn ("file not included in sonar {}" , fileName );
131
- return ;
132
+ return null ;
132
133
}
133
- InputFile component = context .fileSystem ().inputFile (fp );
134
+ return context .fileSystem ().inputFile (fp );
135
+ }
136
+
137
+ static class SwiftFunction extends DefaultInputComponent implements InputComponent {
138
+ private String name ;
139
+ private String key ;
140
+ private String file ;
141
+ private int lineNumber ;
142
+ SwiftFunction (int scannerId , String name ) {
143
+ super (scannerId );
144
+ String [] vals = name .split (" at " );
145
+ if (vals .length >= 2 ) {
146
+ this .name = vals [0 ].replaceAll ("\\ W" ,"" );
147
+
148
+ if (vals [1 ].contains (":" )) {
149
+ String [] sp = vals [1 ].split (":" );
150
+ this .file = sp [0 ].substring (0 ,sp [0 ].lastIndexOf ("." ));
151
+ this .lineNumber = Integer .parseInt (sp [1 ]);
152
+ } else {
153
+ this .file = vals [1 ];
154
+ this .lineNumber = 0 ;
155
+ }
156
+
157
+ this .key = String .format ("%s.%s:%d" , this .file , this .name , this .lineNumber );
158
+ } else {
159
+ this .key = name ;
160
+ }
161
+ }
162
+ @ Override
163
+ public String key () {
164
+ return key ;
165
+ }
166
+ @ Override
167
+ public boolean isFile () {
168
+ return false ;
169
+ }
170
+ }
171
+
172
+ private void addComplexityFileMeasures (InputFile component , NodeList values ) {
173
+ LOGGER .debug ("File measures for {}" ,component .toString ());
134
174
int complexity = Integer .parseInt (values .item (cyclomaticComplexityIndex ).getTextContent ());
175
+
135
176
context .<Integer >newMeasure ()
136
177
.on (component )
137
178
.forMetric (CoreMetrics .COMPLEXITY )
@@ -153,8 +194,8 @@ private void addComplexityFileMeasures(String fileName, NodeList values) {
153
194
.save ();
154
195
}
155
196
156
- private void addComplexityFunctionMeasures (SwiftFunction component , NodeList values ) {
157
- LOGGER .debug ("Function measures for {}" ,component .key );
197
+ private void addComplexityFunctionMeasures (InputComponent component , NodeList values ) {
198
+ LOGGER .debug ("Function measures for {}" ,component .key () );
158
199
int complexity = Integer .parseInt (values .item (cyclomaticComplexityIndex ).getTextContent ());
159
200
context .<Integer >newMeasure ()
160
201
.on (component )
@@ -169,53 +210,4 @@ private void addComplexityFunctionMeasures(SwiftFunction component, NodeList val
169
210
.withValue (numberOfLines )
170
211
.save ();
171
212
}
172
-
173
- private static class SwiftFunction implements InputModule {
174
- private String name ;
175
- private String key ;
176
- private String file ;
177
- private int lineNumber ;
178
-
179
- public SwiftFunction (String name ) {
180
- String [] vals = name .split (" at " );
181
- if (vals .length >= 2 ) {
182
- this .name = vals [0 ].replaceAll ("\\ W" ,"" );
183
-
184
- if (vals [1 ].contains (":" )) {
185
- String [] sp = vals [1 ].split (":" );
186
- this .file = sp [0 ].substring (0 ,sp [0 ].lastIndexOf ("." ));
187
- this .lineNumber = Integer .parseInt (sp [1 ]);
188
- } else {
189
- this .file = vals [1 ];
190
- this .lineNumber = 0 ;
191
- }
192
-
193
- this .key = String .format ("%s.%s:%d" , this .file , this .name , this .lineNumber );
194
- } else {
195
- this .key = name ;
196
- }
197
- }
198
-
199
- @ Override
200
- public String key () {
201
- return key ;
202
- }
203
-
204
- public String getName () {
205
- return name ;
206
- }
207
-
208
- public String getFile () {
209
- return file ;
210
- }
211
-
212
- public int getLineNumber () {
213
- return lineNumber ;
214
- }
215
-
216
- @ Override
217
- public boolean isFile () {
218
- return false ;
219
- }
220
- }
221
213
}
0 commit comments