@SuppressWarnings("unchecked")
  @Override
  protected void createProblems(
      IProgressMonitor monitor,
      IDocument document,
      IRegion region,
      List<ValidationProblem> problems)
      throws CoreException {
    Object lockObject;
    if (annotationModel instanceof ISynchronizable) {
      lockObject = ((ISynchronizable) annotationModel).getLockObject();
    } else {
      lockObject = annotationModel;
    }
    synchronized (lockObject) {
      List<Annotation> toRemove = null;
      Iterator<Annotation> annotationIterator = annotationModel.getAnnotationIterator();
      while (annotationIterator.hasNext()) {
        Annotation annotation = annotationIterator.next();
        if (ValidationProblemAnnotation.isValidationAnnotation(annotation)) {
          Position position = annotationModel.getPosition(annotation);
          int offset = position.getOffset();
          if (overlaps(region, offset, position.getLength()) || offset >= document.getLength()) {
            if (toRemove == null) {
              toRemove = new ArrayList<Annotation>();
            }
            toRemove.add(annotation);
          }
        }
      }

      Map<Annotation, Position> annotationsToAdd = new HashMap<Annotation, Position>();
      for (ValidationProblem problem : problems) {
        annotationsToAdd.put(
            new ValidationProblemAnnotation(problem),
            new Position(problem.getOffset(), problem.getLength()));
      }

      if (toRemove != null && annotationModel instanceof IAnnotationModelExtension) {
        Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
        ((IAnnotationModelExtension) annotationModel)
            .replaceAnnotations(annotationsToRemove, annotationsToAdd);
      } else {
        if (toRemove != null) {
          for (Annotation annotation : toRemove) {
            annotationModel.removeAnnotation(annotation);
          }
        }
        for (Map.Entry<Annotation, Position> entry : annotationsToAdd.entrySet()) {
          annotationModel.addAnnotation(entry.getKey(), entry.getValue());
        }
      }
    }
  }
Exemplo n.º 2
0
  protected void performValidation(File source, String markupContent) {
    if (!validate) {
      return;
    }
    if (markupLanguage == null) {
      throw new IllegalStateException();
    }
    log(
        MessageFormat.format(Messages.getString("MarkupTask.1"), source),
        Project.MSG_VERBOSE); // $NON-NLS-1$

    StandaloneMarkupValidator markupValidator =
        StandaloneMarkupValidator.getValidator(markupLanguage);

    List<ValidationProblem> problems = markupValidator.validate(markupContent);

    int errorCount = 0;
    int warningCount = 0;
    for (ValidationProblem problem : problems) {
      int messageLevel = Project.MSG_ERR;
      if (problem.getSeverity() == Severity.ERROR) {
        ++errorCount;
      } else if (problem.getSeverity() == Severity.WARNING) {
        ++warningCount;
        messageLevel = Project.MSG_WARN;
      }
      log(
          String.format("%s:%s %s", source.getName(), problem.getOffset(), problem.getMessage()),
          messageLevel); //$NON-NLS-1$
    }

    if ((errorCount > 0 && failOnValidationError)
        || (warningCount > 0 && failOnValidationWarning)) {
      throw new BuildException(
          MessageFormat.format(
              Messages.getString("MarkupTask.3"),
              errorCount, //$NON-NLS-1$
              warningCount,
              source));
    }
  }