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