| 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.cpd.jaxb.Duplication; |
|---|
| 48 | import org.jcoderz.phoenix.cpd.jaxb.PmdCpd; |
|---|
| 49 | import org.jcoderz.phoenix.report.jaxb.Item; |
|---|
| 50 | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * CPD Report Reader. |
|---|
| 54 | * |
|---|
| 55 | * @author Michael Griffel |
|---|
| 56 | */ |
|---|
| 57 | public final class CpdReportReader |
|---|
| 58 | extends AbstractReportReader |
|---|
| 59 | { |
|---|
| 60 | /** JAXB context path. */ |
|---|
| 61 | public static final String CPD_JAXB_CONTEXT_PATH |
|---|
| 62 | = "org.jcoderz.phoenix.cpd.jaxb"; |
|---|
| 63 | |
|---|
| 64 | private static final String CLASSNAME = CpdReportReader.class.getName(); |
|---|
| 65 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 66 | |
|---|
| 67 | private PmdCpd mReportDocument; |
|---|
| 68 | |
|---|
| 69 | public CpdReportReader () |
|---|
| 70 | throws JAXBException |
|---|
| 71 | { |
|---|
| 72 | super(CPD_JAXB_CONTEXT_PATH); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** {@inheritDoc} */ |
|---|
| 76 | public void parse (File f) throws JAXBException, FileNotFoundException |
|---|
| 77 | { |
|---|
| 78 | mReportDocument = (PmdCpd) getUnmarshaller().unmarshal( |
|---|
| 79 | new FileInputStream(f)); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** {@inheritDoc} */ |
|---|
| 83 | protected Map getItems () throws JAXBException |
|---|
| 84 | { |
|---|
| 85 | final Map result = new HashMap(); |
|---|
| 86 | |
|---|
| 87 | for (final Iterator iterator |
|---|
| 88 | = mReportDocument.getDuplication().iterator(); |
|---|
| 89 | iterator.hasNext();) |
|---|
| 90 | { |
|---|
| 91 | final Duplication duplication = (Duplication) iterator.next(); |
|---|
| 92 | final List filez = duplication.getFile(); |
|---|
| 93 | for (int i = 0; i < filez.size(); i++) |
|---|
| 94 | { |
|---|
| 95 | final org.jcoderz.phoenix.cpd.jaxb.File file |
|---|
| 96 | = (org.jcoderz.phoenix.cpd.jaxb.File) filez.get(i); |
|---|
| 97 | |
|---|
| 98 | final String key = normalizeFileName(file.getPath()); |
|---|
| 99 | final ResourceInfo info = ResourceInfo.lookup(key); |
|---|
| 100 | if (info != null) |
|---|
| 101 | { |
|---|
| 102 | final Item item = new ObjectFactory().createItem(); |
|---|
| 103 | item.setMessage(constructMessage(i, filez, duplication)); |
|---|
| 104 | item.setOrigin(Origin.CPD); |
|---|
| 105 | item.setSeverity(Severity.CPD); |
|---|
| 106 | item.setFindingType(CpdFindingType.NAME); |
|---|
| 107 | item.setLine(file.getLine()); |
|---|
| 108 | item.setEndLine(file.getLine() + duplication.getLines()); |
|---|
| 109 | |
|---|
| 110 | if (result.get(info) == null) |
|---|
| 111 | { |
|---|
| 112 | final List list = new ArrayList(); |
|---|
| 113 | list.add(item); |
|---|
| 114 | result.put(info, list); |
|---|
| 115 | } |
|---|
| 116 | else |
|---|
| 117 | { |
|---|
| 118 | final List list = (List) result.get(info); |
|---|
| 119 | list.add(item); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | logger.finer("Ignoring findings for resource " + key); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | return result; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * @param file |
|---|
| 133 | * @param filez |
|---|
| 134 | * @return |
|---|
| 135 | */ |
|---|
| 136 | private String constructMessage (int currentIndex, List filez, |
|---|
| 137 | Duplication duplication) |
|---|
| 138 | { |
|---|
| 139 | final StringBuilder sb = new StringBuilder(); |
|---|
| 140 | sb.append("Copied and pasted code. "); |
|---|
| 141 | sb.append(duplication.getTokens()); |
|---|
| 142 | sb.append(" equal tokens ("); |
|---|
| 143 | sb.append(duplication.getLines()); |
|---|
| 144 | sb.append(" lines) found in "); |
|---|
| 145 | sb.append(filez.size()).append(" locations. See also: "); |
|---|
| 146 | |
|---|
| 147 | for (int i = 0; i < filez.size(); i++) |
|---|
| 148 | { |
|---|
| 149 | if (i == currentIndex) |
|---|
| 150 | { |
|---|
| 151 | continue; // skip current finding |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | final org.jcoderz.phoenix.cpd.jaxb.File file |
|---|
| 155 | = (org.jcoderz.phoenix.cpd.jaxb.File) filez.get(i); |
|---|
| 156 | |
|---|
| 157 | final ResourceInfo info = ResourceInfo.lookup(file.getPath()); |
|---|
| 158 | final String resourceName; |
|---|
| 159 | if (info != null) |
|---|
| 160 | { |
|---|
| 161 | resourceName = info.getPackage() + "." + info.getClassname(); |
|---|
| 162 | } |
|---|
| 163 | else |
|---|
| 164 | { |
|---|
| 165 | resourceName = file.getPath(); |
|---|
| 166 | } |
|---|
| 167 | sb.append(resourceName + ":" + file.getLine() + " "); |
|---|
| 168 | } |
|---|
| 169 | return sb.toString(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | } |
|---|