| 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 | */ |
|---|
| 33 | package org.jcoderz.phoenix.report; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.io.FileInputStream; |
|---|
| 37 | import java.io.IOException; |
|---|
| 38 | import java.util.ArrayList; |
|---|
| 39 | import java.util.HashMap; |
|---|
| 40 | import java.util.Iterator; |
|---|
| 41 | import java.util.List; |
|---|
| 42 | import java.util.Map; |
|---|
| 43 | import java.util.logging.Logger; |
|---|
| 44 | |
|---|
| 45 | import javax.xml.bind.JAXBException; |
|---|
| 46 | |
|---|
| 47 | import org.jcoderz.phoenix.jcoverage.jaxb.Clazz; |
|---|
| 48 | import org.jcoderz.phoenix.jcoverage.jaxb.Coverage; |
|---|
| 49 | import org.jcoderz.phoenix.jcoverage.jaxb.LineType; |
|---|
| 50 | import org.jcoderz.phoenix.report.jaxb.Item; |
|---|
| 51 | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * @author Michael Griffel |
|---|
| 55 | */ |
|---|
| 56 | public class JCoverageReportReader |
|---|
| 57 | extends AbstractReportReader |
|---|
| 58 | { |
|---|
| 59 | /** JAXB context path. */ |
|---|
| 60 | public static final String JCOVERAGE_JAXB_CONTEXT_PATH |
|---|
| 61 | = "org.jcoderz.phoenix.jcoverage.jaxb"; |
|---|
| 62 | |
|---|
| 63 | private static final String CLASSNAME |
|---|
| 64 | = JCoverageReportReader.class.getName(); |
|---|
| 65 | |
|---|
| 66 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 67 | |
|---|
| 68 | private Coverage mReportDocument; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | JCoverageReportReader () |
|---|
| 72 | throws JAXBException |
|---|
| 73 | { |
|---|
| 74 | super(JCOVERAGE_JAXB_CONTEXT_PATH); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** {@inheritDoc} */ |
|---|
| 78 | public final void parse (File f) |
|---|
| 79 | throws JAXBException |
|---|
| 80 | { |
|---|
| 81 | try |
|---|
| 82 | { |
|---|
| 83 | mReportDocument = (Coverage) getUnmarshaller().unmarshal( |
|---|
| 84 | new FileInputStream(f)); |
|---|
| 85 | } |
|---|
| 86 | catch (IOException e) |
|---|
| 87 | { |
|---|
| 88 | throw new JAXBException("Cannot read JCoverage report", e); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** {@inheritDoc} */ |
|---|
| 93 | public final Map getItems () |
|---|
| 94 | throws JAXBException |
|---|
| 95 | { |
|---|
| 96 | final Map itemMap = new HashMap(); |
|---|
| 97 | final List files = mReportDocument.getClazzes(); |
|---|
| 98 | final String baseDir = mReportDocument.getSrc() + File.separator; |
|---|
| 99 | |
|---|
| 100 | for (final Iterator iterator = files.iterator(); iterator.hasNext(); ) |
|---|
| 101 | { |
|---|
| 102 | final Clazz clazz = (Clazz) iterator.next(); |
|---|
| 103 | logger.finer("Processing class '" + clazz.getName() + "'"); |
|---|
| 104 | final String javaFile = clazzname2Filename (clazz.getName()); |
|---|
| 105 | final List itemList = new ArrayList(); |
|---|
| 106 | |
|---|
| 107 | for (final Iterator i = clazz.getCoveredLines().iterator(); |
|---|
| 108 | i.hasNext(); ) |
|---|
| 109 | { |
|---|
| 110 | final LineType line = (LineType) i.next(); |
|---|
| 111 | final Item item = new ObjectFactory().createItem(); |
|---|
| 112 | item.setOrigin(Origin.COVERAGE); |
|---|
| 113 | item.setCounter(line.getHits()); |
|---|
| 114 | item.setLine(line.getNumber()); |
|---|
| 115 | item.setSeverity(Severity.COVERAGE); |
|---|
| 116 | item.setFindingType("coverage"); // FIXME: use type |
|---|
| 117 | |
|---|
| 118 | itemList.add(item); |
|---|
| 119 | } |
|---|
| 120 | final ResourceInfo info |
|---|
| 121 | = ResourceInfo.lookup(normalizeFileName(baseDir + javaFile)); |
|---|
| 122 | |
|---|
| 123 | if (info != null) |
|---|
| 124 | { |
|---|
| 125 | if (itemMap.containsKey(info)) |
|---|
| 126 | { |
|---|
| 127 | final List l = (List) itemMap.get(info); |
|---|
| 128 | l.addAll(itemList); |
|---|
| 129 | } |
|---|
| 130 | else |
|---|
| 131 | { |
|---|
| 132 | itemMap.put(info, itemList); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | else |
|---|
| 136 | { |
|---|
| 137 | logger.finer( |
|---|
| 138 | "Ignoring findings for resource " + baseDir + javaFile); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | return itemMap; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | private final String clazzname2Filename (String c) |
|---|
| 146 | { |
|---|
| 147 | return c.replaceAll("\\.", "/") + ".java"; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|