001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2008 Julian Gamble 
005     *
006     * Cobertura is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published
008     * by the Free Software Foundation; either version 2 of the License,
009     * or (at your option) any later version.
010     *
011     * Cobertura is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with Cobertura; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019     * USA
020     */
021    
022    package net.sourceforge.cobertura.reporting.xml;
023    
024    import java.io.File;
025    import java.io.IOException;
026    import java.io.PrintWriter;
027    import java.util.Date;
028    
029    import net.sourceforge.cobertura.coveragedata.ProjectData;
030    import net.sourceforge.cobertura.reporting.ComplexityCalculator;
031    import net.sourceforge.cobertura.util.FileFinder;
032    import net.sourceforge.cobertura.util.Header;
033    import net.sourceforge.cobertura.util.IOUtil;
034    
035    public class SummaryXMLReport
036    {
037            
038    
039            private final PrintWriter pw;
040            private int indent = 0;
041    
042            
043            public SummaryXMLReport(ProjectData projectData, File destinationDir,
044                            FileFinder finder, ComplexityCalculator complexity) throws IOException
045            {
046                    File file = new File(destinationDir, "coverage-summary.xml");
047                    pw = IOUtil.getPrintWriter(file);
048    
049                    try
050                    {
051                            println("<?xml version=\"1.0\"?>");
052                            println("<!DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/"
053                                            + XMLReport.coverageDTD + "\">");
054                            println("");
055    
056                            double ccn = complexity.getCCNForProject(projectData);
057                            int numLinesCovered = projectData.getNumberOfCoveredLines();
058                            int numLinesValid = projectData.getNumberOfValidLines();
059                            int numBranchesCovered = projectData.getNumberOfCoveredBranches();
060                            int numBranchesValid = projectData.getNumberOfValidBranches();
061    
062                            
063                            // TODO: Set a schema?
064                            //println("<coverage " + sourceDirectories.toString() + " xmlns=\"http://cobertura.sourceforge.net\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://cobertura.sourceforge.net/xml/coverage.xsd\">");
065                            println(
066                                            "<coverage line-rate=\"" + projectData.getLineCoverageRate()
067                                            + "\" branch-rate=\"" + projectData.getBranchCoverageRate()
068                                            + "\" lines-covered=\"" + numLinesCovered
069                                            + "\" lines-valid=\"" + numLinesValid
070                                            + "\" branches-covered=\"" + numBranchesCovered
071                                            + "\" branches-valid=\"" + numBranchesValid
072    
073                                            + "\" complexity=\"" + ccn
074    
075                                            + "\" version=\"" + Header.version()
076                                            + "\" timestamp=\"" + new Date().getTime()
077                                            + "\">");
078    
079                            //the DTD requires a "packages" element
080                            increaseIndentation();
081                            println("<packages />");
082                            decreaseIndentation();
083                            
084                            println("</coverage>");
085                    }
086                    finally
087                    {
088                            pw.close();
089                    }
090    
091            }
092            
093            void increaseIndentation()
094            {
095                    indent++;
096            }
097    
098            void decreaseIndentation()
099            {
100                    if (indent > 0)
101                            indent--;
102            }
103    
104            private void println(String ln)
105            {
106                    indent();
107                    pw.println(ln);
108            }
109    
110            private void indent()
111            {
112                    for (int i = 0; i < indent; i++)
113                    {
114                            pw.print("\t");
115                    }
116            }
117    }