private void setAffectedLines(final BugInstance warning, final Bug bug) {
   Iterator<BugAnnotation> annotationIterator = warning.annotationIterator();
   while (annotationIterator.hasNext()) {
     BugAnnotation bugAnnotation = annotationIterator.next();
     if (bugAnnotation instanceof SourceLineAnnotation) {
       SourceLineAnnotation annotation = (SourceLineAnnotation) bugAnnotation;
       bug.addLineRange(new LineRange(annotation.getStartLine(), annotation.getEndLine()));
     }
   }
 }
  /**
   * Sets the cloud information.
   *
   * @param collection the warnings collection
   * @param warning the warning
   * @param bug the bug
   * @return true, if this warning is not a bug and should be ignored
   */
  @edu.umd.cs.findbugs.annotations.SuppressWarnings("NP")
  private boolean setCloudInformation(
      final SortedBugCollection collection, final BugInstance warning, final Bug bug) {
    Cloud cloud = collection.getCloud();
    cloud.waitUntilIssueDataDownloaded();

    bug.setShouldBeInCloud(cloud.isOnlineCloud());
    Map<String, String> cloudDetails = collection.getXmlCloudDetails();
    bug.setDetailsUrlTemplate(cloudDetails.get(CLOUD_DETAILS_URL_PROPERTY));

    long firstSeen = cloud.getFirstSeen(warning);
    bug.setInCloud(cloud.isInCloud(warning));
    bug.setFirstSeen(firstSeen);
    int ageInDays = (int) ((collection.getAnalysisTimestamp() - firstSeen) / DAY_IN_MSEC);
    bug.setAgeInDays(ageInDays);
    bug.setReviewCount(cloud.getNumberReviewers(warning));

    return cloud.overallClassificationIsNotAProblem(warning);
  }
  /**
   * Returns the parsed FindBugs analysis file. This scanner accepts files in the native FindBugs
   * format.
   *
   * @param file the FindBugs analysis file
   * @param sources a collection of folders to scan for source files
   * @param moduleName name of maven module
   * @param hashToMessageMapping mapping of hash codes to messages
   * @param categories mapping from bug types to their categories
   * @return the parsed result (stored in the module instance)
   * @throws IOException if the file could not be parsed
   * @throws DocumentException in case of a parser exception
   */
  private Collection<FileAnnotation> parse(
      final InputStream file,
      final Collection<String> sources,
      final String moduleName,
      final Map<String, String> hashToMessageMapping,
      final Map<String, String> categories)
      throws IOException, DocumentException {
    SortedBugCollection collection = readXml(file);

    Project project = collection.getProject();
    for (String sourceFolder : sources) {
      project.addSourceDir(sourceFolder);
    }

    SourceFinder sourceFinder = new SourceFinder(project);
    String actualName = extractModuleName(moduleName, project);

    TreeStringBuilder stringPool = new TreeStringBuilder();
    List<FileAnnotation> annotations = new ArrayList<FileAnnotation>();
    Collection<BugInstance> bugs = collection.getCollection();

    for (BugInstance warning : bugs) {

      SourceLineAnnotation sourceLine = warning.getPrimarySourceLineAnnotation();

      String message = warning.getMessage();
      String type = warning.getType();
      if (message.contains("TEST: Unknown")) {
        message = FindBugsMessages.getInstance().getShortMessage(type, LocaleProvider.getLocale());
      }
      String category = categories.get(type);
      if (category
          == null) { // alternately, only if warning.getBugPattern().getType().equals("UNKNOWN")
        category = warning.getBugPattern().getCategory();
      }
      Bug bug =
          new Bug(
              getPriority(warning),
              StringUtils.defaultIfEmpty(
                  hashToMessageMapping.get(warning.getInstanceHash()), message),
              category,
              type,
              sourceLine.getStartLine(),
              sourceLine.getEndLine());
      bug.setInstanceHash(warning.getInstanceHash());
      bug.setRank(warning.getBugRank());

      boolean ignore = setCloudInformation(collection, warning, bug);
      if (!ignore) {
        bug.setNotAProblem(false);
        bug.setFileName(findSourceFile(project, sourceFinder, sourceLine));
        bug.setPackageName(warning.getPrimaryClass().getPackageName());
        bug.setModuleName(actualName);
        setAffectedLines(warning, bug);

        annotations.add(bug);
        bug.intern(stringPool);
      }
    }

    return applyFilters(annotations);
  }