forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAclResourceXmlInspection.java
92 lines (81 loc) · 3.4 KB
/
AclResourceXmlInspection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.inspections.xml;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
import com.magento.idea.magento2plugin.magento.files.ModuleAclXml;
import com.magento.idea.magento2plugin.stubs.indexes.xml.AclResourceIndex;
import java.util.List;
import org.jetbrains.annotations.NotNull;
public class AclResourceXmlInspection extends XmlSuppressableInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(
final @NotNull ProblemsHolder problemsHolder,
final boolean isOnTheFly
) {
return new XmlElementVisitor() {
private final InspectionBundle inspectionBundle = new InspectionBundle();
@Override
public void visitXmlTag(final XmlTag xmlTag) {
final PsiFile file = xmlTag.getContainingFile();
final String filename = file.getName();
if (!filename.equals(ModuleAclXml.FILE_NAME)) {
return;
}
if (!xmlTag.getName().equals(ModuleAclXml.XML_TAG_RESOURCE)) {
return;
}
final XmlAttribute identifier = xmlTag.getAttribute(ModuleAclXml.XML_ATTR_ID);
if (identifier == null) {
//should be handled by schema
return;
}
final String idValue = identifier.getValue();
if (idValue == null || idValue.isEmpty()) {
problemsHolder.registerProblem(
identifier,
inspectionBundle.message(
"inspection.error.idAttributeCanNotBeEmpty",
"id"
),
ProblemHighlightType.WARNING
);
return;
}
final XmlAttribute title = xmlTag.getAttribute(ModuleAclXml.XML_ATTR_TITLE);
if (title != null && title.getValue() != null) {
return;
}
final List<String> titles =
FileBasedIndex.getInstance().getValues(
AclResourceIndex.KEY,
idValue,
GlobalSearchScope.allScope(file.getProject()
)
);
if (titles.isEmpty()) {
problemsHolder.registerProblem(
identifier,
inspectionBundle.message(
"inspection.error.missingAttribute",
"title"
),
ProblemHighlightType.WARNING
);
}
}
};
}
}