| 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.FileNotFoundException; |
|---|
| 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.Level; |
|---|
| 44 | import java.util.logging.Logger; |
|---|
| 45 | |
|---|
| 46 | import javax.xml.bind.JAXBException; |
|---|
| 47 | |
|---|
| 48 | import org.jcoderz.commons.util.IoUtil; |
|---|
| 49 | import org.jcoderz.phoenix.checkstyle.jaxb.Checkstyle; |
|---|
| 50 | import org.jcoderz.phoenix.report.jaxb.Item; |
|---|
| 51 | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * This class is used to read the XML report from checkstyle. |
|---|
| 55 | * |
|---|
| 56 | * The checkstyle report is also transformed to the jCoderZ.org format. |
|---|
| 57 | * |
|---|
| 58 | * @author Michael Griffel |
|---|
| 59 | */ |
|---|
| 60 | public final class CheckstyleReportReader |
|---|
| 61 | extends AbstractReportReader |
|---|
| 62 | { |
|---|
| 63 | /** JAXB context path. */ |
|---|
| 64 | public static final String CHECKSTYLE_JAXB_CONTEXT_PATH |
|---|
| 65 | = "org.jcoderz.phoenix.checkstyle.jaxb"; |
|---|
| 66 | |
|---|
| 67 | private static final String CLASSNAME |
|---|
| 68 | = CheckstyleReportReader.class.getName(); |
|---|
| 69 | |
|---|
| 70 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 71 | |
|---|
| 72 | private Checkstyle mReportDocument; |
|---|
| 73 | |
|---|
| 74 | CheckstyleReportReader () |
|---|
| 75 | throws JAXBException |
|---|
| 76 | { |
|---|
| 77 | super(CHECKSTYLE_JAXB_CONTEXT_PATH); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** {@inheritDoc} */ |
|---|
| 81 | public void parse (File f) |
|---|
| 82 | throws FileNotFoundException, JAXBException |
|---|
| 83 | { |
|---|
| 84 | final FileInputStream is = new FileInputStream(f); |
|---|
| 85 | try |
|---|
| 86 | { |
|---|
| 87 | mReportDocument |
|---|
| 88 | = (Checkstyle) getUnmarshaller().unmarshal(is); |
|---|
| 89 | } |
|---|
| 90 | finally |
|---|
| 91 | { |
|---|
| 92 | IoUtil.close(is); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | protected Map getItems () |
|---|
| 97 | throws JAXBException |
|---|
| 98 | { |
|---|
| 99 | if (logger.isLoggable(Level.FINE)) |
|---|
| 100 | { |
|---|
| 101 | logger.entering(CLASSNAME, "getItems"); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | final Map itemMap = new HashMap(); |
|---|
| 105 | |
|---|
| 106 | final List files = mReportDocument.getFileOrErrorOrException(); |
|---|
| 107 | |
|---|
| 108 | for (final Iterator iterator = files.iterator(); iterator.hasNext(); ) |
|---|
| 109 | { |
|---|
| 110 | final Object o = iterator.next(); |
|---|
| 111 | |
|---|
| 112 | if (o instanceof org.jcoderz.phoenix.checkstyle.jaxb.File) |
|---|
| 113 | { |
|---|
| 114 | final org.jcoderz.phoenix.checkstyle.jaxb.File f |
|---|
| 115 | = (org.jcoderz.phoenix.checkstyle.jaxb.File) o; |
|---|
| 116 | final String resourceFilename = normalizeFileName(f.getName()); |
|---|
| 117 | |
|---|
| 118 | final List errors = f.getErrorOrException(); |
|---|
| 119 | final List items = createReportItems(resourceFilename, errors); |
|---|
| 120 | |
|---|
| 121 | addItemsToResource(itemMap, resourceFilename, items); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (logger.isLoggable(Level.FINE)) |
|---|
| 126 | { |
|---|
| 127 | logger.exiting(CLASSNAME, "getItems", itemMap); |
|---|
| 128 | } |
|---|
| 129 | return itemMap; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | private void addItemsToResource ( |
|---|
| 133 | Map itemMap, String resourceFilename, List items) |
|---|
| 134 | { |
|---|
| 135 | final ResourceInfo info = ResourceInfo.lookup(resourceFilename); |
|---|
| 136 | if (info != null) |
|---|
| 137 | { |
|---|
| 138 | if (itemMap.get(info) != null) |
|---|
| 139 | { |
|---|
| 140 | final List l = (List) itemMap.get(info); |
|---|
| 141 | l.addAll(items); |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | itemMap.put(info, items); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | logger.finer("Ignore findings for resource " + resourceFilename); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | private List createReportItems (String resourceFilename, List errors) |
|---|
| 155 | throws JAXBException |
|---|
| 156 | { |
|---|
| 157 | final List ret = new ArrayList(); |
|---|
| 158 | |
|---|
| 159 | for (final Iterator iterator = errors.iterator(); iterator.hasNext(); ) |
|---|
| 160 | { |
|---|
| 161 | final Object o = iterator.next(); |
|---|
| 162 | |
|---|
| 163 | if (o instanceof org.jcoderz.phoenix.checkstyle.jaxb.Error) |
|---|
| 164 | { |
|---|
| 165 | final org.jcoderz.phoenix.checkstyle.jaxb.Error error |
|---|
| 166 | = (org.jcoderz.phoenix.checkstyle.jaxb.Error) o; |
|---|
| 167 | |
|---|
| 168 | final Item item = new ObjectFactory().createItem(); |
|---|
| 169 | |
|---|
| 170 | if (error.isSetSeverity()) |
|---|
| 171 | { |
|---|
| 172 | item.setSeverity(error.getSeverity()); |
|---|
| 173 | } |
|---|
| 174 | item.setMessage(error.getMessage()); |
|---|
| 175 | item.setLine(error.getLine()); |
|---|
| 176 | item.setColumn(error.getColumn()); |
|---|
| 177 | item.setOrigin(Origin.CHECKSTYLE); |
|---|
| 178 | |
|---|
| 179 | final FindingType type = CheckstyleFindingType. |
|---|
| 180 | detectFindingTypeForMessage(error.getMessage()); |
|---|
| 181 | if (type == null) |
|---|
| 182 | { |
|---|
| 183 | item.setFindingType(sourceToClass(error.getSource())); |
|---|
| 184 | logger.log(Level.INFO, "Could not find finding type for " |
|---|
| 185 | + "Checkstyle finding with message '" + error.getMessage() |
|---|
| 186 | + "'."); |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| 190 | item.setFindingType(type.getSymbol()); |
|---|
| 191 | item.setSeverity(((CheckstyleFindingType) type).getSeverity()); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | ret.add(item); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | return ret; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | private static String sourceToClass (String source) |
|---|
| 202 | { |
|---|
| 203 | final int i = source.lastIndexOf('.'); |
|---|
| 204 | return source.substring(i + 1); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | } |
|---|