/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.xml.util;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.XmlInspectionGroupNames;
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.xml.*;
import com.intellij.util.containers.HashMap;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

/**
 * @author Maxim Mossienko
 */
public class CheckDtdReferencesInspection extends XmlSuppressableInspectionTool {
  @Override
  public boolean isEnabledByDefault() {
    return true;
  }

  @Override
  @NotNull
  public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new XmlElementVisitor() {

      private final Map<PsiFile, Boolean> myDoctypeMap = new HashMap<PsiFile, Boolean>();

      @Override
      public void visitXmlElement(final XmlElement element) {
        if (isHtml5Doctype(element)) {
          return;
        }

        if (element instanceof XmlElementContentSpec ||
            element instanceof XmlEntityRef
          ) {
          doCheckRefs(element, holder);
        }
      }

      private boolean isHtml5Doctype(XmlElement element) {
        if (HtmlUtil.isHtml5Context(element)) {
          return true;
        }

        PsiFile file = element.getContainingFile();
        if (file instanceof XmlFile) {
          if (!myDoctypeMap.containsKey(file)) {
            myDoctypeMap.put(file, computeHtml5Doctype((XmlFile)file));
          }
          return myDoctypeMap.get(file);
        }
        return false;
      }

      private boolean computeHtml5Doctype(XmlFile file) {
        XmlDoctype doctype = null;
        //Search for doctypes from providers
        for (HtmlDoctypeProvider provider : HtmlDoctypeProvider.EP_NAME.getExtensions()) {
          doctype = provider.getDoctype(file);
          if (doctype != null) {
            break;
          }
        }

        return doctype != null && HtmlUtil.isHtml5Doctype(doctype);
      }
    };
  }

  private static void doCheckRefs(final XmlElement element, final ProblemsHolder holder) {
    for (PsiReference ref : element.getReferences()) {
      ProgressManager.checkCanceled();
      if (XmlHighlightVisitor.hasBadResolve(ref, true)) {
        if (ref.getElement() instanceof XmlElementContentSpec) {
          final String image = ref.getCanonicalText();
          if (image.equals("-") || image.equals("O")) continue;
        }
        holder.registerProblem(ref);
      }
    }
  }

  @Override
  @NotNull
  public HighlightDisplayLevel getDefaultLevel() {
    return HighlightDisplayLevel.ERROR;
  }

  @Override
  @NotNull
  public String getGroupDisplayName() {
    return XmlInspectionGroupNames.XML_INSPECTIONS;
  }

  @Override
  @NotNull
  public String getDisplayName() {
    return XmlBundle.message("xml.inspections.check.dtd.references");
  }

  @Override
  @NotNull
  @NonNls
  public String getShortName() {
    return "CheckDtdRefs";
  }
}

@@ -1039,41 +1039,8 @@
 or;%0A
-import com.intellij.psi.PsiFile;%0A
 impo
@@ -1144,17 +1144,26 @@
 psi.xml.
-*
+XmlElement
 ;%0Aimport
@@ -1180,31 +1180,37 @@
 lij.
-util.containers.HashMap
+psi.xml.XmlElementContentSpec
 ;%0Aim
@@ -1231,62 +1231,63 @@
 lij.
+psi.
 xml.Xml
-Bundle;%0Aimport org.jetbrains.annotations.NonNls
+EntityRef;%0Aimport com.intellij.xml.XmlBundle
 ;%0Aim
@@ -1331,31 +1331,8 @@
 l;%0A%0A
-import java.util.Map;%0A%0A
 /**%0A
@@ -1682,100 +1682,8 @@
 ) %7B%0A
-%0A      private final Map%3CPsiFile, Boolean%3E myDoctypeMap = new HashMap%3CPsiFile, Boolean%3E();%0A%0A
     
@@ -1760,76 +1760,8 @@
 ) %7B%0A
-        if (isHtml5Doctype(element)) %7B%0A          return;%0A        %7D%0A%0A
     
@@ -1931,883 +1931,8 @@
    %7D
-%0A%0A      private boolean isHtml5Doctype(XmlElement element) %7B%0A        if (HtmlUtil.isHtml5Context(element)) %7B%0A          return true;%0A        %7D%0A%0A        PsiFile file = element.getContainingFile();%0A        if (file instanceof XmlFile) %7B%0A          if (!myDoctypeMap.containsKey(file)) %7B%0A            myDoctypeMap.put(file, computeHtml5Doctype((XmlFile)file));%0A          %7D%0A          return myDoctypeMap.get(file);%0A        %7D%0A        return false;%0A      %7D%0A%0A      private boolean computeHtml5Doctype(XmlFile file) %7B%0A        XmlDoctype doctype = null;%0A        //Search for doctypes from providers%0A        for (HtmlDoctypeProvider provider : HtmlDoctypeProvider.EP_NAME.getExtensions()) %7B%0A          doctype = provider.getDoctype(file);%0A          if (doctype != null) %7B%0A            break;%0A          %7D%0A        %7D%0A%0A        return doctype != null && HtmlUtil.isHtml5Doctype(doctype);%0A      %7D
 %0A   
@@ -2807,105 +2807,7 @@
 %0A  %7D
-%0A%0A  @Override%0A  @NotNull%0A  @NonNls%0A  public String getShortName() %7B%0A    return %22CheckDtdRefs%22;%0A  %7D
 %0A%7D%0A

enable entities validation for html5 #WEB-7053 fixed