2 * Copyright 2000-2011 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 org.jetbrains.idea.devkit.inspections;
18 import com.intellij.codeInspection.LocalInspectionToolSession;
19 import com.intellij.codeInspection.ProblemsHolder;
20 import com.intellij.openapi.util.Pair;
21 import com.intellij.openapi.util.TextRange;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiElementVisitor;
24 import com.intellij.psi.XmlElementVisitor;
25 import com.intellij.psi.util.InheritanceUtil;
26 import com.intellij.psi.xml.XmlTag;
27 import com.intellij.util.xml.DomElement;
28 import com.intellij.util.xml.DomUtil;
29 import org.jetbrains.annotations.Nls;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.idea.devkit.dom.Extension;
32 import org.jetbrains.idea.devkit.dom.ExtensionPoint;
33 import org.jetbrains.idea.devkit.dom.IdeaPlugin;
36 * @author Dmitry Avdeev
39 public class InspectionMappingConsistencyInspection extends DevKitInspectionBase {
43 public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder,
45 @NotNull LocalInspectionToolSession session) {
46 return new XmlElementVisitor() {
48 public void visitXmlTag(XmlTag tag) {
49 DomElement element = DomUtil.getDomElement(tag);
50 if (element instanceof Extension) {
51 ExtensionPoint extensionPoint = ((Extension)element).getExtensionPoint();
52 if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) {
53 boolean key = tag.getAttribute("key") != null;
54 boolean groupKey = tag.getAttribute("groupKey") != null;
56 if (tag.getAttribute("bundle") == null) {
57 checkDefaultBundle(element, holder);
60 else if (tag.getAttribute("displayName") == null) {
61 registerProblem(element, holder, "displayName or key should be specified");
64 if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) {
65 checkDefaultBundle(element, holder);
68 else if (tag.getAttribute("groupName") == null) {
69 registerProblem(element, holder, "groupName or groupKey should be specified");
77 private static void checkDefaultBundle(DomElement element, ProblemsHolder holder) {
78 IdeaPlugin plugin = DomUtil.getParentOfType(element, IdeaPlugin.class, true);
79 if (plugin != null && plugin.getResourceBundles().isEmpty()) {
80 registerProblem(element, holder, "Bundle should be specified");
84 private static void registerProblem(DomElement element, ProblemsHolder holder, String message) {
85 Pair<TextRange,PsiElement> range = DomUtil.getProblemRange(element.getXmlTag());
86 holder.registerProblem(range.second, range.first, message);
92 public String getDisplayName() {
93 return "<inspection> tag consistency";
98 public String getShortName() {
99 return "InspectionMappingConsistency";