forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlPsiTreeUtil.java
236 lines (211 loc) · 8.15 KB
/
XmlPsiTreeUtil.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.util.xml;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlElement;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
import gnu.trove.THashSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings({
"PMD.LocalVariableCouldBeFinal",
"PMD.CommentSize",
"PMD.MethodArgumentCouldBeFinal",
"PMD.UseUtilityClass",
"PMD.OnlyOneReturn",
"PMD.UseObjectForClearerAPI"
})
public class XmlPsiTreeUtil {
public static final String EVENT_TAG_NAME = "event";
public static final String OBSERVER_TAG_NAME = "observer";
public static final String NAME_ATTRIBUTE = "name";
/**
* Get type tag of an argument.
*
* @param psiArgumentValueElement value element
* @return xml tag
*/
@Nullable
public static XmlTag getTypeTagOfArgument(final XmlElement psiArgumentValueElement) {
final XmlTag argumentTag = PsiTreeUtil.getParentOfType(
psiArgumentValueElement,
XmlTag.class
);
final XmlTag argumentsTag = PsiTreeUtil.getParentOfType(argumentTag, XmlTag.class);
return PsiTreeUtil.getParentOfType(argumentsTag, XmlTag.class);
}
/**
* Finds observer tags by event-observer name combination.
*/
@SuppressWarnings({
"PMD.CyclomaticComplexity"
})
public static Collection<XmlAttributeValue> findObserverTags(
final XmlFile xmlFile,
final String eventName,
final String observerName
) {
Collection<XmlAttributeValue> psiElements = new THashSet<>();
final XmlTag configTag = xmlFile.getRootTag();
if (configTag == null) {
return psiElements;
}
/* Loop through event tags */
for (XmlTag eventTag: configTag.getSubTags()) {
XmlAttribute eventNameAttribute = eventTag.getAttribute(NAME_ATTRIBUTE);
/* Check if event tag and name matches */
if (EVENT_TAG_NAME.equals(eventTag.getName())
&& eventNameAttribute != null
&& eventName.equals(eventNameAttribute.getValue())
) {
/* Loop through observer tags under matched event tag */
for (XmlTag observerTag: eventTag.getSubTags()) {
XmlAttribute observerNameAttribute = observerTag.getAttribute(NAME_ATTRIBUTE);
/* Check if observer tag and name matches */
if (OBSERVER_TAG_NAME.equals(observerTag.getName())
&& observerNameAttribute != null
&& observerNameAttribute.getValueElement() != null
&& observerName.equals(observerNameAttribute.getValue())
) {
psiElements.add(observerNameAttribute.getValueElement());
}
}
}
}
return psiElements;
}
/**
* Find attribute value elements based on the tag name.
*
* @param xmlFile xml file
* @param tagName xml tag name
* @param attributeName xml attribute name
* @return Collection of xml attributes.
*/
public static Collection<XmlAttributeValue> findAttributeValueElements(
final XmlFile xmlFile,
final String tagName,
final String attributeName) {
final Collection<XmlAttributeValue> psiElements = new THashSet<>();
XmlTag rootTag = xmlFile.getRootTag();
if (rootTag == null) {
return psiElements;
}
for (XmlTag tag: rootTag.findSubTags(tagName)) {
if (tag != null) {
XmlAttribute attribute = tag.getAttribute(attributeName);
if (attribute != null && attribute.getValueElement() != null) {
psiElements.add(attribute.getValueElement());
}
}
}
return psiElements;
}
/**
* Search for the attribute value elements.
*
* @param xmlFile xml file
* @param tagName tag name
* @param attributeName attribute name
* @param value value
* @return collection of xml elements
*/
public static Collection<XmlAttributeValue> findAttributeValueElements(
final XmlFile xmlFile,
final String tagName,
final String attributeName,
final String value) {
Collection<XmlAttributeValue> psiElements = findAttributeValueElements(
xmlFile,
tagName,
attributeName
);
psiElements.removeIf(e -> e.getValue() == null || !e.getValue().equals(value));
return psiElements;
}
/**
* Return collection of arguments items.
*
* @param xmlFile xml file
* @param parentTagName parent node name
* @param parentTagAttributeName parent attribute name
* @param parentTagAttributeNameValue parent attribute name value
* @param argumentTagAttributeName argument attribute name
* @return collection of xml elements
*/
public static Collection<XmlAttributeValue> findTypeArgumentsItemValueElement(
final XmlFile xmlFile,
final String parentTagName,
final String parentTagAttributeName,
final String parentTagAttributeNameValue,
final String argumentTagAttributeName) {
Collection<XmlAttributeValue> psiElements = new THashSet<>();
XmlTag rootTag = xmlFile.getRootTag();
if (rootTag == null) {
return psiElements;
}
for (XmlTag parentTag: rootTag.findSubTags(parentTagName)) {
if (parentTag != null && parentTag.findSubTags(ModuleDiXml.ARGUMENTS_TAG).length > 0) {
XmlAttribute parentAttribute = parentTag.getAttribute(parentTagAttributeName);
if (parentAttribute != null
&& parentAttribute.getValueElement() != null
&& Objects.equals(parentAttribute.getValue(), parentTagAttributeNameValue)
) {
iterateArgumentsNode(
parentTag,
argumentTagAttributeName,
psiElements
);
}
}
}
return psiElements;
}
private static Collection<XmlAttributeValue> iterateArgumentsNode(
final XmlTag parentTag,
final String argumentTagAttributeName,
Collection<XmlAttributeValue> psiElements
) {
for (XmlTag argumentsTag: parentTag.findSubTags(ModuleDiXml.ARGUMENTS_TAG)) {
if (argumentsTag != null
&& argumentsTag.findSubTags(ModuleDiXml.ARGUMENT_TAG).length > 0) {
for (XmlTag argumentTag: argumentsTag.findSubTags(ModuleDiXml.ARGUMENT_TAG)) {
XmlAttribute argumentAttribute = argumentTag.getAttribute(ModuleDiXml.NAME_TAG);
if (argumentAttribute != null
&& argumentAttribute.getValueElement() != null
&& Objects.equals(
argumentAttribute.getValue(),
argumentTagAttributeName)
) {
psiElements.add(argumentAttribute.getValueElement());
}
}
}
}
return psiElements;
}
/**
* Get child tags of parent.
*
* @param parentTag XmlTag
* @param subTagsName String
*
* @return List
*/
public static List<XmlTag> findSubTagsOfParent(
final XmlTag parentTag,
final String subTagsName
) {
return new LinkedList<>(Arrays.asList(parentTag.findSubTags(subTagsName)));
}
}