update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / xml / util / XmlDuplicatedIdInspection.java
blob280105f15dc4b55c1a8d8e3d61cffe0a71cc5778
1 /*
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.
16 package com.intellij.xml.util;
18 import com.intellij.codeHighlighting.HighlightDisplayLevel;
19 import com.intellij.codeInsight.daemon.XmlErrorMessages;
20 import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
21 import com.intellij.codeInspection.LocalInspectionTool;
22 import com.intellij.codeInspection.ProblemHighlightType;
23 import com.intellij.codeInspection.ProblemsHolder;
24 import com.intellij.openapi.extensions.Extensions;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.psi.*;
27 import com.intellij.psi.html.HtmlTag;
28 import com.intellij.psi.xml.XmlAttribute;
29 import com.intellij.psi.xml.XmlAttributeValue;
30 import com.intellij.psi.xml.XmlFile;
31 import com.intellij.psi.xml.XmlTag;
32 import com.intellij.xml.XmlBundle;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
36 /**
37 * @author Dmitry Avdeev
39 public class XmlDuplicatedIdInspection extends LocalInspectionTool {
41 @NotNull
42 @Override
43 public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
44 return new XmlElementVisitor() {
45 @Override
46 public void visitXmlAttributeValue(final XmlAttributeValue value) {
47 if (value.getTextRange().isEmpty()) {
48 return;
50 final PsiFile file = value.getContainingFile();
51 if (file instanceof XmlFile) {
52 final XmlRefCountHolder refHolder = XmlRefCountHolder.getInstance((XmlFile)file);
53 if (refHolder == null) return;
55 final PsiElement parent = value.getParent();
56 if (!(parent instanceof XmlAttribute)) return;
57 final XmlTag tag = ((XmlAttribute)parent).getParent();
58 if (tag == null) return;
60 if (refHolder.isValidatable(tag.getParent()) && refHolder.isDuplicateIdAttributeValue(value)) {
61 holder.registerProblem(value, XmlErrorMessages.message("duplicate.id.reference"), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
64 String idRef = XmlHighlightVisitor.getUnquotedValue(value, tag);
66 if (tag instanceof HtmlTag) {
67 idRef = idRef.toLowerCase();
70 if (XmlUtil.isSimpleXmlAttributeValue(idRef, value) && refHolder.isIdReferenceValue(value)) {
71 boolean hasIdDeclaration = refHolder.hasIdDeclaration(idRef);
72 if (!hasIdDeclaration && tag instanceof HtmlTag) {
73 hasIdDeclaration = refHolder.hasIdDeclaration(StringUtil.stripQuotesAroundValue(value.getText()));
76 if (!hasIdDeclaration) {
77 for(XmlIdContributor contributor: Extensions.getExtensions(XmlIdContributor.EP_NAME)) {
78 if (contributor.suppressExistingIdValidation((XmlFile)file)) {
79 return;
83 final FileViewProvider viewProvider = tag.getContainingFile().getViewProvider();
84 if (viewProvider instanceof MultiplePsiFilesPerDocumentFileViewProvider) {
85 holder.registerProblem(value, XmlErrorMessages.message("invalid.id.reference"), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
86 new XmlDeclareIdInCommentAction(idRef));
89 else {
90 holder.registerProblem(value, XmlErrorMessages.message("invalid.id.reference"), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
99 public boolean runForWholeFile() {
100 return false;
103 @NotNull
104 public HighlightDisplayLevel getDefaultLevel() {
105 return HighlightDisplayLevel.ERROR;
108 public boolean isEnabledByDefault() {
109 return true;
112 @NotNull
113 public String getGroupDisplayName() {
114 return XmlBundle.message("xml.inspections.group.name");
117 @NotNull
118 public String getDisplayName() {
119 return XmlBundle.message("xml.inspections.duplicated.id");
122 @NotNull
123 @NonNls
124 public String getShortName() {
125 return "XmlDuplicatedId";