/** Creates a new instance of {@link CoberturaPublisher} from a submitted form. */
 @Override
 public CoberturaPublisher newInstance(StaplerRequest req, JSONObject formData)
     throws FormException {
   CoberturaPublisher instance = req.bindJSON(CoberturaPublisher.class, formData);
   ConvertUtils.register(CoberturaPublisherTarget.CONVERTER, CoverageMetric.class);
   List<CoberturaPublisherTarget> targets =
       req.bindParametersToList(CoberturaPublisherTarget.class, "cobertura.target.");
   instance.setTargets(targets);
   return instance;
 }
  /** Obtains the detailed {@link hudson.plugins.cobertura.targets.CoverageResult} instance. */
  public synchronized CoverageResult getResult() {
    if (report != null) {
      CoverageResult r = report.get();
      if (r != null) {
        return r;
      }
    }

    CoverageResult r = null;
    for (File reportFile : CoberturaPublisher.getCoberturaReports(owner)) {
      try {
        r = CoberturaCoverageParser.parse(reportFile, r);
      } catch (IOException e) {
        logger.log(Level.WARNING, "Failed to load " + reportFile, e);
      }
    }
    if (r != null) {
      r.setOwner(owner);
      report = new WeakReference<CoverageResult>(r);
      return r;
    } else {
      return null;
    }
  }
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  public HealthReport getBuildHealth() {
    if (health != null) {
      return health;
    }
    // try to get targets from root project (for maven modules targets are null)
    DescribableList rootpublishers = owner.getProject().getRootProject().getPublishersList();

    if (rootpublishers != null) {
      CoberturaPublisher publisher =
          (CoberturaPublisher) rootpublishers.get(CoberturaPublisher.class);
      if (publisher != null) {
        healthyTarget = publisher.getHealthyTarget();
        unhealthyTarget = publisher.getUnhealthyTarget();
      }
    }

    if (healthyTarget == null || unhealthyTarget == null) {
      return null;
    }

    if (result == null) {
      CoverageResult projectCoverage = getResult();
      result = new EnumMap<CoverageMetric, Ratio>(CoverageMetric.class);
      result.putAll(projectCoverage.getResults());
    }
    Map<CoverageMetric, Integer> scores = healthyTarget.getRangeScores(unhealthyTarget, result);
    int minValue = 100;
    CoverageMetric minKey = null;
    for (Map.Entry<CoverageMetric, Integer> e : scores.entrySet()) {
      if (e.getValue() < minValue) {
        minKey = e.getKey();
        minValue = e.getValue();
      }
    }
    if (minKey == null) {
      if (result == null || result.size() == 0) {
        return null;
      } else {
        for (Map.Entry<CoverageMetric, Integer> e : scores.entrySet()) {
          minKey = e.getKey();
        }
        if (minKey != null) {
          Localizable localizedDescription =
              Messages._CoberturaBuildAction_description(
                  result.get(minKey).getPercentage(),
                  result.get(minKey).toString(),
                  minKey.getName());
          health = new HealthReport(minValue, localizedDescription);
          return health;
        }
        return null;
      }

    } else {
      Localizable localizedDescription =
          Messages._CoberturaBuildAction_description(
              result.get(minKey).getPercentage(), result.get(minKey).toString(), minKey.getName());
      health = new HealthReport(minValue, localizedDescription);
      return health;
    }
  }
 public List<CoberturaPublisherTarget> getTargets(CoberturaPublisher instance) {
   if (instance == null) {
     return getDefaultTargets();
   }
   return instance.getTargets();
 }