1 package net.firstpartners.nounit.report.process; 2 3 26 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import net.firstpartners.nounit.snippet.xml.IXmlConstants; 35 import net.firstpartners.nounit.utility.NoUnitException; 36 import net.firstpartners.nounit.utility.XmlUtil; 37 38 import org.jdom.Attribute; 39 import org.jdom.Document; 40 import org.jdom.Element; 41 import org.jdom.JDOMException; 42 import org.jdom.input.SAXBuilder; 43 import org.jdom.output.XMLOutputter; 44 45 55 public class CallChainer implements ICallsXmlConstants { 56 57 61 private final static int MAX_SEARCH_DEPTH=5; 62 64 72 public void addCallChainInformation( File fullXmlFileName, 73 File outputFile, 74 String nameOfBaseClass) 75 throws NoUnitException , IOException , JDOMException { 76 77 SAXBuilder builder = new SAXBuilder(); 79 Document sourceDocument = builder.build( fullXmlFileName ); 80 81 sourceDocument=lookThroughNodes( sourceDocument, nameOfBaseClass ); 83 84 if ( outputFile.exists()) { 86 outputFile.delete(); 87 } 88 89 FileOutputStream output = new FileOutputStream ( outputFile ); 91 92 XMLOutputter fmt = new XMLOutputter(" ", true); 94 fmt.output( sourceDocument, output ); 95 } 96 97 103 private Document lookThroughNodes( Document sourceDocument, 104 String nameOfBaseClass) 105 throws JDOMException { 106 107 HashSet allNodes = XmlUtil.getAllNodes(sourceDocument); 109 Iterator nodeLoop = allNodes.iterator(); 110 111 while (nodeLoop.hasNext()){ 113 Element tmpElement=(Element)nodeLoop.next(); 114 searchForExtendsTag(sourceDocument,tmpElement,nameOfBaseClass); 115 } 116 return sourceDocument; 117 } 118 119 127 private void searchForExtendsTag( Document currentDocument, 128 Element inElement, 129 String nameOfBaseClass) 130 throws JDOMException { 131 132 String tmpString = inElement.getName(); 134 if( ( tmpString!=null ) && 135 ( tmpString.equals(IXmlConstants.ELEMENT_CLASS_EXTENDS) ) ) { 136 137 Attribute tmpAttribute = 138 inElement.getAttribute(IXmlConstants.ATTRIBUTE_NAME); 139 String tmpName = tmpAttribute.getValue(); 140 141 if( (tmpName!=null ) && ( tmpName.equals(nameOfBaseClass) ) ) { 142 startCallChain(currentDocument,inElement); 143 } 144 } 145 } 146 147 154 private void startCallChain(Document currentDocument, Element inElement) 155 throws JDOMException { 156 157 int currentCallDepth =0 ; 160 Element classElement = inElement.getParent(); 162 List possibleMethods= classElement.getChildren(); 163 Iterator possMethodsLoop = possibleMethods.iterator(); 164 165 while ( possMethodsLoop.hasNext() ) { 167 168 Element tmpMethodElement = (Element)possMethodsLoop.next(); 170 String tmpString = tmpMethodElement.getName(); 171 if( ( tmpString!=null ) && 172 ( tmpString.equals(IXmlConstants.ELEMENT_METHOD) ) ) { 173 174 updateNodeWithDepth( tmpMethodElement, 0 ); 176 updateNodeWithVolume( tmpMethodElement ); 177 178 List possibleCalls= tmpMethodElement.getChildren(); 181 Iterator possCallsLoop = possibleCalls.iterator(); 182 183 while (possCallsLoop.hasNext()){ 185 186 Element tmpCallsElement = (Element)possCallsLoop.next(); 188 tmpString=tmpCallsElement.getName(); 189 if( ( tmpString!=null ) && 190 ( tmpString.equals(IXmlConstants.ELEMENT_CALLS) ) ) { 191 192 followTheCall( currentDocument, tmpCallsElement, 195 currentCallDepth); 196 } 197 } 198 } 199 } 200 } 201 202 203 212 private void followTheCall( Document currentDocument, Element callsElement, 213 int currentSearchDepth) 214 throws JDOMException{ 215 216 Attribute tmpAttribute; 218 String findClassName; 219 String findMethodName; 220 Element possMethodElement; 221 Element nextClassElementInChain; 222 Element nextMethodElementInChain; 223 List possNextMethodElementsInChain; 224 Iterator nextElementMethodsLoop; 225 226 int localCallDepth=currentSearchDepth; 228 229 230 tmpAttribute = callsElement.getAttribute(IXmlConstants.ATTRIBUTE_CLASS); 232 if(tmpAttribute==null) {return;} 233 findClassName = tmpAttribute.getValue(); 234 235 tmpAttribute = callsElement.getAttribute(IXmlConstants.ATTRIBUTE_METHOD); 237 if(tmpAttribute==null) {return;} 238 findMethodName = tmpAttribute.getValue(); 239 240 nextClassElementInChain=XmlUtil.findNode(currentDocument, 242 IXmlConstants.ATTRIBUTE_NAME, 243 findClassName); 244 245 if (nextClassElementInChain==null) {return;} 247 248 249 possNextMethodElementsInChain = nextClassElementInChain.getChildren( 251 IXmlConstants.ELEMENT_METHOD); 252 253 nextElementMethodsLoop = possNextMethodElementsInChain.iterator(); 254 255 while(nextElementMethodsLoop.hasNext()) { 257 258 possMethodElement=(Element)nextElementMethodsLoop.next(); 260 tmpAttribute = possMethodElement.getAttribute(IXmlConstants.ATTRIBUTE_NAME); 261 if ((tmpAttribute!=null)&&(tmpAttribute.getValue().equals(findMethodName))){ 262 263 264 updateNodeWithDepth(possMethodElement,localCallDepth); 266 updateNodeWithVolume(possMethodElement); 267 268 localCallDepth++; 270 271 if (localCallDepth<MAX_SEARCH_DEPTH) { 273 followFurtherCalls(currentDocument,possMethodElement,localCallDepth); 274 } 275 } 277 } } 279 280 281 288 private void followFurtherCalls(Document currentDocument, 289 Element methodElement, 290 int currentSearchDepth) 291 throws JDOMException { 292 293 294 295 String tmpString; 297 Element tmpCallsElement; 298 List possibleCalls; 299 Iterator possCallsLoop; 300 301 possibleCalls= methodElement.getChildren(); 302 possCallsLoop = possibleCalls.iterator(); 303 304 while (possCallsLoop.hasNext()){ 306 307 tmpCallsElement = (Element)possCallsLoop.next(); 309 tmpString=tmpCallsElement.getName(); 310 if((tmpString!=null)&&(tmpString.equals(IXmlConstants.ELEMENT_CALLS))){ 311 312 followTheCall(currentDocument,tmpCallsElement,currentSearchDepth); 315 316 } 318 } 320 } 321 322 323 324 325 330 private void updateNodeWithDepth(Element nodeToUpdate, 331 int currentSearchDepth) { 332 int previousCallDepth; 334 int currentCallDepth=currentSearchDepth; Attribute tmpAttribute; 336 337 338 339 341 try { 343 tmpAttribute = nodeToUpdate.getAttribute(ATTRIBUTE_MIN_CALL_DEPTH); 344 if (tmpAttribute!=null) { 345 previousCallDepth=Integer.parseInt(tmpAttribute.getValue()); 346 347 if (currentSearchDepth>previousCallDepth){ 349 currentSearchDepth=previousCallDepth; 350 } 351 } 352 } catch (NumberFormatException nfe) { 353 } 355 356 357 tmpAttribute = new Attribute(ATTRIBUTE_MIN_CALL_DEPTH, 359 String.valueOf(currentSearchDepth)); 360 nodeToUpdate.setAttribute(tmpAttribute); 361 362 363 364 } 365 366 371 private void updateNodeWithVolume(Element nodeToUpdate) { 372 int callVolume=1; Attribute tmpAttribute; 375 376 377 378 380 try { 382 tmpAttribute = nodeToUpdate.getAttribute(ATTRIBUTE_NUMBER_OF_CALLS); 383 if (tmpAttribute!=null) { 384 callVolume=Integer.parseInt(tmpAttribute.getValue()); 385 386 callVolume++; 388 } 389 } catch (NumberFormatException nfe) { 390 } 392 393 394 tmpAttribute = new Attribute(ATTRIBUTE_NUMBER_OF_CALLS, 396 String.valueOf(callVolume)); 397 nodeToUpdate.setAttribute(tmpAttribute); 398 399 400 } 401 402 } 403 404 405 | Popular Tags |