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 40 import javax.xml.parsers.DocumentBuilder ; 41 import javax.xml.parsers.DocumentBuilderFactory ; 42 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 48 49 61 public class XmlSourceReportGenerator implements IXmlSourceReportConst 62 { 63 private static final org.apache.log4j.Logger LOG = 64 org.apache.log4j.Logger.getLogger( XmlCombinedReportGenerator.class ); 65 66 67 private Map sourceFileMap = new HashMap (); 68 private Node moduleTypesNode; 69 70 71 public XmlSourceReportGenerator( Document coverageReport ) 72 { 73 if (coverageReport == null) 74 { 75 throw new IllegalArgumentException ( "No null args" ); 76 } 77 78 NodeList list = coverageReport.getDocumentElement(). 79 getElementsByTagName( T_CLASSCOVERAGE ); 80 for (int i = 0; i < list.getLength(); ++i) 81 { 82 Node node = list.item(i); 83 if (node instanceof Element ) 84 { 85 String name = getSourceName( (Element )node ); 86 List nodes = (List )this.sourceFileMap.get( name ); 87 if (nodes == null) 88 { 89 nodes = new LinkedList (); 90 this.sourceFileMap.put( name, nodes ); 91 } 92 nodes.add( node ); 93 } 94 } 95 96 this.moduleTypesNode = coverageReport.getDocumentElement(). 97 getElementsByTagName( "moduletypes" ).item(0); 98 } 99 100 101 public String [] getSourceNames() 102 { 103 Set keys = this.sourceFileMap.keySet(); 104 return (String [])keys.toArray( new String [ keys.size() ] ); 105 } 106 107 108 114 public Document createXML( String srcName, File srcFile ) 115 throws IOException 116 { 117 List coverageNodes = (List )this.sourceFileMap.get( srcName ); 118 if (coverageNodes == null) 119 { 120 throw new IllegalArgumentException ( "No such source: "+ 121 srcName ); 122 } 123 124 DocumentBuilder docBuilder = getDocumentBuilder(); 126 Document doc = docBuilder.newDocument(); 127 Element rootEl = doc.createElement( T_JAVACOVERAGE ); 128 doc.appendChild( rootEl ); 129 rootEl.setAttribute( A_JAVACLASS, srcName ); 130 rootEl.appendChild( copyNode( this.moduleTypesNode, doc ) ); 131 132 parseSourceFile( srcFile, rootEl, doc ); 133 Iterator iter = coverageNodes.iterator(); 134 while (iter.hasNext()) 135 { 136 rootEl.appendChild( copyNode( (Node )iter.next(), doc ) ); 137 } 138 139 return doc; 140 } 141 142 143 protected void parseSourceFile( File src, Element parent, Document doc ) 144 throws IOException 145 { 146 Element srcEl = doc.createElement( T_SOURCE ); 147 parent.appendChild( srcEl ); 148 149 if (src == null || !src.exists() || !src.isFile()) 151 { 152 Element lineEl = doc.createElement( T_LINE ); 154 srcEl.appendChild( lineEl ); 155 lineEl.setAttribute( A_INDEX, Integer.toString( 0 ) ); 156 lineEl.setAttribute( A_SRC, "Could not find source file." ); 157 return; 158 } 159 161 BufferedReader in = new BufferedReader ( new FileReader ( src ) ); 162 try 163 { 164 int lineNo = 0; 165 for (String line = in.readLine(); 166 line != null; 167 line = in.readLine()) 168 { 169 ++lineNo; 170 171 Element lineEl = doc.createElement( T_LINE ); 175 srcEl.appendChild( lineEl ); 176 177 lineEl.setAttribute( A_INDEX, Integer.toString( lineNo ) ); 178 lineEl.setAttribute( A_SRC, line ); 179 } 180 } 181 finally 182 { 183 if (in != null) 184 { 185 in.close(); 186 } 187 } 188 } 189 190 191 protected Node copyNode( Node el, Document doc ) 192 { 193 return doc.importNode( el, true ); 194 } 195 196 197 198 201 202 private static DocumentBuilder getDocumentBuilder() 203 { 204 try 205 { 206 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 207 } 208 catch (Exception exc) 209 { 210 throw new ExceptionInInitializerError (exc); 211 } 212 } 213 214 215 private static String getSourceName( Element coverageEl ) 216 { 217 String pkg = coverageEl.getAttribute( A_PACKAGE ); 218 String src = coverageEl.getAttribute( A_SOURCEFILE ); 219 return makeSourceName( pkg, src ); 220 } 221 222 223 private static String makeSourceName( String pkg, String srcFile ) 224 { 225 if (pkg == null || pkg.length() <= 0) 226 { 227 return srcFile; 228 } 229 pkg = pkg.replace( '.', File.separatorChar ); 230 return pkg + File.separatorChar + srcFile; 231 } 232 } 233 234 | Popular Tags |