| 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.Logger; |
|---|
| 44 | |
|---|
| 45 | import javax.xml.bind.JAXBException; |
|---|
| 46 | |
|---|
| 47 | import org.jcoderz.phoenix.pmd.jaxb.FileType; |
|---|
| 48 | import org.jcoderz.phoenix.pmd.jaxb.Pmd; |
|---|
| 49 | import org.jcoderz.phoenix.pmd.jaxb.Violation; |
|---|
| 50 | import org.jcoderz.phoenix.report.jaxb.Item; |
|---|
| 51 | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * PMD Report Reader. |
|---|
| 55 | * |
|---|
| 56 | * @author Michael Griffel |
|---|
| 57 | */ |
|---|
| 58 | public final class PmdReportReader |
|---|
| 59 | extends AbstractReportReader |
|---|
| 60 | { |
|---|
| 61 | /** JAXB context path. */ |
|---|
| 62 | public static final String PMD_JAXB_CONTEXT_PATH |
|---|
| 63 | = "org.jcoderz.phoenix.pmd.jaxb"; |
|---|
| 64 | |
|---|
| 65 | private static final String CLASSNAME = PmdReportReader.class.getName(); |
|---|
| 66 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 67 | |
|---|
| 68 | private static final int PRIORITY_HIGH = 1; |
|---|
| 69 | private static final int PRIORITY_MEDIUM_HIGH = 2; |
|---|
| 70 | private static final int PRIORITY_MEDIUM = 3; |
|---|
| 71 | private static final int PRIORITY_MEDIUM_LOW = 4; |
|---|
| 72 | private static final int PRIORITY_LOW = 5; |
|---|
| 73 | |
|---|
| 74 | private Pmd mReportDocument; |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Constructor. |
|---|
| 78 | * |
|---|
| 79 | * @throws JAXBException |
|---|
| 80 | */ |
|---|
| 81 | public PmdReportReader () |
|---|
| 82 | throws JAXBException |
|---|
| 83 | { |
|---|
| 84 | super(PMD_JAXB_CONTEXT_PATH); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** {@inheritDoc} */ |
|---|
| 88 | public void parse (File f) |
|---|
| 89 | throws JAXBException, FileNotFoundException |
|---|
| 90 | { |
|---|
| 91 | logger.entering(CLASSNAME, "parse", f); |
|---|
| 92 | mReportDocument = (Pmd) getUnmarshaller().unmarshal( |
|---|
| 93 | new FileInputStream(f)); |
|---|
| 94 | logger.exiting(CLASSNAME, "parse"); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** {@inheritDoc} */ |
|---|
| 98 | protected Map<ResourceInfo, List<Item>> getItems () |
|---|
| 99 | throws JAXBException |
|---|
| 100 | { |
|---|
| 101 | logger.entering(CLASSNAME, "getItems()"); |
|---|
| 102 | final Map<ResourceInfo, List<Item>> result |
|---|
| 103 | = new HashMap<ResourceInfo, List<Item>>(); |
|---|
| 104 | |
|---|
| 105 | for (final Iterator<FileType> iterator = mReportDocument.getFile().iterator(); |
|---|
| 106 | iterator.hasNext();) |
|---|
| 107 | { |
|---|
| 108 | final FileType file = iterator.next(); |
|---|
| 109 | |
|---|
| 110 | final String key = normalizeFileName(file.getName()); |
|---|
| 111 | final List<Item> items = createItemMap(file); |
|---|
| 112 | final ResourceInfo info = ResourceInfo.lookup(key); |
|---|
| 113 | if (info != null) |
|---|
| 114 | { |
|---|
| 115 | result.put(info, items); |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | logger.finer("Ingoring findings for resource " + key); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | logger.exiting(CLASSNAME, "getItems()", result); |
|---|
| 123 | return result; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | private List<Item> createItemMap (org.jcoderz.phoenix.pmd.jaxb.FileType file) |
|---|
| 127 | throws JAXBException |
|---|
| 128 | { |
|---|
| 129 | final List<Item> items = new ArrayList<Item>(); |
|---|
| 130 | for (final Iterator<Violation> iterator = file.getViolation().iterator(); iterator |
|---|
| 131 | .hasNext();) |
|---|
| 132 | { |
|---|
| 133 | final Violation violation = iterator.next(); |
|---|
| 134 | |
|---|
| 135 | final Item item = new ObjectFactory().createItem(); |
|---|
| 136 | item.setMessage(violation.getValue().trim()); |
|---|
| 137 | item.setOrigin(Origin.PMD); |
|---|
| 138 | item.setSeverity(mapPriority(violation)); |
|---|
| 139 | item.setFindingType(violation.getRule()); |
|---|
| 140 | item.setLine(violation.getBeginline()); |
|---|
| 141 | item.setEndLine(violation.getEndline()); |
|---|
| 142 | item.setColumn(violation.getBegincolumn()); |
|---|
| 143 | item.setEndColumn(violation.getEndcolumn()); |
|---|
| 144 | items.add(item); |
|---|
| 145 | } |
|---|
| 146 | return items; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | private Severity mapPriority (Violation violation) |
|---|
| 150 | { |
|---|
| 151 | final Severity ret; |
|---|
| 152 | |
|---|
| 153 | switch (violation.getPriority()) |
|---|
| 154 | { |
|---|
| 155 | case PRIORITY_HIGH: |
|---|
| 156 | ret = Severity.ERROR; |
|---|
| 157 | break; |
|---|
| 158 | case PRIORITY_MEDIUM_HIGH: |
|---|
| 159 | ret = Severity.WARNING; |
|---|
| 160 | break; |
|---|
| 161 | case PRIORITY_MEDIUM: |
|---|
| 162 | ret = Severity.DESIGN; |
|---|
| 163 | break; |
|---|
| 164 | case PRIORITY_MEDIUM_LOW: |
|---|
| 165 | ret = Severity.CODE_STYLE; |
|---|
| 166 | break; |
|---|
| 167 | case PRIORITY_LOW: |
|---|
| 168 | ret = Severity.INFO; |
|---|
| 169 | break; |
|---|
| 170 | default: |
|---|
| 171 | ret = Severity.WARNING; |
|---|
| 172 | } |
|---|
| 173 | return ret; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|