/**
   * Creates an IMessage from an IProblem
   *
   * @param problem
   * @param f
   * @param translation
   * @param textDoc
   * @return message representation of the problem, or null if it could not create one
   */
  private IMessage createMessageFromProblem(
      IProblem problem, IFile f, IJsTranslation translation, IDocument textDoc) {
    int sourceStart = problem.getSourceStart();
    int sourceEnd = problem.getSourceEnd();
    if (sourceStart == -1) {
      return null;
    }
    sourceStart = translation.getWebPageOffset(sourceStart);
    sourceEnd = translation.getWebPageOffset(sourceEnd);
    /*
     * Bug 241794 - Validation shows errors when using JSP Expressions
     * inside JavaScript code
     */
    IStructuredDocument doc = (IStructuredDocument) textDoc;
    IStructuredDocumentRegion documentRegion = doc.getRegionAtCharacterOffset(sourceStart);
    if (documentRegion != null) {
      ITextRegion textRegion = documentRegion.getRegionAtCharacterOffset(sourceStart);
      /*
       * Filter out problems from areas that aren't simple JavaScript,
       * e.g. JSP.
       */
      if (textRegion != null && textRegion instanceof ITextRegionCollection) return null;
    }

    int sev =
        problem.isError()
            ? IMessage.HIGH_SEVERITY
            : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : IMessage.LOW_SEVERITY);
    IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
    // line numbers for marker starts @ 1
    // line numbers from document starts @ 0
    try {
      int lineNo = textDoc.getLineOfOffset(sourceStart) + 1;
      m.setLineNo(lineNo);
      m.setOffset(sourceStart);
      m.setLength(sourceEnd - sourceStart + 1);
    } catch (BadLocationException e) {
      Logger.logException(e);
    }
    return m;
  }
  void performValidation(IFile f, IReporter reporter, IStructuredModel model, boolean inBatch) {
    if (model instanceof IDOMModel) {
      IDOMModel domModel = (IDOMModel) model;
      JsTranslationAdapterFactory.setupAdapterFactory(domModel);
      IDOMDocument xmlDoc = domModel.getDocument();
      JsTranslationAdapter translationAdapter =
          (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
      // translationAdapter.resourceChanged();
      IJsTranslation translation = translationAdapter.getJsTranslation(false);
      if (!reporter.isCancelled()) {
        translation.setProblemCollectingActive(true);
        translation.reconcileCompilationUnit();
        List problems = translation.getProblems();
        // only update task markers if the model is the same as what's on disk
        boolean updateTasks = !domModel.isDirty() && f != null && f.isAccessible();
        if (updateTasks) {
          // remove old JavaScript task markers
          try {
            IMarker[] foundMarkers =
                f.findMarkers(JAVASCRIPT_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE);
            for (int i = 0; i < foundMarkers.length; i++) {
              foundMarkers[i].delete();
            }
          } catch (CoreException e) {
            Logger.logException(e);
          }
        }

        //				if(!inBatch) reporter.removeAllMessages(this, f);
        // add new messages
        for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) {
          IProblem problem = (IProblem) problems.get(i);
          IMessage m =
              createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument());
          if (m != null) {
            if (problem.getID() == IProblem.Task) {
              if (updateTasks) {
                // add new JavaScript task marker
                try {
                  IMarker task = f.createMarker(JAVASCRIPT_TASK_MARKER_TYPE);
                  task.setAttribute(IMarker.LINE_NUMBER, new Integer(m.getLineNumber()));
                  task.setAttribute(IMarker.CHAR_START, new Integer(m.getOffset()));
                  task.setAttribute(IMarker.CHAR_END, new Integer(m.getOffset() + m.getLength()));
                  task.setAttribute(IMarker.MESSAGE, m.getText());
                  task.setAttribute(IMarker.USER_EDITABLE, Boolean.FALSE);

                  switch (m.getSeverity()) {
                    case IMessage.HIGH_SEVERITY:
                      {
                        task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH));
                        task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
                      }
                      break;
                    case IMessage.LOW_SEVERITY:
                      {
                        task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW));
                        task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
                      }
                      break;
                    default:
                      {
                        task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
                        task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
                      }
                  }
                } catch (CoreException e) {
                  Logger.logException(e);
                }
              }
            } else {
              reporter.addMessage(fMessageOriginator, m);
            }
          }
        }
      }
    }
  }