2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.xml
.util
;
19 import com
.intellij
.codeInspection
.*;
20 import com
.intellij
.lang
.ASTNode
;
21 import com
.intellij
.lang
.Language
;
22 import com
.intellij
.lang
.html
.HTMLLanguage
;
23 import com
.intellij
.lang
.xml
.XMLLanguage
;
24 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import com
.intellij
.openapi
.fileTypes
.FileType
;
26 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
27 import com
.intellij
.openapi
.project
.Project
;
28 import com
.intellij
.openapi
.vfs
.ReadonlyStatusHandler
;
29 import com
.intellij
.psi
.PsiElementVisitor
;
30 import com
.intellij
.psi
.PsiFile
;
31 import com
.intellij
.psi
.PsiFileFactory
;
32 import com
.intellij
.psi
.XmlElementVisitor
;
33 import com
.intellij
.psi
.html
.HtmlTag
;
34 import com
.intellij
.psi
.xml
.XmlChildRole
;
35 import com
.intellij
.psi
.xml
.XmlFile
;
36 import com
.intellij
.psi
.xml
.XmlTag
;
37 import com
.intellij
.util
.IncorrectOperationException
;
38 import com
.intellij
.xml
.XmlBundle
;
39 import gnu
.trove
.THashSet
;
40 import org
.jetbrains
.annotations
.NonNls
;
41 import org
.jetbrains
.annotations
.NotNull
;
43 import java
.util
.Arrays
;
47 * @author Maxim Mossienko
49 public class CheckEmptyTagInspection
extends XmlSuppressableInspectionTool
{
50 private static final Logger LOG
= Logger
.getInstance("#com.intellij.xml.util.CheckEmptyTagInspection");
51 @NonNls private static final String SCRIPT_TAG_NAME
= "script";
52 private static Set
<String
> ourTagsWithEmptyEndsNotAllowed
= new THashSet
<String
>(Arrays
.asList(SCRIPT_TAG_NAME
, "div", "iframe"));
54 public boolean isEnabledByDefault() {
59 public PsiElementVisitor
buildVisitor(@NotNull final ProblemsHolder holder
, boolean isOnTheFly
) {
60 return new XmlElementVisitor() {
61 @Override public void visitXmlTag(final XmlTag tag
) {
62 if (isTagWithEmptyEndNotAllowed(tag
)) {
63 final ASTNode child
= XmlChildRole
.EMPTY_TAG_END_FINDER
.findChild(tag
.getNode());
67 final LocalQuickFix fix
= new LocalQuickFix() {
69 public String
getName() {
70 return XmlBundle
.message("html.inspections.check.empty.script.tag.fix.message");
73 public void applyFix(@NotNull Project project
, @NotNull ProblemDescriptor descriptor
) {
74 final XmlTag tag
= (XmlTag
)descriptor
.getPsiElement();
75 if (tag
== null) return;
76 final PsiFile psiFile
= tag
.getContainingFile();
78 if (psiFile
== null) return;
79 ReadonlyStatusHandler
.getInstance(project
).ensureFilesWritable(psiFile
.getVirtualFile());
81 final StringBuilder builder
= new StringBuilder(tag
.getText());
82 builder
.replace(builder
.length() - 2, builder
.length(), "></" + tag
.getLocalName() + ">");
85 final FileType fileType
= psiFile
.getFileType();
86 PsiFile file
= PsiFileFactory
.getInstance(tag
.getProject()).createFileFromText(
87 "dummy." + (fileType
== StdFileTypes
.JSP
|| tag
.getContainingFile().getLanguage() == HTMLLanguage
.INSTANCE ?
"html" : "xml"), builder
.toString());
89 tag
.replace(((XmlFile
)file
).getDocument().getRootTag());
91 catch (IncorrectOperationException e
) {
96 //to appear in "Apply Fix" statement when multiple Quick Fixes exist
98 public String
getFamilyName() {
103 holder
.registerProblem(tag
,
104 XmlBundle
.message("html.inspections.check.empty.script.message"),
112 static boolean isTagWithEmptyEndNotAllowed(final XmlTag tag
) {
113 String tagName
= tag
.getName();
114 if (tag
instanceof HtmlTag
) tagName
= tagName
.toLowerCase();
116 Language language
= tag
.getLanguage();
117 return (ourTagsWithEmptyEndsNotAllowed
.contains(tagName
) &&
118 language
!= XMLLanguage
.INSTANCE
) ||
119 (language
== HTMLLanguage
.INSTANCE
&&
120 ( !HtmlUtil
.isSingleHtmlTagL(tagName
) && tagName
.indexOf(':') == -1))
125 public String
getGroupDisplayName() {
126 return XmlInspectionGroupNames
.HTML_INSPECTIONS
;
130 public String
getDisplayName() {
131 return XmlBundle
.message("html.inspections.check.empty.tag");
136 public String
getShortName() {
137 return "CheckEmptyScriptTag";