This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 320
/
TailorParser.java
119 lines (105 loc) · 4.58 KB
/
TailorParser.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
package org.springframework.roo.addon.tailor.config.xml;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.roo.addon.tailor.actions.ActionConfig;
import org.springframework.roo.addon.tailor.config.CommandConfiguration;
import org.springframework.roo.addon.tailor.config.TailorConfiguration;
import org.springframework.roo.support.logging.HandlerUtils;
import org.springframework.roo.support.util.XmlUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
public class TailorParser {
private static final Logger LOGGER = HandlerUtils
.getLogger(TailorParser.class);
/**
* Maps the XML file contents to a TailorConfiguration object. It is
* possible to have multiple configurations in tailor.xml
*
* @param root
* @return list of tailor configurations
*/
public static List<TailorConfiguration> mapXmlToTailorConfiguration(
final Element root) {
final List<Element> elTailors = XmlUtils.findElements(
"/tailorconfiguration/tailor", root);
if (elTailors.isEmpty()) {
logTailorXMLInvalid("no <tailor> definitions found in <tailorconfiguration> root element");
return null;
}
final List<TailorConfiguration> configs = new ArrayList<TailorConfiguration>();
for (final Element eTailor : elTailors) {
final TailorConfiguration config = parseTailorConfiguration(eTailor);
if (config != null) {
configs.add(config);
}
}
return configs;
}
/**
* Maps the single XML tailor configuration to a TailorConfiguration object.
*
* @param tailor element
* @return tailor configurations
*/
public static TailorConfiguration parseTailorConfiguration(
final Element elTailor) {
if (StringUtils.isBlank(elTailor.getAttribute("name"))) {
logTailorXMLInvalid("<tailor> must have a name attribute");
return null;
}
final TailorConfiguration result = new TailorConfiguration(
elTailor.getAttribute("name"),
elTailor.getAttribute("description"));
final String activeAttribute = elTailor.getAttribute("activate");
if (StringUtils.isNotBlank(activeAttribute)) {
final boolean isActive = "true".equalsIgnoreCase(activeAttribute)
|| "yes".equalsIgnoreCase(activeAttribute);
result.setActive(isActive);
}
final List<Element> elConfigs = XmlUtils.findElements("config",
elTailor);
if (elConfigs.isEmpty()) {
logTailorXMLInvalid("<tailor> must have <config> child elements");
return null;
}
for (final Element elConfig : elConfigs) {
final String command = elConfig.getAttribute("command");
if (StringUtils.isBlank(command)) {
logTailorXMLInvalid("found <config> without command attribute");
return null;
}
final CommandConfiguration newCmdConfig = new CommandConfiguration();
newCmdConfig.setCommandName(command);
final List<Element> elActions = XmlUtils.findElements("action",
elConfig);
for (final Element elAction : elActions) {
// Determine the action type
if (StringUtils.isBlank(elAction.getAttribute("type"))) {
logTailorXMLInvalid("found <action> without type attribute");
return null;
}
final ActionConfig newAction = new ActionConfig(
elAction.getAttribute("type"));
final NamedNodeMap attributes = elAction.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
final Node item = attributes.item(i);
final String attributeKey = item.getNodeName();
if (!"type".equals(attributeKey)) {
newAction.setAttribute(attributeKey,
item.getNodeValue());
}
}
newCmdConfig.addAction(newAction);
}
result.addCommandConfig(newCmdConfig);
}
return result;
}
private static void logTailorXMLInvalid(final String msg) {
LOGGER.warning("Invalid tailor.xml - please correct and restart the shell to use this configuration ("
+ msg + ")");
}
}