| 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.IOException; |
| 36 | | | import java.io.Writer; |
| 37 | | | import java.util.ArrayList; |
| 38 | | | import java.util.Arrays; |
| 39 | | | import java.util.Collection; |
| 40 | | | import java.util.Collections; |
| 41 | | | import java.util.HashMap; |
| 42 | | | import java.util.Iterator; |
| 43 | | | import java.util.List; |
| 44 | | | import java.util.Map; |
| 45 | | | |
| 46 | | | import org.jcoderz.commons.util.XmlUtil; |
| 47 | | | import org.jcoderz.phoenix.report.jaxb.Item; |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | @author |
| 55 | | | |
| 56 | 0 | | final class FindingsSummary |
| 57 | | | { |
| 58 | | | |
| 59 | 0 | | private static FindingsSummary sFindingsSummary = new FindingsSummary(); |
| 60 | | | |
| 61 | 0 | | private final Map<String, FindingSummary> mFindings |
| 62 | | | = new HashMap<String, FindingSummary>(); |
| 63 | 0 | | private int mOverallCounter = 0; |
| 64 | | | |
| 65 | | | private FindingsSummary () |
| 66 | 0 | | { |
| 67 | | | |
| 68 | 0 | | } |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | @return |
| 73 | | | |
| 74 | | | public static FindingsSummary getFindingsSummary () |
| 75 | | | { |
| 76 | 0 | | return sFindingsSummary; |
| 77 | | | } |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | @param |
| 82 | | | @return |
| 83 | | | |
| 84 | | | public static String getKeyForFinding (Item finding) |
| 85 | | | { |
| 86 | 0 | | return finding.getFindingType() + "_" |
| 87 | | | + finding.getSeverity().toString(); |
| 88 | | | } |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | @param |
| 95 | | | @return |
| 96 | | | |
| 97 | | | |
| 98 | | | public static String getKeyForFinding (FindingType findingType, |
| 99 | | | Severity severity) |
| 100 | | | { |
| 101 | 0 | | return findingType.getSymbol() + "_" + severity.toString(); |
| 102 | | | } |
| 103 | | | |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | @param |
| 108 | | | @param |
| 109 | | | |
| 110 | | | public static void addFinding (Item finding, FileSummary file) |
| 111 | | | { |
| 112 | 0 | | getFindingsSummary().getFindingSummary(finding) |
| 113 | | | .addFinding(finding, file); |
| 114 | 0 | | } |
| 115 | | | |
| 116 | | (1) | |
| 117 | | | |
| 118 | | | @param |
| 119 | | | @param |
| 120 | | | @return |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | public FindingSummary getFindingSummary (FindingType findingType, |
| 125 | | | Severity severity) |
| 126 | | | { |
| 127 | 0 | | return mFindings.get(getKeyForFinding(findingType, severity)); |
| 128 | | | } |
| 129 | | | |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | @param |
| 136 | | | @return |
| 137 | | | |
| 138 | | | |
| 139 | | | public FindingSummary getFindingSummary (Item item) |
| 140 | | | { |
| 141 | 0 | | final String key = getKeyForFinding(item); |
| 142 | | | |
| 143 | | | |
| 144 | 0 | | FindingSummary result = mFindings.get(key); |
| 145 | 0 | | if (result == null) |
| 146 | | | { |
| 147 | 0 | | result = new FindingSummary(item); |
| 148 | | | } |
| 149 | 0 | | return result; |
| 150 | | | } |
| 151 | | | |
| 152 | | | |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | |
| 157 | | | @return |
| 158 | | | |
| 159 | | | |
| 160 | | | Map<String, FindingSummary> getFindings () |
| 161 | | | { |
| 162 | 0 | | return Collections.unmodifiableMap(mFindings); |
| 163 | | | } |
| 164 | | | |
| 165 | | | {@inheritDoc} |
| 166 | | | public String toString () |
| 167 | | | { |
| 168 | 0 | | return "[FindingsSummary: " + mFindings + "(" + mOverallCounter + ")]"; |
| 169 | | | } |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | |
| 174 | | | |
| 175 | | | @param |
| 176 | | | @throws |
| 177 | | | |
| 178 | | | static void createOverallContent (Writer out) throws IOException |
| 179 | | | { |
| 180 | 0 | | final Collection<FindingSummary> colAllFindings |
| 181 | | | = getFindingsSummary().getFindings().values(); |
| 182 | 0 | | final FindingSummary[] allFindings |
| 183 | | | = colAllFindings.toArray( |
| 184 | | | new FindingSummary[colAllFindings.size()]); |
| 185 | | | |
| 186 | 0 | | Arrays.sort(allFindings); |
| 187 | | | |
| 188 | 0 | | Severity currentSeverity = null; |
| 189 | | | |
| 190 | 0 | | out.write("<table border='0' cellpadding='0' cellspacing='0' " |
| 191 | | | + "width='95%' summary='Summary of all findings.'>"); |
| 192 | 0 | | int row = 0; |
| 193 | 0 | | for (final FindingSummary summary : allFindings) |
| 194 | | | { |
| 195 | 0 | | if (summary.getSeverity() != currentSeverity) |
| 196 | | | { |
| 197 | 0 | | out.write("<tr><td colspan='3' class='severityheader'>"); |
| 198 | 0 | | currentSeverity = summary.getSeverity(); |
| 199 | 0 | | out.write("<a name='" + currentSeverity.toString() + "'/>"); |
| 200 | 0 | | out.write("Severity: "); |
| 201 | 0 | | out.write(currentSeverity.toString()); |
| 202 | 0 | | out.write("\n</td></tr>"); |
| 203 | 0 | | row = 0; |
| 204 | | | } |
| 205 | 0 | | row++; |
| 206 | 0 | | out.write("<tr class='" + currentSeverity |
| 207 | | | + Java2Html.toOddEvenString(row) + "'>"); |
| 208 | 0 | | out.write("<td class='finding-counter'>"); |
| 209 | 0 | | out.write(String.valueOf(summary.getCounter())); |
| 210 | 0 | | out.write("</td>"); |
| 211 | 0 | | out.write("<td class='finding-origin'>"); |
| 212 | 0 | | out.write(summary.getOrigin().toString()); |
| 213 | 0 | | out.write("</td>"); |
| 214 | 0 | | out.write("<td class='finding-data' width='100%'>"); |
| 215 | | | |
| 216 | 0 | | out.write("<a href='"); |
| 217 | 0 | | out.write(summary.createFindingDetailFilename()); |
| 218 | 0 | | out.write("' title='"); |
| 219 | 0 | | out.write(summary.getFindingType().getSymbol()); |
| 220 | 0 | | out.write("'>"); |
| 221 | | | |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | | | |
| 226 | | | |
| 227 | | | { |
| 228 | 0 | | out.write(summary.getFindingType().getShortText()); |
| 229 | | | } |
| 230 | 0 | | out.write("</a></td></tr>\n"); |
| 231 | | | } |
| 232 | 0 | | out.write("</table>"); |
| 233 | 0 | | } |
| 234 | | | |
| 235 | | | |
| 236 | | | |
| 237 | | | |
| 238 | | | |
| 239 | | | @author |
| 240 | | | |
| 241 | 0 | | final class FindingSummary implements Comparable<FindingSummary> |
| 242 | | | { |
| 243 | 0 | | private final Map<String, FindingOccurrence> mOccurrences |
| 244 | | | = new HashMap<String, FindingOccurrence>(); |
| 245 | | | private final Severity mSeverity; |
| 246 | | | private final Origin mOrigin; |
| 247 | | | private int mCounter; |
| 248 | 0 | | private boolean mFindingsHaveSameMessage = true; |
| 249 | | | private final String mFindingMessage; |
| 250 | | | private final FindingType mFindingType; |
| 251 | | | |
| 252 | | | |
| 253 | | | |
| 254 | | | |
| 255 | | | @param |
| 256 | | | |
| 257 | | | |
| 258 | | | public FindingSummary (Item finding) |
| 259 | 0 | | { |
| 260 | 0 | | final String key = getKeyForFinding(finding); |
| 261 | 0 | | mFindingType = FindingType.fromString(finding.getFindingType()); |
| 262 | 0 | | mSeverity = finding.getSeverity(); |
| 263 | 0 | | mOrigin = finding.getOrigin(); |
| 264 | 0 | | mFindingMessage = finding.getMessage(); |
| 265 | 0 | | mFindings.put(key, this); |
| 266 | 0 | | } |
| 267 | | | |
| 268 | | | |
| 269 | | | @return |
| 270 | | | |
| 271 | | | public int getCounter () |
| 272 | | | { |
| 273 | 0 | | return mCounter; |
| 274 | | | } |
| 275 | | | |
| 276 | | | |
| 277 | | | @return |
| 278 | | | |
| 279 | | | public Origin getOrigin () |
| 280 | | | { |
| 281 | 0 | | return mOrigin; |
| 282 | | | } |
| 283 | | | |
| 284 | | | |
| 285 | | | @return |
| 286 | | | |
| 287 | | | public Severity getSeverity () |
| 288 | | | { |
| 289 | 0 | | return mSeverity; |
| 290 | | | } |
| 291 | | | |
| 292 | | | |
| 293 | | | @return |
| 294 | | | |
| 295 | | | public String getFindingMessage () |
| 296 | | | { |
| 297 | 0 | | return mFindingMessage; |
| 298 | | | } |
| 299 | | | |
| 300 | | | @return |
| 301 | | | |
| 302 | | | public boolean isFindingsHaveSameMessage () |
| 303 | | | { |
| 304 | 0 | | return mFindingsHaveSameMessage; |
| 305 | | | } |
| 306 | | | |
| 307 | | | |
| 308 | | | @return |
| 309 | | | |
| 310 | | | public FindingType getFindingType () |
| 311 | | | { |
| 312 | 0 | | return mFindingType; |
| 313 | | | } |
| 314 | | | |
| 315 | | | |
| 316 | | | @return |
| 317 | | | |
| 318 | | | public Map<String, FindingOccurrence> getOccurrences () |
| 319 | | | { |
| 320 | 0 | | return Collections.unmodifiableMap(mOccurrences); |
| 321 | | | } |
| 322 | | | |
| 323 | | | public FindingOccurrence getOccurrence (FileSummary fileSummary) |
| 324 | | | { |
| 325 | 0 | | FindingOccurrence result = |
| 326 | | | getOccurrence(fileSummary.getFullClassName()); |
| 327 | | | |
| 328 | 0 | | if (result == null) |
| 329 | | | { |
| 330 | 0 | | result = new FindingOccurrence(fileSummary); |
| 331 | | | } |
| 332 | | | |
| 333 | 0 | | return result; |
| 334 | | | } |
| 335 | | | |
| 336 | | | public void addFinding (Item finding, FileSummary summary) |
| 337 | | | { |
| 338 | 0 | | if (mFindingsHaveSameMessage) |
| 339 | | | { |
| 340 | 0 | | if (mFindingMessage == null) |
| 341 | | | { |
| 342 | 0 | | mFindingsHaveSameMessage |
| 343 | | | = (finding.getMessage() == null); |
| 344 | | | } |
| 345 | | | else |
| 346 | | | { |
| 347 | 0 | | mFindingsHaveSameMessage |
| 348 | | | = mFindingMessage.equals(finding.getMessage()); |
| 349 | | | } |
| 350 | | | } |
| 351 | 0 | | getOccurrence(summary).addFinding(finding); |
| 352 | 0 | | } |
| 353 | | | |
| 354 | | | {@inheritDoc} |
| 355 | | | public String toString () |
| 356 | | | { |
| 357 | 0 | | return "[" + mFindingType + "(" + mSeverity |
| 358 | | | + (mFindingsHaveSameMessage ? " " + mFindingMessage : "") |
| 359 | | | + "): " + mOccurrences + "(" + mCounter + ")]"; |
| 360 | | | } |
| 361 | | | |
| 362 | | | |
| 363 | | | |
| 364 | | | {@inheritDoc} |
| 365 | | | {@link } |
| 366 | | | |
| 367 | | | |
| 368 | | | |
| 369 | | | |
| 370 | | | public int compareTo (FindingSummary other) |
| 371 | | | { |
| 372 | 0 | (2) | int result = -mSeverity.compareTo(other.mSeverity); |
| 373 | 0 | | if (result == 0) |
| 374 | | | { |
| 375 | 0 | | result = other.mCounter - mCounter; |
| 376 | | | } |
| 377 | 0 | | return result; |
| 378 | | | } |
| 379 | | | |
| 380 | | | private void addOccurrence (FindingOccurrence occurrence) |
| 381 | | | { |
| 382 | 0 | | mOccurrences.put(occurrence.getFullClassName(), occurrence); |
| 383 | 0 | | } |
| 384 | | | |
| 385 | | | private FindingOccurrence getOccurrence (String filename) |
| 386 | | | { |
| 387 | 0 | | return mOccurrences.get(filename); |
| 388 | | | } |
| 389 | | | |
| 390 | | | public String createFindingDetailFilename () |
| 391 | | | { |
| 392 | 0 | | return "finding-" + getSeverity() + "-" |
| 393 | | | + getFindingType().getSymbol() + ".html"; |
| 394 | | | } |
| 395 | | | |
| 396 | | (3)(4) | public void createFindingTypeContent (Writer out) |
| 397 | | | throws IOException |
| 398 | | | { |
| 399 | | (5) | |
| 400 | 0 | (6) | final FindingOccurrence[] allFindings |
| 401 | | | = mOccurrences.values().toArray(new FindingOccurrence[0]); |
| 402 | | | |
| 403 | 0 | | Arrays.sort(allFindings); |
| 404 | | | |
| 405 | 0 | | out.write("<h1><a href='index.html'>View by Classes</a></h1>"); |
| 406 | 0 | | out.write("<h1><a href='findings.html'>Findings - Overview</a></h1>"); |
| 407 | | | |
| 408 | 0 | | out.write("<h1 title='"); |
| 409 | 0 | | out.write(getFindingType().getSymbol()); |
| 410 | 0 | | out.write("'>"); |
| 411 | | | |
| 412 | 0 | | out.write(getSeverity().toString()); |
| 413 | 0 | | out.write(" "); |
| 414 | 0 | | out.write(getFindingType().getShortText()); |
| 415 | 0 | | out.write(" ("); |
| 416 | 0 | | out.write(getOrigin().toString()); |
| 417 | 0 | | out.write(")"); |
| 418 | 0 | | out.write("</h1>\n"); |
| 419 | | | |
| 420 | 0 | | if (isFindingsHaveSameMessage() |
| 421 | | | && getFindingMessage() != null) |
| 422 | | | { |
| 423 | 0 | | out.write("<h2>"); |
| 424 | 0 | | out.write(XmlUtil.escape(getFindingMessage())); |
| 425 | 0 | | out.write("</h2>\n"); |
| 426 | | | } |
| 427 | | | |
| 428 | 0 | | if (getWikiPrefix() != null) |
| 429 | | | { |
| 430 | 0 | | out.write("<a href='" + getWikiPrefix() |
| 431 | | | + getFindingType().getSymbol() |
| 432 | | | + "'>Further info on the wiki.</a>\n"); |
| 433 | | | } |
| 434 | | | |
| 435 | 0 | | out.write("<blockquote>\n"); |
| 436 | 0 | | out.write(getFindingType().getDescription()); |
| 437 | 0 | | out.write("</blockquote>\n"); |
| 438 | | | |
| 439 | | | |
| 440 | 0 | | out.write("<table border='0' cellpadding='0' cellspacing='0' " |
| 441 | | | + "width='95%' summary='Places of this finding.'>"); |
| 442 | | | |
| 443 | | | for (final FindingSummary.FindingOccurrence |
| 444 | 0 | | occurrence : allFindings) |
| 445 | | | { |
| 446 | 0 | | out.write("<tr><td class='findingtype-counter'>"); |
| 447 | 0 | | out.write(Integer.toString(occurrence.getFindings().size())); |
| 448 | 0 | | out.write("</td><td class='findingtype-class' width='100%'>"); |
| 449 | | | |
| 450 | | | |
| 451 | | | |
| 452 | 0 | | out.write(occurrence.getFullClassName()); |
| 453 | 0 | | out.write("</td></tr>"); |
| 454 | | | |
| 455 | 0 | | out.write("<tr><td class='findingtype-data' colspan='2'>"); |
| 456 | | | |
| 457 | 0 | | final Iterator<Item> i = occurrence.getFindings().iterator(); |
| 458 | 0 | | while (i.hasNext()) |
| 459 | | | { |
| 460 | 0 | | final Item item = i.next(); |
| 461 | 0 | | final String htmlLink = occurrence.getHtmlLink(); |
| 462 | 0 | | if (htmlLink != null) |
| 463 | | | { |
| 464 | 0 | | out.write("<a href='"); |
| 465 | 0 | | out.write(occurrence.getHtmlLink()); |
| 466 | 0 | | out.write("#LINE"); |
| 467 | 0 | | out.write(Integer.toString(item.getLine())); |
| 468 | 0 | | out.write("'>"); |
| 469 | | | } |
| 470 | 0 | | if (!isFindingsHaveSameMessage() && item.getMessage() != null) |
| 471 | | | { |
| 472 | 0 | | out.write(XmlUtil.escape(item.getMessage())); |
| 473 | | | } |
| 474 | 0 | | out.write(" ["); |
| 475 | 0 | | out.write(Integer.toString(item.getLine())); |
| 476 | 0 | | if (item.getColumn() != 0) |
| 477 | | | { |
| 478 | 0 | | out.write(":"); |
| 479 | 0 | | out.write(Integer.toString(item.getColumn())); |
| 480 | | | } |
| 481 | 0 | | out.write("]"); |
| 482 | 0 | | if (htmlLink != null) |
| 483 | | | { |
| 484 | 0 | | out.write("</a>"); |
| 485 | | | } |
| 486 | 0 | | if (i.hasNext()) |
| 487 | | | { |
| 488 | 0 | | out.write(", "); |
| 489 | | | } |
| 490 | 0 | | if (!isFindingsHaveSameMessage() && item.getMessage() != null) |
| 491 | | | { |
| 492 | 0 | | out.write("<br />"); |
| 493 | | | } |
| 494 | 0 | | } |
| 495 | 0 | | out.write("</td></tr>\n"); |
| 496 | | | } |
| 497 | 0 | | out.write("</table>\n"); |
| 498 | 0 | | } |
| 499 | | | |
| 500 | | | |
| 501 | | | |
| 502 | | | @return |
| 503 | | | |
| 504 | | | private String getWikiPrefix () |
| 505 | | | { |
| 506 | 0 | | return System.getProperty(Java2Html.WIKI_BASE_PROPERTY); |
| 507 | | | } |
| 508 | | | |
| 509 | | | |
| 510 | | | |
| 511 | | | |
| 512 | | | |
| 513 | | | {@link } |
| 514 | | | |
| 515 | | | |
| 516 | | | @author |
| 517 | | | |
| 518 | 0 | | final class FindingOccurrence implements Comparable<FindingOccurrence> |
| 519 | | | { |
| 520 | | | private final FileSummary mFileSummary; |
| 521 | 0 | | private final List<Item> mFindingsInFile = new ArrayList<Item>(); |
| 522 | | | |
| 523 | | | private FindingOccurrence (FileSummary summary) |
| 524 | 0 | | { |
| 525 | 0 | | mFileSummary = summary; |
| 526 | 0 | | addOccurrence(this); |
| 527 | 0 | | } |
| 528 | | | |
| 529 | | | |
| 530 | | | @return |
| 531 | | | |
| 532 | | | public String getPackagename () |
| 533 | | | { |
| 534 | 0 | | return mFileSummary.getPackage(); |
| 535 | | | } |
| 536 | | | |
| 537 | | | public void addFinding (Item finding) |
| 538 | | | { |
| 539 | 0 | | mFindingsInFile.add(finding); |
| 540 | 0 | | mCounter++; |
| 541 | 0 | | mOverallCounter++; |
| 542 | 0 | | } |
| 543 | | | |
| 544 | | | public List<Item> getFindings () |
| 545 | | | { |
| 546 | 0 | | return Collections.unmodifiableList(mFindingsInFile); |
| 547 | | | } |
| 548 | | | |
| 549 | | | |
| 550 | | | @return |
| 551 | | | |
| 552 | | | public String getFullClassName () |
| 553 | | | { |
| 554 | 0 | | return mFileSummary.getFullClassName(); |
| 555 | | | } |
| 556 | | | |
| 557 | | | public String getClassName () |
| 558 | | | { |
| 559 | 0 | | return mFileSummary.getClassName(); |
| 560 | | | } |
| 561 | | | |
| 562 | | | public String getHtmlLink () |
| 563 | | | { |
| 564 | 0 | | return mFileSummary.getHtmlLink(); |
| 565 | | | } |
| 566 | | | |
| 567 | | | public int countFindingsInFile () |
| 568 | | | { |
| 569 | 0 | | return mFindingsInFile.size(); |
| 570 | | | } |
| 571 | | | |
| 572 | | | {@inheritDoc} |
| 573 | | | public String toString () |
| 574 | | | { |
| 575 | 0 | | return "[" + getClassName() + ": " + findingsToString() |
| 576 | | | + "(" + mFindingsInFile.size() + ")]"; |
| 577 | | | } |
| 578 | | | |
| 579 | | | public String findingsToString () |
| 580 | | | { |
| 581 | 0 | | final StringBuilder sb = new StringBuilder(); |
| 582 | 0 | | sb.append('{'); |
| 583 | 0 | | final Iterator<Item> i = mFindingsInFile.iterator(); |
| 584 | 0 | | while (i.hasNext()) |
| 585 | | | { |
| 586 | 0 | | final Item finding = i.next(); |
| 587 | 0 | | sb.append('@'); |
| 588 | 0 | | sb.append(finding.getLine()); |
| 589 | 0 | | sb.append(':'); |
| 590 | 0 | | sb.append(finding.getColumn()); |
| 591 | 0 | | if (i.hasNext()) |
| 592 | | | { |
| 593 | 0 | | sb.append(", "); |
| 594 | | | } |
| 595 | 0 | | } |
| 596 | 0 | | sb.append('}'); |
| 597 | 0 | | return sb.toString(); |
| 598 | | | } |
| 599 | | | |
| 600 | | | |
| 601 | | | {@inheritDoc} |
| 602 | | | {@link } |
| 603 | | | |
| 604 | | | |
| 605 | | | |
| 606 | | | public int compareTo (FindingOccurrence o) |
| 607 | | | { |
| 608 | 0 | (7) | return o.mFindingsInFile.size() - this.mFindingsInFile.size(); |
| 609 | | | } |
| 610 | | | } |
| 611 | | | } |
| 612 | | | } |