1 26 27 package net.sourceforge.groboutils.codecoverage.v2.report; 28 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.FileReader ; 32 import java.io.IOException ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Set ; 39 import java.util.Collection ; 40 41 import javax.xml.parsers.DocumentBuilder ; 42 import javax.xml.parsers.DocumentBuilderFactory ; 43 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 49 50 62 public class XmlSourceReportGenerator2 implements IXmlSourceReportConst 63 { 64 private static final org.apache.log4j.Logger LOG = 65 org.apache.log4j.Logger.getLogger( XmlCombinedReportGenerator.class ); 66 67 68 private SourceNodeMap sourceNodeMap = new SourceNodeMap(); 69 private Node moduleTypesNode; 70 71 private static class SourceNodes 72 { 73 public List coverNodes = new LinkedList (); 74 public Map uncoveredMarkNodesByLine = new HashMap (); 75 public List noLine = new LinkedList (); 76 77 public void addCoverNode( Element e ) 78 { 79 this.coverNodes.add( e ); 81 NodeList nl = e.getElementsByTagName( T_MARK ); 82 for (int i = 0; i < nl.getLength(); ++i) 83 { 84 Node node = nl.item(i); 85 if (node instanceof Element ) 86 { 87 Element me = (Element )node; 88 if (!"true".equals( me.getAttribute( A_COVERED ) )) 89 { 90 Element methodNode = (Element )me.getParentNode(); 91 me.setAttribute( A_METHODSIGNATURE, 92 methodNode.getAttribute( A_METHODSIGNATURE ) ); 93 me.setAttribute( A_METHODSIGNATURE_REAL, 94 methodNode.getAttribute( A_METHODSIGNATURE_REAL ) ); 95 String line = me.getAttribute( A_SOURCELINE ); 96 List marks; 97 if ("-1".equals( line )) 98 { 99 marks = this.noLine; 100 } 101 else 102 { 103 marks = (List )this.uncoveredMarkNodesByLine. 104 get( line ); 105 if (marks == null) 106 { 107 marks = new LinkedList (); 108 this.uncoveredMarkNodesByLine.put( line, marks ); 109 } 110 } 111 marks.add( me ); 112 } 113 } 114 } 115 } 116 117 118 public void copyClassCoverInto( Element rootEl, Document doc ) 119 { 120 copyNodesInto( this.coverNodes, rootEl, doc ); 121 } 122 123 124 public int getNoLineMarkCount() 125 { 126 return this.noLine.size(); 127 } 128 129 130 public void copyMarksInto( int lineNo, Element rootEl, Document doc ) 131 { 132 List marks; 133 if (lineNo < 0) 134 { 135 marks = this.noLine; 136 } 137 else 138 { 139 marks = (List )this.uncoveredMarkNodesByLine.get( 140 Integer.toString( lineNo ) ); 141 } 142 copyNodesInto( marks, rootEl, doc ); 143 } 144 145 146 public boolean copyAllMarksInto( Element rootEl, Document doc ) 147 { 148 boolean added = false; 149 Iterator iter = this.uncoveredMarkNodesByLine.keySet().iterator(); 150 while (iter.hasNext()) 151 { 152 added = copyNodesInto( (List )this.uncoveredMarkNodesByLine. 153 get( iter.next() ), rootEl, doc ); 154 } 155 return added; 156 } 157 158 159 private boolean copyNodesInto( Collection src, Element rootEl, Document doc ) 160 { 161 boolean added =false; 162 if (src != null) 163 { 164 Iterator iter = src.iterator(); 165 while (iter.hasNext()) 166 { 167 added = true; 168 rootEl.appendChild( copyNode( (Node )iter.next(), doc ) ); 169 } 170 } 171 return added; 172 } 173 } 174 175 private static class SourceNodeMap 176 { 177 private Map sourceFileMap = new HashMap (); 178 public void addClassCoverageNode( Node n ) 179 { 180 if (n instanceof Element ) 181 { 182 Element e = (Element )n; 183 String name = getSourceName( e ); 184 SourceNodes sn = getSource( name ); 185 if (sn == null) 186 { 187 sn = new SourceNodes(); 188 this.sourceFileMap.put( name, sn ); 189 } 190 sn.addCoverNode( e ); 191 } 192 } 193 194 public SourceNodes getSource( String name ) 195 { 196 return (SourceNodes)this.sourceFileMap.get( name ); 197 } 198 199 public String [] getSourceNames() 200 { 201 Set keys = this.sourceFileMap.keySet(); 202 return (String [])keys.toArray( new String [ keys.size() ] ); 203 } 204 } 205 206 public XmlSourceReportGenerator2( Document coverageReport ) 207 { 208 if (coverageReport == null) 209 { 210 throw new IllegalArgumentException ( "No null args" ); 211 } 212 213 NodeList list = coverageReport.getDocumentElement(). 214 getElementsByTagName( T_CLASSCOVERAGE ); 215 for (int i = 0; i < list.getLength(); ++i) 216 { 217 this.sourceNodeMap.addClassCoverageNode( list.item(i) ); 218 } 219 220 this.moduleTypesNode = coverageReport.getDocumentElement(). 221 getElementsByTagName( "moduletypes" ).item(0); 222 } 223 224 225 public String [] getSourceNames() 226 { 227 return this.sourceNodeMap.getSourceNames(); 228 } 229 230 231 237 public Document createXML( String srcName, File srcFile ) 238 throws IOException 239 { 240 SourceNodes coverageNodes = this.sourceNodeMap.getSource( srcName ); 241 if (coverageNodes == null) 242 { 243 throw new IllegalArgumentException ( "No such source: "+ 244 srcName ); 245 } 246 247 DocumentBuilder docBuilder = getDocumentBuilder(); 249 Document doc = docBuilder.newDocument(); 250 Element rootEl = doc.createElement( T_JAVACOVERAGE ); 251 doc.appendChild( rootEl ); 252 rootEl.setAttribute( A_JAVACLASS, srcName ); 253 rootEl.appendChild( copyNode( this.moduleTypesNode, doc ) ); 254 255 parseSourceFile( srcFile, rootEl, doc, coverageNodes ); 256 coverageNodes.copyClassCoverInto( rootEl, doc ); 257 258 return doc; 259 } 260 261 262 protected void parseSourceFile( File src, Element parent, Document doc, 263 SourceNodes sourceNode ) 264 throws IOException 265 { 266 Element nolineEl = null; 268 if (sourceNode.getNoLineMarkCount() > 0) 269 { 270 nolineEl = doc.createElement( T_NO_LINE ); 271 sourceNode.copyMarksInto( -1, nolineEl, doc ); 272 } 273 274 if (src == null || !src.exists() || !src.isFile()) 276 { 277 Element nosrcEl = doc.createElement( T_NO_SOURCE ); 278 parent.appendChild( nosrcEl ); 279 280 if (nolineEl != null) 282 { 283 nosrcEl.appendChild( nolineEl ); 284 nolineEl.setAttribute( A_INDEX, Integer.toString( -1 ) ); 285 nolineEl.setAttribute( A_SRC, "Could not find source file." ); 286 } 287 288 Element nsmEl = doc.createElement( T_NO_SOURCE_MARKS ); 289 if (sourceNode.copyAllMarksInto( nsmEl, doc )) 290 { 291 nosrcEl.appendChild( nsmEl ); 292 } 293 294 return; 295 } 296 297 Element srcEl = doc.createElement( T_SOURCE ); 299 parent.appendChild( srcEl ); 300 if (nolineEl != null) 301 { 302 srcEl.appendChild( nolineEl ); 303 } 304 305 306 BufferedReader in = new BufferedReader ( new FileReader ( src ) ); 307 try 308 { 309 int lineNo = 0; 310 for (String line = in.readLine(); 311 line != null; 312 line = in.readLine()) 313 { 314 ++lineNo; 315 316 Element lineEl = doc.createElement( T_LINE ); 320 srcEl.appendChild( lineEl ); 321 322 lineEl.setAttribute( A_INDEX, Integer.toString( lineNo ) ); 323 lineEl.setAttribute( A_SRC, line ); 324 325 sourceNode.copyMarksInto( lineNo, lineEl, doc ); 326 } 327 } 328 finally 329 { 330 if (in != null) 331 { 332 in.close(); 333 } 334 } 335 } 336 337 338 protected static Node copyNode( Node el, Document doc ) 339 { 340 return doc.importNode( el, true ); 341 } 342 343 344 345 348 349 private static DocumentBuilder getDocumentBuilder() 350 { 351 try 352 { 353 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 354 } 355 catch (Exception exc) 356 { 357 throw new ExceptionInInitializerError (exc); 358 } 359 } 360 361 362 private static String getSourceName( Element coverageEl ) 363 { 364 String pkg = coverageEl.getAttribute( A_PACKAGE ); 365 String src = coverageEl.getAttribute( A_SOURCEFILE ); 366 return makeSourceName( pkg, src ); 367 } 368 369 370 private static String makeSourceName( String pkg, String srcFile ) 371 { 372 if (pkg == null || pkg.length() <= 0) 373 { 374 return srcFile; 375 } 376 pkg = pkg.replace( '.', File.separatorChar ); 377 return pkg + File.separatorChar + srcFile; 378 } 379 } 380 381 | Popular Tags |