1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.role; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
20 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
21 | |
import org.kuali.rice.kew.api.extension.ExtensionDefinition; |
22 | |
import org.kuali.rice.kew.engine.RouteContext; |
23 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
24 | |
import org.w3c.dom.Document; |
25 | |
import org.w3c.dom.Element; |
26 | |
import org.w3c.dom.Node; |
27 | |
import org.w3c.dom.NodeList; |
28 | |
import org.xml.sax.InputSource; |
29 | |
|
30 | |
import javax.xml.xpath.XPath; |
31 | |
import javax.xml.xpath.XPathConstants; |
32 | |
import javax.xml.xpath.XPathExpressionException; |
33 | |
import java.io.StringReader; |
34 | |
import java.util.ArrayList; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
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 | 0 | public class XPathQualifierResolver implements QualifierResolver { |
124 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(XPathQualifierResolver.class); |
125 | |
|
126 | |
private ExtensionDefinition extensionDefinition; |
127 | |
String xmlConfigData; |
128 | |
|
129 | |
public List<Map<String, String>> resolve(RouteContext context) { |
130 | 0 | ResolverConfig config = parseResolverConfig(); |
131 | 0 | Document xmlContent = context.getDocumentContent().getDocument(); |
132 | 0 | XPath xPath = XPathHelper.newXPath(); |
133 | 0 | boolean isCompoundMap = config.getExpressionMap().size() > 1; |
134 | |
try { |
135 | 0 | List<Map<String, String>> maps = new ArrayList<Map<String, String>>(); |
136 | 0 | NodeList baseElements = (NodeList)xPath.evaluate(config.getBaseXPathExpression(), xmlContent, XPathConstants.NODESET); |
137 | 0 | if (LOG.isDebugEnabled()) { |
138 | 0 | LOG.debug("Found " + baseElements.getLength() + " baseElements to parse for Map<String, String>s using document XML:" + XmlJotter.jotDocument(xmlContent)); |
139 | |
} |
140 | 0 | for (int index = 0; index < baseElements.getLength(); index++) { |
141 | 0 | Node baseNode = baseElements.item(index); |
142 | 0 | if (isCompoundMap) { |
143 | 0 | handleCompoundMap(baseNode, maps, config, xPath); |
144 | |
} else { |
145 | 0 | handleSimpleMap(baseNode, maps, config, xPath); |
146 | |
} |
147 | |
} |
148 | 0 | return maps; |
149 | 0 | } catch (XPathExpressionException e) { |
150 | 0 | throw new RiceRuntimeException("Encountered an issue executing XPath.", e); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
protected void handleCompoundMap(Node baseNode, List<Map<String, String>> maps, ResolverConfig config, XPath xPath) throws XPathExpressionException { |
155 | 0 | Map<String, String> map = new HashMap<String, String>(); |
156 | 0 | for (String attributeName : config.getExpressionMap().keySet()) { |
157 | 0 | String xPathExpression = config.getExpressionMap().get(attributeName); |
158 | 0 | NodeList attributes = (NodeList)xPath.evaluate(xPathExpression, baseNode, XPathConstants.NODESET); |
159 | 0 | if (attributes.getLength() > 1) { |
160 | 0 | throw new RiceRuntimeException("Found more than more XPath result for an attribute in a compound attribute set for attribute: " + attributeName + " with expression " + xPathExpression); |
161 | 0 | } else if (attributes.getLength() != 0) { |
162 | 0 | String attributeValue = ((Element)attributes.item(0)).getTextContent(); |
163 | 0 | if (LOG.isDebugEnabled()) { |
164 | 0 | LOG.debug("Adding values to compound Map<String, String>: " + attributeName + "::" + attributeValue); |
165 | |
} |
166 | 0 | map.put(attributeName, attributeValue); |
167 | |
} |
168 | 0 | } |
169 | 0 | maps.add(map); |
170 | 0 | } |
171 | |
|
172 | |
protected void handleSimpleMap(Node baseNode, List<Map<String, String>> maps, ResolverConfig config, XPath xPath) throws XPathExpressionException { |
173 | 0 | String attributeName = config.getExpressionMap().keySet().iterator().next(); |
174 | 0 | String xPathExpression = config.getExpressionMap().get(attributeName); |
175 | 0 | NodeList attributes = (NodeList)xPath.evaluate(xPathExpression, baseNode, XPathConstants.NODESET); |
176 | 0 | for (int index = 0; index < attributes.getLength(); index++) { |
177 | 0 | Element attributeElement = (Element)attributes.item(index); |
178 | 0 | Map<String, String> map = new HashMap<String, String>(); |
179 | 0 | String attributeValue = attributeElement.getTextContent(); |
180 | 0 | if (LOG.isDebugEnabled()) { |
181 | 0 | LOG.debug("Adding values to simple Map<String, String>: " + attributeName + "::" + attributeValue); |
182 | |
} |
183 | 0 | map.put(attributeName, attributeValue); |
184 | 0 | maps.add(map); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
public void setXmlConfigData (String xmlConfigData) { |
189 | 0 | this.xmlConfigData = xmlConfigData; |
190 | 0 | } |
191 | |
|
192 | |
protected ResolverConfig parseResolverConfig() { |
193 | 0 | if (xmlConfigData == null) { |
194 | 0 | throw new RiceRuntimeException("Failed to locate a RuleAttribute for the given XPathQualifierResolver"); |
195 | |
} |
196 | |
try { |
197 | 0 | ResolverConfig resolverConfig = new ResolverConfig(); |
198 | 0 | String xmlConfig = xmlConfigData; |
199 | 0 | XPath xPath = XPathHelper.newXPath(); |
200 | 0 | String baseExpression = xPath.evaluate("//resolverConfig/baseXPathExpression", new InputSource(new StringReader(xmlConfig))); |
201 | 0 | if (!StringUtils.isEmpty(baseExpression)) { |
202 | 0 | resolverConfig.setBaseXPathExpression(baseExpression); |
203 | |
} |
204 | 0 | NodeList qualifiers = (NodeList)xPath.evaluate("//resolverConfig/qualifier", new InputSource(new StringReader(xmlConfig)), XPathConstants.NODESET); |
205 | 0 | if (qualifiers == null || qualifiers.getLength() == 0) { |
206 | 0 | throw new RiceRuntimeException("Invalid qualifier resolver configuration. Must contain at least one qualifier!"); |
207 | |
} |
208 | 0 | for (int index = 0; index < qualifiers.getLength(); index++) { |
209 | 0 | Element qualifierElement = (Element)qualifiers.item(index); |
210 | 0 | String name = qualifierElement.getAttribute("name"); |
211 | 0 | NodeList expressions = qualifierElement.getElementsByTagName("xPathExpression"); |
212 | 0 | if (expressions.getLength() != 1) { |
213 | 0 | throw new RiceRuntimeException("There should only be a single xPathExpression per qualifier"); |
214 | |
} |
215 | 0 | Element expressionElement = (Element)expressions.item(0); |
216 | 0 | resolverConfig.getExpressionMap().put(name, expressionElement.getTextContent()); |
217 | |
} |
218 | 0 | if (LOG.isDebugEnabled()) { |
219 | 0 | LOG.debug("Using Resolver Config Settings: " + resolverConfig.toString()); |
220 | |
} |
221 | 0 | return resolverConfig; |
222 | 0 | } catch (XPathExpressionException e) { |
223 | 0 | throw new RiceRuntimeException("Encountered an error parsing resolver config.", e); |
224 | |
} |
225 | |
} |
226 | |
|
227 | 0 | class ResolverConfig { |
228 | 0 | private String baseXPathExpression = "/"; |
229 | 0 | private Map<String, String> expressionMap = new HashMap<String, String>(); |
230 | |
public String getBaseXPathExpression() { |
231 | 0 | return this.baseXPathExpression; |
232 | |
} |
233 | |
public void setBaseXPathExpression(String baseXPathExpression) { |
234 | 0 | this.baseXPathExpression = baseXPathExpression; |
235 | 0 | } |
236 | |
public Map<String, String> getExpressionMap() { |
237 | 0 | return this.expressionMap; |
238 | |
} |
239 | |
public void setExpressionMap(Map<String, String> expressionMap) { |
240 | 0 | this.expressionMap = expressionMap; |
241 | 0 | } |
242 | |
@Override |
243 | |
public String toString() { |
244 | 0 | StringBuffer sb = new StringBuffer(); |
245 | 0 | sb.append( '\n' ); |
246 | 0 | sb.append("ResolverConfig Parameters\n"); |
247 | 0 | sb.append( " baseXPathExpression: " + baseXPathExpression + "\n" ); |
248 | 0 | sb.append( " expressionMap:\n" ); |
249 | 0 | for (Map.Entry<String, String> entry : expressionMap.entrySet()) { |
250 | 0 | sb.append( " " + entry.getKey() + ": " + entry.getValue() + "\n" ); |
251 | |
} |
252 | 0 | return sb.toString(); |
253 | |
} |
254 | |
} |
255 | |
|
256 | |
} |