| 1 | /* |
|---|
| 2 | * $Id: CheckstyleFindingType.java 1173 2008-09-22 10:04:44Z amandel $ |
|---|
| 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.Serializable; |
|---|
| 36 | import java.util.Comparator; |
|---|
| 37 | import java.util.regex.Matcher; |
|---|
| 38 | import java.util.regex.Pattern; |
|---|
| 39 | |
|---|
| 40 | import javax.xml.bind.JAXBException; |
|---|
| 41 | |
|---|
| 42 | import org.jcoderz.phoenix.report.GenericReportReader.SourceFile; |
|---|
| 43 | import org.jcoderz.phoenix.report.ftf.jaxb.FindingDescription; |
|---|
| 44 | import org.jcoderz.phoenix.report.jaxb.Item; |
|---|
| 45 | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Enumeration type for generic findings. |
|---|
| 49 | * The description and patterns are read from a xml file. |
|---|
| 50 | * |
|---|
| 51 | * @author Andreas Mandel |
|---|
| 52 | */ |
|---|
| 53 | public final class GenericFindingType |
|---|
| 54 | extends FindingType |
|---|
| 55 | { |
|---|
| 56 | private final Pattern mPattern; |
|---|
| 57 | private final int mPriority; |
|---|
| 58 | private final FindingDescription mFindingDescription; |
|---|
| 59 | private final int mTextPos; |
|---|
| 60 | private final int mLineStart; |
|---|
| 61 | private final int mLineEnd; |
|---|
| 62 | private final int mColumnStart; |
|---|
| 63 | private final int mColumnEnd; |
|---|
| 64 | private final int mSourceText; |
|---|
| 65 | private final boolean mSourceColumnByCaret; |
|---|
| 66 | private final Severity mSeverity; |
|---|
| 67 | private final boolean mIsGlobal; |
|---|
| 68 | |
|---|
| 69 | private final ObjectFactory mOf = new ObjectFactory(); |
|---|
| 70 | |
|---|
| 71 | // private final Severity mSeverity; |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Create new finding type based on xml description. |
|---|
| 75 | * @param root the definition of the root finding description. |
|---|
| 76 | * @param fd the definition of the detailed finding description. |
|---|
| 77 | */ |
|---|
| 78 | public GenericFindingType ( |
|---|
| 79 | FindingDescription root, FindingDescription fd) |
|---|
| 80 | { |
|---|
| 81 | super(fd.getSymbol(), fd.getShortDescription(), |
|---|
| 82 | fd.getDescription()); |
|---|
| 83 | |
|---|
| 84 | mPriority = fd.getPriority(); |
|---|
| 85 | mPattern = Pattern.compile( |
|---|
| 86 | fd.getPattern(), Pattern.MULTILINE + Pattern.UNIX_LINES); |
|---|
| 87 | mFindingDescription = fd; |
|---|
| 88 | mTextPos = fd.isSetTextPos() ? Integer.parseInt(fd.getTextPos()) : -1; |
|---|
| 89 | mLineStart = fd.isSetLineStartPos() |
|---|
| 90 | ? Integer.parseInt(fd.getLineStartPos()) : -1; |
|---|
| 91 | mLineEnd = fd.isSetLineEndPos() |
|---|
| 92 | ? Integer.parseInt(fd.getLineEndPos()) : -1; |
|---|
| 93 | if (fd.isSetColumnStartPos() && "caret".equals(fd.getColumnStartPos())) |
|---|
| 94 | { |
|---|
| 95 | mColumnStart = -1; |
|---|
| 96 | mSourceColumnByCaret = true; |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | mColumnStart = fd.isSetColumnStartPos() |
|---|
| 101 | ? Integer.parseInt(fd.getColumnStartPos()) : -1; |
|---|
| 102 | mSourceColumnByCaret = false; |
|---|
| 103 | } |
|---|
| 104 | mColumnEnd = fd.isSetColumnEndPos() |
|---|
| 105 | ? Integer.parseInt(fd.getColumnEndPos()) : -1; |
|---|
| 106 | mSourceText = fd.isSetSourceTextPos() |
|---|
| 107 | ? Integer.parseInt(fd.getSourceTextPos()) : -1; |
|---|
| 108 | mSeverity = fd.isSetSeverity() ? fd.getSeverity() : null; |
|---|
| 109 | mIsGlobal = fd.isGlobal(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Try to match the given method and fill the item |
|---|
| 115 | * accordingly if a match is found. |
|---|
| 116 | * @param sf the input source read. Allows to set the new file position. |
|---|
| 117 | * @param message the message to parse. |
|---|
| 118 | * @return a new Item with available data filled or null. |
|---|
| 119 | * @throws JAXBException if Item creation fails on jaxb level. |
|---|
| 120 | */ |
|---|
| 121 | public Item createItem (SourceFile sf, String message) throws JAXBException |
|---|
| 122 | { |
|---|
| 123 | Item result = null; |
|---|
| 124 | final Matcher match = mPattern.matcher(message); |
|---|
| 125 | if (match.lookingAt()) |
|---|
| 126 | { |
|---|
| 127 | sf.setPos(sf.getPos() + match.end() + 1); |
|---|
| 128 | result = mOf.createItem(); |
|---|
| 129 | result.setFindingType(getSymbol()); |
|---|
| 130 | if (mTextPos != -1) |
|---|
| 131 | { |
|---|
| 132 | result.setMessage(match.group(mTextPos)); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | result.setMessage(match.group()); |
|---|
| 137 | } |
|---|
| 138 | if (mLineStart != -1) |
|---|
| 139 | { |
|---|
| 140 | result.setLine(Integer.parseInt(match.group(mLineStart))); |
|---|
| 141 | } |
|---|
| 142 | if (mLineEnd != -1) |
|---|
| 143 | { |
|---|
| 144 | result.setEndLine(Integer.parseInt(match.group(mLineEnd))); |
|---|
| 145 | } |
|---|
| 146 | if (mColumnStart != -1) |
|---|
| 147 | { |
|---|
| 148 | result.setColumn(Integer.parseInt(match.group(mColumnStart))); |
|---|
| 149 | } |
|---|
| 150 | if (mColumnEnd != -1) |
|---|
| 151 | { |
|---|
| 152 | result.setEndColumn(Integer.parseInt(match.group(mColumnEnd))); |
|---|
| 153 | } |
|---|
| 154 | if (mSourceText != -1) |
|---|
| 155 | { |
|---|
| 156 | result.setSourceText(match.group(mSourceText)); |
|---|
| 157 | } |
|---|
| 158 | if (mSeverity != null) |
|---|
| 159 | { |
|---|
| 160 | result.setSeverity(mSeverity); |
|---|
| 161 | } |
|---|
| 162 | if (mFindingDescription.isGlobal()) |
|---|
| 163 | { |
|---|
| 164 | result.setGlobal(true); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | return result; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** @return the severity assigned to findings of this type by default. */ |
|---|
| 171 | public Severity getSeverity () |
|---|
| 172 | { |
|---|
| 173 | return mSeverity; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * The priority used to match for this finding in relation to |
|---|
| 178 | * other findings of this type. |
|---|
| 179 | * The higher the value the higher is the priority of this pattern. |
|---|
| 180 | * A catch all pattern like "(.*)" should therefore get a low |
|---|
| 181 | * number (eg. {@link Integer#MIN_VALUE}) as priority. |
|---|
| 182 | * Default priority is 0. |
|---|
| 183 | */ |
|---|
| 184 | private int getPriority () |
|---|
| 185 | { |
|---|
| 186 | return mPriority; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * @return the sourceColumnByCaret |
|---|
| 191 | */ |
|---|
| 192 | public boolean isSourceColumnByCaret () |
|---|
| 193 | { |
|---|
| 194 | return mSourceColumnByCaret; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * @return the isGlobal |
|---|
| 200 | */ |
|---|
| 201 | public boolean isGlobal () |
|---|
| 202 | { |
|---|
| 203 | return mIsGlobal; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Init of the enum. |
|---|
| 209 | */ |
|---|
| 210 | public static void initialize () |
|---|
| 211 | { |
|---|
| 212 | // already done |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * Class to sort {@link GenericFindingType}s by their priority. |
|---|
| 217 | */ |
|---|
| 218 | public static class OrderByPriority |
|---|
| 219 | implements Comparator<GenericFindingType>, Serializable |
|---|
| 220 | { |
|---|
| 221 | private static final long serialVersionUID = 1L; |
|---|
| 222 | |
|---|
| 223 | /** {@inheritDoc} */ |
|---|
| 224 | public int compare (GenericFindingType o1, GenericFindingType o2) |
|---|
| 225 | { |
|---|
| 226 | final int result; |
|---|
| 227 | if (o1.getPriority() > o2.getPriority()) |
|---|
| 228 | { |
|---|
| 229 | result = -1; |
|---|
| 230 | } |
|---|
| 231 | else if (o1.getPriority() < o2.getPriority()) |
|---|
| 232 | { |
|---|
| 233 | result = 1; |
|---|
| 234 | } |
|---|
| 235 | else |
|---|
| 236 | { |
|---|
| 237 | result = o1.getSymbol().compareTo(o2.getSymbol()); |
|---|
| 238 | } |
|---|
| 239 | return result; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | } |
|---|