/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.xml.util;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.html.HtmlTag;
import com.intellij.psi.impl.source.resolve.reference.ElementManipulator;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlChildRole;
import com.intellij.xml.XmlBundle;
import com.intellij.util.IncorrectOperationException;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.lang.ASTNode;

import org.jetbrains.annotations.NonNls;

/**
 * @author Maxim Mossienko
 */
public class CheckEmptyScriptTagInspection extends LocalInspectionTool {
  private static final Logger LOG = Logger.getInstance("#com.intellij.xml.util.CheckEmptyScriptTagInspection");
  @NonNls private static final String SCRIPT_TAG_NAME = "script";

  public boolean isEnabledByDefault() {
    return true;
  }

  public PsiElementVisitor buildVisitor(final ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {
      public void visitReferenceExpression(PsiReferenceExpression expression) {}

      public void visitXmlTag(final XmlTag tag) {
        if (SCRIPT_TAG_NAME.equals(tag.getName()) ||
            (tag instanceof HtmlTag && SCRIPT_TAG_NAME.equalsIgnoreCase(tag.getLocalName()))
           ) {
          final ASTNode child = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(tag.getNode());

          if (child != null) {

            final LocalQuickFix fix = new LocalQuickFix() {
              public String getName() {
                return XmlBundle.message("html.inspections.check.empty.script.tag.fix.message");
              }

              public void applyFix(Project project, ProblemDescriptor descriptor) {
                final StringBuilder builder = new StringBuilder(tag.getText());
                builder.replace(builder.length() - 2, builder.length() - 1, "></" + SCRIPT_TAG_NAME + ">");

                try {
                  final XmlTag tagFromText = tag.getManager().getElementFactory().createTagFromText(builder.toString());
                  tag.replace(tagFromText);
                }
                catch (IncorrectOperationException e) {
                  LOG.error(e);
                }
              }

              //to appear in "Apply Fix" statement when multiple Quick Fixes exist
              public String getFamilyName() {
                return getName();
              }
            };

            holder.registerProblem(tag,
                                   XmlBundle.message("html.inspections.check.empty.script.message"),
                                   fix);
          }
        }
      }
    };
  }

  public String getGroupDisplayName() {
    return GroupNames.HTML_INSPECTIONS;
  }

  public String getDisplayName() {
    return XmlBundle.message("html.inspections.check.empty.script.tag");
  }

  @NonNls
  public String getShortName() {
    return "CheckEmptyScriptTag";
  }
}

@@ -129,35 +129,30 @@
 eIns
-pection.LocalInspectionTool
+ight.daemon.GroupNames
 ;%0Aim
@@ -184,30 +184,35 @@
 pection.
-ProblemsHolder
+LocalInspectionTool
 ;%0Aimport
@@ -333,31 +333,71 @@
 lij.
-openapi.project.Project
+codeInspection.ProblemsHolder;%0Aimport com.intellij.lang.ASTNode
 ;%0Aim
@@ -437,24 +437,69 @@
 tic.Logger;%0A
+import com.intellij.openapi.project.Project;%0A
 import com.i
@@ -643,440 +643,151 @@
 psi.
-impl.source.resolve.reference.ElementManipulator;%0Aimport com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;%0Aimport com.intellij.psi.xml.XmlAttributeValue;%0Aimport com.intellij.psi.xml.XmlTag;%0Aimport com.intellij.psi.xml.XmlChildRole;%0Aimport com.intellij.xml.XmlBundle;%0Aimport com.intellij.util.IncorrectOperationException;%0Aimport com.intellij.codeInsight.daemon.GroupNames;%0Aimport com.intellij.lang.ASTNod
+xml.XmlChildRole;%0Aimport com.intellij.psi.xml.XmlTag;%0Aimport com.intellij.util.IncorrectOperationException;%0Aimport com.intellij.xml.XmlBundl
 e;%0A
-%0A
 impo

optimize imports