| 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.BufferedReader; |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.FileNotFoundException; |
| 39 | | | import java.io.FileReader; |
| 40 | | | import java.io.IOException; |
| 41 | | | import java.io.InputStream; |
| 42 | | | import java.io.Reader; |
| 43 | | | import java.util.ArrayList; |
| 44 | | | import java.util.Collections; |
| 45 | | | import java.util.HashMap; |
| 46 | | | import java.util.Iterator; |
| 47 | | | import java.util.List; |
| 48 | | | import java.util.Map; |
| 49 | | | import java.util.logging.Level; |
| 50 | | | import java.util.logging.Logger; |
| 51 | | | import java.util.regex.Matcher; |
| 52 | | | import java.util.regex.Pattern; |
| 53 | | | |
| 54 | | | import javax.xml.bind.JAXBException; |
| 55 | | | |
| 56 | | | import org.jcoderz.commons.util.Assert; |
| 57 | | | import org.jcoderz.commons.util.Constants; |
| 58 | | | import org.jcoderz.commons.util.IoUtil; |
| 59 | | | import org.jcoderz.commons.util.JaxbUtil; |
| 60 | | | import org.jcoderz.commons.util.ObjectUtil; |
| 61 | | | import org.jcoderz.commons.util.StringUtil; |
| 62 | | | import org.jcoderz.commons.util.JaxbUtil.UnmarshalResult; |
| 63 | | | import org.jcoderz.phoenix.report.ftf.jaxb.FindingDescription; |
| 64 | | | import org.jcoderz.phoenix.report.ftf.jaxb.FindingTypeFormat; |
| 65 | | | import org.jcoderz.phoenix.report.jaxb.Item; |
| 66 | | | import org.xml.sax.InputSource; |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | <i></i> |
| 78 | | | <code></code> |
| 79 | | | <code></code> |
| 80 | | | |
| 81 | | | |
| 82 | | | |
| 83 | | | @author |
| 84 | | | |
| 85 | | | |
| 86 | | | public final class GenericReportReader implements ReportReader |
| 87 | | | { |
| 88 | | | private static final int MAX_DEBUG_TEXT_CHARS = 100; |
| 89 | 0 | | private static final String CLASSNAME |
| 90 | | | = GenericReportReader.class.getName(); |
| 91 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 92 | | | |
| 93 | 0 | | private static final Pattern CODE_LINE_PATTERN |
| 94 | | | = Pattern.compile("^.*$", Pattern.MULTILINE); |
| 95 | | | |
| 96 | 0 | | private static final Pattern CARET_LINE_PATTERN |
| 97 | | | = Pattern.compile("^\\s*\\^$", Pattern.MULTILINE); |
| 98 | | | |
| 99 | 0 | | private static final Map<Origin, GenericReportReader> GENERIC_REPORT_TYPES |
| 100 | | | = new HashMap<Origin, GenericReportReader>(); |
| 101 | | | |
| 102 | | | |
| 103 | 0 | | private final List<GenericFindingType> mFindingTypes |
| 104 | | | = new ArrayList<GenericFindingType>(); |
| 105 | | | |
| 106 | | | private Map<ResourceInfo, List<Item>> mItems; |
| 107 | | | |
| 108 | | | private SourceFile mSourceFile; |
| 109 | | | |
| 110 | | | private final Pattern mMessagePattern; |
| 111 | | | private final FindingTypeFormat mFindingTypeFormatDescription; |
| 112 | | | |
| 113 | | | private final int mTextPos; |
| 114 | | | private final Origin mOrigin; |
| 115 | | | private final int mFilePos; |
| 116 | | | private final int mLineStart; |
| 117 | | | private final Severity mDefaultSeverity; |
| 118 | | | |
| 119 | 0 | | private Matcher mRootMatcher = null; |
| 120 | | | |
| 121 | | | private GenericReportReader (Origin type) |
| 122 | | | throws JAXBException |
| 123 | 0 | | { |
| 124 | 0 | | mOrigin = type; |
| 125 | 0 | | mFindingTypeFormatDescription = loadFormatDescription(type); |
| 126 | 0 | | initializeFindingTypes(); |
| 127 | 0 | | final FindingDescription root |
| 128 | | | = mFindingTypeFormatDescription.getRootType(); |
| 129 | 0 | | mMessagePattern |
| 130 | | | = Pattern.compile(root.getPattern(), |
| 131 | | | Pattern.MULTILINE); |
| 132 | 0 | | mTextPos = Integer.parseInt(root.getTextPos()); |
| 133 | 0 | | mFilePos = Integer.parseInt(root.getFilenamePos()); |
| 134 | 0 | | mLineStart = root.isSetLineStartPos() |
| 135 | | | ? Integer.parseInt(root.getLineStartPos()) : -1; |
| 136 | 0 | | mDefaultSeverity = root.isSetSeverity() |
| 137 | | | ? root.getSeverity() : Severity.CODE_STYLE; |
| 138 | 0 | | } |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | <code></code> |
| 143 | | (1) | |
| 144 | | | @param |
| 145 | | | @return |
| 146 | | | |
| 147 | | | public static GenericReportReader initialize (Origin findingType) |
| 148 | | | { |
| 149 | 0 | | GenericReportReader result = null; |
| 150 | 0 | | synchronized (GENERIC_REPORT_TYPES) |
| 151 | | | { |
| 152 | 0 | | if (!GENERIC_REPORT_TYPES.containsKey(findingType)) |
| 153 | | | { |
| 154 | | | try |
| 155 | | | { |
| 156 | 0 | | result = new GenericReportReader(findingType); |
| 157 | | | } |
| 158 | 0 | | catch (Exception ex) |
| 159 | | | { |
| 160 | | (2) | |
| 161 | 0 | | logger.log(Level.WARNING, |
| 162 | | | "Could not load finding type for '" + findingType |
| 163 | | | + "' failed with " + ex.getMessage() + ".", ex); |
| 164 | 0 | | } |
| 165 | 0 | (3) | GENERIC_REPORT_TYPES.put(findingType, result); |
| 166 | | | } |
| 167 | 0 | | result = GENERIC_REPORT_TYPES.get(findingType); |
| 168 | 0 | | } |
| 169 | 0 | | return result; |
| 170 | | | } |
| 171 | | | |
| 172 | | | private static FindingTypeFormat loadFormatDescription (Origin type) |
| 173 | | | throws JAXBException |
| 174 | | | { |
| 175 | 0 | | FindingTypeFormat findingTypeFormatDescription = null; |
| 176 | 0 | | InputStream in = null; |
| 177 | | | try |
| 178 | | | { |
| 179 | 0 | | final String filename |
| 180 | | | = type.toString().toLowerCase(Constants.SYSTEM_LOCALE) |
| 181 | | | + ".xml"; |
| 182 | 0 | | in = GenericReportReader.class.getResourceAsStream( |
| 183 | | | "ftf/" + filename); |
| 184 | 0 | | if (in == null) |
| 185 | | | { |
| 186 | 0 | | in = GenericReportReader.class.getResourceAsStream( |
| 187 | | | "/ftf/" + filename); |
| 188 | | | } |
| 189 | 0 | | if (in == null) |
| 190 | | | { |
| 191 | | | try |
| 192 | | | { |
| 193 | 0 | | in = new FileInputStream(filename); |
| 194 | | | } |
| 195 | 0 | | catch (FileNotFoundException ex) |
| 196 | | | { |
| 197 | | | |
| 198 | 0 | | } |
| 199 | | | } |
| 200 | 0 | | Assert.notNull(in, "report type description " + type); |
| 201 | 0 | | final UnmarshalResult unmarshal |
| 202 | | | = JaxbUtil.unmarshal(new InputSource(in), |
| 203 | | | "org.jcoderz.phoenix.report.ftf.jaxb"); |
| 204 | | | |
| 205 | 0 | | findingTypeFormatDescription |
| 206 | | | = (FindingTypeFormat) unmarshal.getParsedData(); |
| 207 | | | } |
| 208 | | | finally |
| 209 | | | { |
| 210 | 0 | | IoUtil.close(in); |
| 211 | 0 | | } |
| 212 | 0 | | return findingTypeFormatDescription; |
| 213 | | | } |
| 214 | | | |
| 215 | | | {@inheritDoc} |
| 216 | | | public void parse (File f) |
| 217 | | | throws JAXBException |
| 218 | | | { |
| 219 | | | try |
| 220 | | | { |
| 221 | 0 | | mSourceFile = new SourceFile(f); |
| 222 | 0 | | mRootMatcher = mMessagePattern.matcher(mSourceFile.getContent()); |
| 223 | | | } |
| 224 | 0 | | catch (IOException ex) |
| 225 | | | { |
| 226 | 0 | | throw new JAXBException("Failed to read '" + f + "'.", ex); |
| 227 | 0 | | } |
| 228 | 0 | | } |
| 229 | | | |
| 230 | | | {@inheritDoc} |
| 231 | | | public void merge (Map<ResourceInfo, List<Item>> items) |
| 232 | | | throws JAXBException |
| 233 | | | { |
| 234 | 0 | | mItems = items; |
| 235 | 0 | | while (!mSourceFile.readFully()) |
| 236 | | | { |
| 237 | 0 | | parseNext(); |
| 238 | | | } |
| 239 | 0 | | } |
| 240 | | | |
| 241 | | | |
| 242 | | | |
| 243 | | | @param |
| 244 | | | @return |
| 245 | | | |
| 246 | | | @throws |
| 247 | | | |
| 248 | | | public Item detectFindingTypeForMessage (String message) |
| 249 | | | throws JAXBException |
| 250 | | | { |
| 251 | 0 | | Item result = null; |
| 252 | 0 | | for (final GenericFindingType type : mFindingTypes) |
| 253 | | | { |
| 254 | 0 | | result = type.createItem(mSourceFile, message); |
| 255 | 0 | | if (result != null) |
| 256 | | | { |
| 257 | 0 | | if (type.isSourceColumnByCaret()) |
| 258 | | | { |
| 259 | 0 | | addPositionByCaret(result); |
| 260 | | | } |
| 261 | | | break; |
| 262 | | | } |
| 263 | | | } |
| 264 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 265 | | | { |
| 266 | 0 | | logger.fine("For text: '" |
| 267 | | | + StringUtil.trimLength(message, MAX_DEBUG_TEXT_CHARS) |
| 268 | | | + "' matched finding: " |
| 269 | | | + (result == null ? "null" : result.getFindingType() |
| 270 | | | + "'. End at " + mSourceFile.getPos())); |
| 271 | | | } |
| 272 | 0 | | return result; |
| 273 | | | } |
| 274 | | | |
| 275 | | | private void addPositionByCaret (final Item i) |
| 276 | | | { |
| 277 | 0 | | final String text |
| 278 | | | = mSourceFile.getContent().substring(mSourceFile.getPos()); |
| 279 | 0 | | final Matcher codeMat |
| 280 | | | = CODE_LINE_PATTERN.matcher(text); |
| 281 | 0 | | if (codeMat.lookingAt()) |
| 282 | | | { |
| 283 | 0 | | final String textAfterCode |
| 284 | | | = mSourceFile.getContent().substring( |
| 285 | | | mSourceFile.getPos() + codeMat.end() + 1); |
| 286 | 0 | | final Matcher caretMat |
| 287 | | | = CARET_LINE_PATTERN.matcher(textAfterCode); |
| 288 | 0 | | if (caretMat.lookingAt()) |
| 289 | | | { |
| 290 | 0 | | i.setColumn(caretMat.end()); |
| 291 | 0 | | mSourceFile.setPos( |
| 292 | | | mSourceFile.getPos() |
| 293 | | | + codeMat.end() + 1 |
| 294 | | | + caretMat.end() + 1); |
| 295 | | | } |
| 296 | | | else |
| 297 | | | { |
| 298 | 0 | | logger.fine("Caret defined but not found for '" |
| 299 | | | + i.getFindingType() |
| 300 | | | + "' Code Line: '" + codeMat + "' caretLine: '" |
| 301 | | | + caretMat + "'. text: '" |
| 302 | | | + StringUtil.trimLength( |
| 303 | | | textAfterCode, MAX_DEBUG_TEXT_CHARS) + "'."); |
| 304 | | | } |
| 305 | 0 | | } |
| 306 | | | else |
| 307 | | | { |
| 308 | 0 | | logger.fine("Caret defined but not found for '" |
| 309 | | | + i.getFindingType() |
| 310 | | | + "' Code Line: '" + codeMat + "'. text: '" |
| 311 | | | + StringUtil.trimLength( |
| 312 | | | text, MAX_DEBUG_TEXT_CHARS) + "'."); |
| 313 | | | } |
| 314 | 0 | | } |
| 315 | | | |
| 316 | | (4) | private void parseNext () |
| 317 | | | throws JAXBException |
| 318 | | | { |
| 319 | 0 | | if (mRootMatcher.find()) |
| 320 | | | { |
| 321 | 0 | | final String text = mRootMatcher.group(mTextPos); |
| 322 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 323 | | | { |
| 324 | 0 | | logger.fine("Main pattern matched for: '" |
| 325 | | | + StringUtil.trimLength(text, MAX_DEBUG_TEXT_CHARS) |
| 326 | | | + "'. End at " + mRootMatcher.end()); |
| 327 | | | } |
| 328 | 0 | | mSourceFile.setPos(mRootMatcher.start(mTextPos)); |
| 329 | 0 | | final Item item = detectFindingTypeForMessage(text); |
| 330 | 0 | | if (item == null) |
| 331 | | | { |
| 332 | 0 | | final int pos |
| 333 | | | = mSourceFile.getContent().indexOf( |
| 334 | | | '\n', mSourceFile.getPos()); |
| 335 | 0 | | if (pos != -1) |
| 336 | | | { |
| 337 | 0 | | mSourceFile.setPos(pos + 1); |
| 338 | | | } |
| 339 | | | else |
| 340 | | | { |
| 341 | 0 | | mSourceFile.setPos(mSourceFile.getContent().length()); |
| 342 | | | } |
| 343 | 0 | | } |
| 344 | | | else |
| 345 | | | { |
| 346 | 0 | | item.setOrigin(mOrigin); |
| 347 | 0 | | if (!item.isSetSeverity()) |
| 348 | | | { |
| 349 | 0 | | item.setSeverity(mDefaultSeverity); |
| 350 | | | } |
| 351 | 0 | | if (!item.isSetLine() && mLineStart != -1 |
| 352 | | | && mRootMatcher.group(mLineStart) != null) |
| 353 | | | { |
| 354 | 0 | | item.setLine( |
| 355 | | | Integer.parseInt(mRootMatcher.group(mLineStart))); |
| 356 | | | } |
| 357 | 0 | | if (!item.isSetFindingType()) |
| 358 | | | { |
| 359 | 0 | | item.setFindingType(mOrigin.toString()); |
| 360 | | | } |
| 361 | 0 | | if (!item.isSetMessage()) |
| 362 | | | { |
| 363 | 0 | | item.setMessage(mRootMatcher.group(mTextPos)); |
| 364 | | | } |
| 365 | 0 | | if (mFindingTypeFormatDescription.getRootType().isGlobal()) |
| 366 | | | { |
| 367 | 0 | | item.setGlobal(true); |
| 368 | | | } |
| 369 | 0 | | addItemToResource(mRootMatcher.group(mFilePos), item); |
| 370 | | | } |
| 371 | 0 | | mRootMatcher.region( |
| 372 | | | mSourceFile.getPos(), mSourceFile.getContent().length()); |
| 373 | 0 | | } |
| 374 | | | else |
| 375 | | | { |
| 376 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 377 | | | { |
| 378 | 0 | | logger.fine("No match after " + mSourceFile.getPos() |
| 379 | | | + " '" + StringUtil.trimLength( |
| 380 | | | mSourceFile.getContent().substring( |
| 381 | | | mSourceFile.getPos()), |
| 382 | | | MAX_DEBUG_TEXT_CHARS)); |
| 383 | | | } |
| 384 | | | |
| 385 | 0 | | mSourceFile.setPos(mSourceFile.getContent().length()); |
| 386 | | | } |
| 387 | 0 | | } |
| 388 | | | |
| 389 | | | private void addItemToResource (String resourceFilename, Item item) |
| 390 | | | { |
| 391 | 0 | | final ResourceInfo info = ResourceInfo.lookup(resourceFilename); |
| 392 | 0 | | if (info != null || item.isGlobal()) |
| 393 | | | { |
| 394 | | | final List<Item> l; |
| 395 | 0 | | if (mItems.containsKey(info)) |
| 396 | | | { |
| 397 | 0 | | l = mItems.get(info); |
| 398 | | | } |
| 399 | | | else |
| 400 | | | { |
| 401 | 0 | | l = new ArrayList<Item>(); |
| 402 | 0 | | mItems.put(info, l); |
| 403 | | | } |
| 404 | | | |
| 405 | 0 | | final Iterator<Item> i = l.iterator(); |
| 406 | 0 | | while (i.hasNext()) |
| 407 | | | { |
| 408 | 0 | | final Item it = i.next(); |
| 409 | 0 | | if (it.getLine() == item.getLine() |
| 410 | | | && it.getColumn() == item.getColumn() |
| 411 | | | && it.getOrigin() == item.getOrigin() |
| 412 | | | && ObjectUtil.equals(it.getMessage(), item.getMessage())) |
| 413 | | | { |
| 414 | 0 | | i.remove(); |
| 415 | 0 | | break; |
| 416 | | | } |
| 417 | 0 | | } |
| 418 | 0 | | l.add(item); |
| 419 | 0 | | } |
| 420 | | | else |
| 421 | | | { |
| 422 | 0 | | logger.finer("Ignore findings for resource '" |
| 423 | | | + resourceFilename + "' type was " |
| 424 | | | + item.getFindingType() + "."); |
| 425 | | | } |
| 426 | 0 | | } |
| 427 | | | |
| 428 | | | private void initializeFindingTypes () |
| 429 | | | { |
| 430 | 0 | | final FindingDescription root |
| 431 | | | = mFindingTypeFormatDescription.getRootType(); |
| 432 | 0 | | final List<FindingDescription> findingTypes |
| 433 | | (5) | = mFindingTypeFormatDescription.getFindingType(); |
| 434 | 0 | | for (FindingDescription findingDesc : findingTypes) |
| 435 | | | { |
| 436 | 0 | | final GenericFindingType gft |
| 437 | | | = new GenericFindingType(root, findingDesc); |
| 438 | 0 | | mFindingTypes.add(gft); |
| 439 | 0 | | } |
| 440 | 0 | | Collections.sort( |
| 441 | | | mFindingTypes, new GenericFindingType.OrderByPriority()); |
| 442 | 0 | | } |
| 443 | | | |
| 444 | | | |
| 445 | | | static final class SourceFile |
| 446 | | | { |
| 447 | | | private final File mFile; |
| 448 | | | private final String mContent; |
| 449 | | | private int mPos; |
| 450 | | | |
| 451 | | | public SourceFile (File file) |
| 452 | | | throws IOException |
| 453 | 0 | | { |
| 454 | 0 | | mFile = file; |
| 455 | 0 | | Reader in = null; |
| 456 | 0 | | Reader buffered = null; |
| 457 | | | try |
| 458 | | | { |
| 459 | 0 | | in = new FileReader(file); |
| 460 | 0 | | buffered = new BufferedReader(in); |
| 461 | 0 | | mContent = IoUtil.readFullyNormalizeNewLine(buffered); |
| 462 | | | } |
| 463 | | | finally |
| 464 | | | { |
| 465 | 0 | | IoUtil.close(buffered); |
| 466 | 0 | | IoUtil.close(in); |
| 467 | 0 | | } |
| 468 | 0 | | mPos = 0; |
| 469 | 0 | | } |
| 470 | | | |
| 471 | | | |
| 472 | | | @return |
| 473 | | | |
| 474 | | | public int getPos () |
| 475 | | | { |
| 476 | 0 | | return mPos; |
| 477 | | | } |
| 478 | | | |
| 479 | | | |
| 480 | | | @param |
| 481 | | | |
| 482 | | | public void setPos (int pos) |
| 483 | | | { |
| 484 | 0 | | mPos = pos; |
| 485 | 0 | | } |
| 486 | | | |
| 487 | | | |
| 488 | | | @return |
| 489 | | | |
| 490 | | | public File getFile () |
| 491 | | | { |
| 492 | 0 | | return mFile; |
| 493 | | | } |
| 494 | | | |
| 495 | | | |
| 496 | | | @return |
| 497 | | | |
| 498 | | | public String getContent () |
| 499 | | | { |
| 500 | 0 | | return mContent; |
| 501 | | | } |
| 502 | | | |
| 503 | | | public boolean readFully () |
| 504 | | | { |
| 505 | 0 | | return mPos >= mContent.length(); |
| 506 | | | } |
| 507 | | | } |
| 508 | | | } |