root/trunk/src/java/org/jcoderz/phoenix/report/CoberturaReportReader.java

Revision 1011, 5.8 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.phoenix.report;
34
35import java.io.File;
36import java.io.FileInputStream;
37import java.io.IOException;
38import java.util.ArrayList;
39import java.util.HashMap;
40import java.util.Iterator;
41import java.util.List;
42import java.util.Map;
43import java.util.logging.Logger;
44
45import javax.xml.bind.JAXBException;
46
47import org.jcoderz.phoenix.coverage.jaxb.ClassType;
48import org.jcoderz.phoenix.coverage.jaxb.Coverage;
49import org.jcoderz.phoenix.coverage.jaxb.LineType;
50import org.jcoderz.phoenix.coverage.jaxb.MethodType;
51import org.jcoderz.phoenix.coverage.jaxb.PackageType;
52import org.jcoderz.phoenix.report.jaxb.Item;
53import org.jcoderz.phoenix.report.jaxb.ObjectFactory;
54
55/**
56 * Reads the coverage report generated by cobertura.
57 *
58 * @author Michael Griffel
59 */
60public class CoberturaReportReader
61        extends AbstractReportReader
62{
63    /** JAXB context path. */
64    public static final String JCOVERAGE_JAXB_CONTEXT_PATH
65            = "org.jcoderz.phoenix.coverage.jaxb";
66
67    private static final String CLASSNAME
68            = CoberturaReportReader.class.getName();
69
70    private static final Logger logger = Logger.getLogger(CLASSNAME);
71
72    private Coverage mReportDocument;
73
74
75    CoberturaReportReader ()
76            throws JAXBException
77    {
78        super(JCOVERAGE_JAXB_CONTEXT_PATH);
79    }
80
81    /** {@inheritDoc} */
82    public final void parse (File f)
83            throws JAXBException
84    {
85        try
86        {
87            mReportDocument = (Coverage) unmarshall(new FileInputStream(f));
88        }
89        catch (IOException e)
90        {
91            throw new JAXBException("Cannot read JCoverage report", e);
92        }
93    }
94
95    /** {@inheritDoc} */
96    public final Map getItems ()
97        throws JAXBException
98    {
99        final Map itemMap = new HashMap();
100
101        final String baseDir = mReportDocument.getSources().getSource().get(0)
102                + File.separator;
103
104        for (final Iterator pkgIterator = mReportDocument.getPackages()
105                .getPackage().iterator(); pkgIterator.hasNext();)
106        {
107            final PackageType currentPackage = (PackageType) pkgIterator.next();
108            for (final Iterator clazzIterator
109                    = currentPackage.getClasses().getClazzes().iterator();
110                    clazzIterator.hasNext();)
111            {
112                final ClassType clazz = (ClassType) clazzIterator.next();
113                processClazz(itemMap, baseDir, clazz);
114            }
115        }
116
117        return itemMap;
118    }
119
120    private void processClazz (Map itemMap, final String baseDir,
121            final ClassType clazz)
122            throws JAXBException
123    {
124        logger.finer("Processing class '" + clazz.getName() + "'");
125
126        final String javaFile = clazzname2Filename(clazz.getName());
127        final List itemList = new ArrayList();
128
129        for (final Iterator methodIterator = clazz.getMethods().getMethod()
130                .iterator(); methodIterator.hasNext();)
131        {
132            final MethodType method = (MethodType) methodIterator.next();
133            for (final Iterator lineIterator = method.getLines().getLine()
134                    .iterator(); lineIterator.hasNext();)
135            {
136                final LineType line = (LineType) lineIterator.next();
137
138                final Item item = new ObjectFactory().createItem();
139                item.setOrigin(Origin.COVERAGE);
140                item.setCounter(line.getHits());
141                item.setLine(line.getNumber());
142                item.setSeverity(Severity.COVERAGE);
143                item.setFindingType("coverage"); // FIXME: use type
144
145                itemList.add(item);
146            }
147        }
148        final ResourceInfo info = ResourceInfo.lookup(
149                normalizeFileName(baseDir + javaFile));
150
151        if (info != null)
152        {
153            if (itemMap.containsKey(info))
154            {
155                final List l = (List) itemMap.get(info);
156                l.addAll(itemList);
157            }
158            else
159            {
160                itemMap.put(info, itemList);
161            }
162        }
163        else
164        {
165            logger.finer(
166                    "Ignoring findings for resource " + baseDir + javaFile);
167        }
168    }
169
170    private final String clazzname2Filename (String c)
171    {
172        return c.replaceAll("\\.", "/") + ".java";
173    }
174
175}
Note: See TracBrowser for help on using the browser.