KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > report > XmlSourceReportGenerator


1 /*
2  * @(#)XmlSourceReportGenerator.java
3  *
4  * Copyright (C) 2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.report;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39
40 import javax.xml.parsers.DocumentBuilder JavaDoc;
41 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47
48
49 /**
50  * Generates an XML report about a Java source file, containing the source
51  * file's line-by-line source, and the coverage information on that class.
52  * <p>
53  * Unlike the other generators, this class must be instantiated for a
54  * particular coverage report, and will be reused to make each sub-source
55  * file.
56  *
57  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
58  * @version $Date: 2004/04/15 05:48:26 $
59  * @since November 26, 2003
60  */

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 JavaDoc sourceFileMap = new HashMap JavaDoc();
68     private Node JavaDoc moduleTypesNode;
69     
70     
71     public XmlSourceReportGenerator( Document JavaDoc coverageReport )
72     {
73         if (coverageReport == null)
74         {
75             throw new IllegalArgumentException JavaDoc( "No null args" );
76         }
77         
78         NodeList JavaDoc list = coverageReport.getDocumentElement().
79             getElementsByTagName( T_CLASSCOVERAGE );
80         for (int i = 0; i < list.getLength(); ++i)
81         {
82             Node JavaDoc node = list.item(i);
83             if (node instanceof Element JavaDoc)
84             {
85                 String JavaDoc name = getSourceName( (Element JavaDoc)node );
86                 List JavaDoc nodes = (List JavaDoc)this.sourceFileMap.get( name );
87                 if (nodes == null)
88                 {
89                     nodes = new LinkedList JavaDoc();
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 JavaDoc[] getSourceNames()
102     {
103         Set JavaDoc keys = this.sourceFileMap.keySet();
104         return (String JavaDoc[])keys.toArray( new String JavaDoc[ keys.size() ] );
105     }
106     
107     
108     /**
109      * Sends the generated report to <tt>out</tt> using the given module
110      * and data set.
111      *
112      * @return the root element generated.
113      */

114     public Document JavaDoc createXML( String JavaDoc srcName, File JavaDoc srcFile )
115             throws IOException JavaDoc
116     {
117         List JavaDoc coverageNodes = (List JavaDoc)this.sourceFileMap.get( srcName );
118         if (coverageNodes == null)
119         {
120             throw new IllegalArgumentException JavaDoc( "No such source: "+
121                 srcName );
122         }
123         
124         // this is a bit slower, but it conserves some memory resources.
125
DocumentBuilder JavaDoc docBuilder = getDocumentBuilder();
126         Document JavaDoc doc = docBuilder.newDocument();
127         Element JavaDoc 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 JavaDoc iter = coverageNodes.iterator();
134         while (iter.hasNext())
135         {
136             rootEl.appendChild( copyNode( (Node JavaDoc)iter.next(), doc ) );
137         }
138         
139         return doc;
140     }
141     
142     
143     protected void parseSourceFile( File JavaDoc src, Element JavaDoc parent, Document JavaDoc doc )
144             throws IOException JavaDoc
145     {
146         Element JavaDoc srcEl = doc.createElement( T_SOURCE );
147         parent.appendChild( srcEl );
148         
149         // If the file doesn't exist, don't fail. Just put it in the report.
150
if (src == null || !src.exists() || !src.isFile())
151         {
152             // System.out.println( "No source file" );
153
Element JavaDoc 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         // else, parse the source file.
160

161         BufferedReader JavaDoc in = new BufferedReader JavaDoc( new FileReader JavaDoc( src ) );
162         try
163         {
164             int lineNo = 0;
165             for (String JavaDoc line = in.readLine();
166                     line != null;
167                     line = in.readLine())
168             {
169                 ++lineNo;
170                 
171                 // should color-code the line according to Java syntax.
172
// But, we can't guarantee that the source is Java! It
173
// might be NetRexx or any number of other source types.
174
Element JavaDoc 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 JavaDoc copyNode( Node JavaDoc el, Document JavaDoc doc )
192     {
193         return doc.importNode( el, true );
194     }
195     
196     
197     
198     //------------------------------------------------------------------------
199
// Private methods
200

201     
202     private static DocumentBuilder JavaDoc getDocumentBuilder()
203     {
204         try
205         {
206             return DocumentBuilderFactory.newInstance().newDocumentBuilder();
207         }
208         catch (Exception JavaDoc exc)
209         {
210             throw new ExceptionInInitializerError JavaDoc(exc);
211         }
212     }
213     
214     
215     private static String JavaDoc getSourceName( Element JavaDoc coverageEl )
216     {
217         String JavaDoc pkg = coverageEl.getAttribute( A_PACKAGE );
218         String JavaDoc src = coverageEl.getAttribute( A_SOURCEFILE );
219         return makeSourceName( pkg, src );
220     }
221     
222     
223     private static String JavaDoc makeSourceName( String JavaDoc pkg, String JavaDoc 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