-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
PluginXmlParser.java
287 lines (219 loc) · 7.47 KB
/
PluginXmlParser.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.build.mavenplugin;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* A parser for a Maven plugin's {@code plugin.xml} file.
*
* @author Andy Wilkinson
* @author Mike Smithson
*/
class PluginXmlParser {
private final XPath xpath;
PluginXmlParser() {
this.xpath = XPathFactory.newInstance().newXPath();
}
Plugin parse(File pluginXml) {
try {
Node root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(pluginXml);
List<Mojo> mojos = parseMojos(root);
return new Plugin(textAt("//plugin/groupId", root), textAt("//plugin/artifactId", root),
textAt("//plugin/version", root), textAt("//plugin/goalPrefix", root), mojos);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
private String textAt(String path, Node source) throws XPathExpressionException {
String text = this.xpath.evaluate(path + "/text()", source);
return text.isEmpty() ? null : text;
}
private List<Mojo> parseMojos(Node plugin) throws XPathExpressionException {
List<Mojo> mojos = new ArrayList<>();
for (Node mojoNode : nodesAt("//plugin/mojos/mojo", plugin)) {
mojos.add(new Mojo(textAt("goal", mojoNode), format(textAt("description", mojoNode)),
parseParameters(mojoNode)));
}
return mojos;
}
private Iterable<Node> nodesAt(String path, Node source) throws XPathExpressionException {
return IterableNodeList.of((NodeList) this.xpath.evaluate(path, source, XPathConstants.NODESET));
}
private List<Parameter> parseParameters(Node mojoNode) throws XPathExpressionException {
Map<String, String> defaultValues = new HashMap<>();
Map<String, String> userProperties = new HashMap<>();
for (Node parameterConfigurationNode : nodesAt("configuration/*", mojoNode)) {
String userProperty = parameterConfigurationNode.getTextContent();
if (userProperty != null && !userProperty.isEmpty()) {
userProperties.put(parameterConfigurationNode.getNodeName(),
userProperty.replace("${", "`").replace("}", "`"));
}
Node defaultValueAttribute = parameterConfigurationNode.getAttributes().getNamedItem("default-value");
if (defaultValueAttribute != null && !defaultValueAttribute.getTextContent().isEmpty()) {
defaultValues.put(parameterConfigurationNode.getNodeName(), defaultValueAttribute.getTextContent());
}
}
List<Parameter> parameters = new ArrayList<>();
for (Node parameterNode : nodesAt("parameters/parameter", mojoNode)) {
parameters.add(parseParameter(parameterNode, defaultValues, userProperties));
}
return parameters;
}
private Parameter parseParameter(Node parameterNode, Map<String, String> defaultValues,
Map<String, String> userProperties) throws XPathExpressionException {
return new Parameter(textAt("name", parameterNode), textAt("type", parameterNode),
booleanAt("required", parameterNode), booleanAt("editable", parameterNode),
format(textAt("description", parameterNode)), defaultValues.get(textAt("name", parameterNode)),
userProperties.get(textAt("name", parameterNode)), textAt("since", parameterNode));
}
private boolean booleanAt(String path, Node node) throws XPathExpressionException {
return Boolean.parseBoolean(textAt(path, node));
}
private String format(String input) {
return input.replace("<code>", "`").replace("</code>", "`").replace("<", "<").replace(">", ">")
.replace("<br>", " ").replace("\n", " ").replace(""", "\"").replaceAll("\\{@code (.*?)}", "`$1`")
.replaceAll("\\{@link (.*?)}", "`$1`").replaceAll("\\{@literal (.*?)}", "`$1`")
.replaceAll("<a href=.\"(.*?)\".>(.*?)</a>", "$1[$2]");
}
private static final class IterableNodeList implements Iterable<Node> {
private final NodeList nodeList;
private IterableNodeList(NodeList nodeList) {
this.nodeList = nodeList;
}
private static Iterable<Node> of(NodeList nodeList) {
return new IterableNodeList(nodeList);
}
@Override
public Iterator<Node> iterator() {
return new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return this.index < IterableNodeList.this.nodeList.getLength();
}
@Override
public Node next() {
return IterableNodeList.this.nodeList.item(this.index++);
}
};
}
}
static final class Plugin {
private final String groupId;
private final String artifactId;
private final String version;
private final String goalPrefix;
private final List<Mojo> mojos;
private Plugin(String groupId, String artifactId, String version, String goalPrefix, List<Mojo> mojos) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.goalPrefix = goalPrefix;
this.mojos = mojos;
}
String getGroupId() {
return this.groupId;
}
String getArtifactId() {
return this.artifactId;
}
String getVersion() {
return this.version;
}
String getGoalPrefix() {
return this.goalPrefix;
}
List<Mojo> getMojos() {
return this.mojos;
}
}
static final class Mojo {
private final String goal;
private final String description;
private final List<Parameter> parameters;
private Mojo(String goal, String description, List<Parameter> parameters) {
this.goal = goal;
this.description = description;
this.parameters = parameters;
}
String getGoal() {
return this.goal;
}
String getDescription() {
return this.description;
}
List<Parameter> getParameters() {
return this.parameters;
}
}
static final class Parameter {
private final String name;
private final String type;
private final boolean required;
private final boolean editable;
private final String description;
private final String defaultValue;
private final String userProperty;
private final String since;
private Parameter(String name, String type, boolean required, boolean editable, String description,
String defaultValue, String userProperty, String since) {
this.name = name;
this.type = type;
this.required = required;
this.editable = editable;
this.description = description;
this.defaultValue = defaultValue;
this.userProperty = userProperty;
this.since = since;
}
String getName() {
return this.name;
}
String getType() {
return this.type;
}
boolean isRequired() {
return this.required;
}
boolean isEditable() {
return this.editable;
}
String getDescription() {
return this.description;
}
String getDefaultValue() {
return this.defaultValue;
}
String getUserProperty() {
return this.userProperty;
}
String getSince() {
return this.since;
}
}
}