forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlPsiTreeUtil.java
186 lines (168 loc) · 6.25 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
/**
* 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 {
/**
* 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);
}
/**
* 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)));
}
}