2020import  org .slf4j .Logger ;
2121import  org .slf4j .LoggerFactory ;
2222import  org .sonar .api .batch .fs .FilePredicate ;
23+ import  org .sonar .api .batch .fs .InputComponent ;
2324import  org .sonar .api .batch .fs .InputFile ;
24- import  org .sonar .api .batch .fs .InputModule ;
25+ import  org .sonar .api .batch .fs .internal . DefaultInputComponent ;
2526import  org .sonar .api .batch .sensor .SensorContext ;
2627import  org .sonar .api .measures .CoreMetrics ;
2728import  org .w3c .dom .Document ;
@@ -115,23 +116,63 @@ private void parseMeasure(String type, NodeList itemList) {
115116
116117                NodeList  values  = itemElement .getElementsByTagName (VALUE );
117118                if  (FILE_MEASURE .equalsIgnoreCase (type )) {
118-                     addComplexityFileMeasures (name , values );
119+                     InputFile  inputFile  = getFile (name );
120+                     addComplexityFileMeasures (inputFile , values );
119121                } else  if  (FUNCTION_MEASURE .equalsIgnoreCase (type )) {
120-                     addComplexityFunctionMeasures (new  SwiftFunction (name ), values );
122+                     addComplexityFunctionMeasures (new  SwiftFunction (0 , name ), values );
121123                }
122124            }
123125        }
124126    }
125127
126-     private  void  addComplexityFileMeasures (String  fileName , NodeList  values ) {
127-         LOGGER .debug ("File measures for {}" ,fileName );
128+     private  InputFile  getFile (String  fileName ){
128129        FilePredicate  fp  = context .fileSystem ().predicates ().hasRelativePath (fileName );
129130        if  (!context .fileSystem ().hasFiles (fp )) {
130131            LOGGER .warn ("file not included in sonar {}" , fileName );
131-             return ;
132+             return   null ;
132133        }
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 ());
134174        int  complexity  = Integer .parseInt (values .item (cyclomaticComplexityIndex ).getTextContent ());
175+ 
135176        context .<Integer >newMeasure ()
136177            .on (component )
137178            .forMetric (CoreMetrics .COMPLEXITY )
@@ -153,8 +194,8 @@ private void addComplexityFileMeasures(String fileName, NodeList values) {
153194            .save ();
154195    }
155196
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 () );
158199        int  complexity  = Integer .parseInt (values .item (cyclomaticComplexityIndex ).getTextContent ());
159200        context .<Integer >newMeasure ()
160201            .on (component )
@@ -169,53 +210,4 @@ private void addComplexityFunctionMeasures(SwiftFunction component, NodeList val
169210            .withValue (numberOfLines )
170211            .save ();
171212    }
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-     }
221213}
0 commit comments