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