1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.workflow.attribute; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.commons.logging.Log; |
20 | |
import org.apache.commons.logging.LogFactory; |
21 | |
import org.kuali.rice.core.api.util.KeyValue; |
22 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
23 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
24 | |
import org.kuali.rice.krad.keyvalues.KeyValuesFinder; |
25 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
26 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
27 | |
import org.w3c.dom.Element; |
28 | |
import org.w3c.dom.NamedNodeMap; |
29 | |
import org.w3c.dom.Node; |
30 | |
import org.w3c.dom.NodeList; |
31 | |
|
32 | |
import javax.xml.transform.Result; |
33 | |
import javax.xml.transform.Source; |
34 | |
import javax.xml.transform.TransformerFactory; |
35 | |
import javax.xml.transform.dom.DOMSource; |
36 | |
import javax.xml.transform.stream.StreamResult; |
37 | |
import javax.xml.xpath.XPath; |
38 | |
import javax.xml.xpath.XPathConstants; |
39 | |
import javax.xml.xpath.XPathExpressionException; |
40 | |
import java.io.StringWriter; |
41 | |
import java.util.ArrayList; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
import java.util.regex.Matcher; |
45 | |
import java.util.regex.Pattern; |
46 | |
|
47 | |
|
48 | 0 | public class KualiXmlAttributeHelper { |
49 | 0 | private static Log LOG = LogFactory.getLog(KualiXmlAttributeHelper.class); |
50 | 0 | private static XPath xpath = XPathHelper.newXPath(); |
51 | |
private static final String testVal = "\'/[^\']*\'"; |
52 | |
private static final String testVal2 = "/[^/]+/" + "*"; |
53 | |
private static final String cleanVal = "[^/\']+"; |
54 | |
private static final String ruledataVal = "ruledata[^\']*\'([^\']*)"; |
55 | |
|
56 | |
|
57 | |
|
58 | 0 | private static final Pattern xPathPattern = Pattern.compile(testVal); |
59 | 0 | private static final Pattern termPattern = Pattern.compile(testVal2); |
60 | 0 | private static final Pattern cleanPattern = Pattern.compile(cleanVal); |
61 | 0 | private static final Pattern targetPattern = Pattern.compile(ruledataVal); |
62 | |
|
63 | |
public static final String ATTRIBUTE_LABEL_BO_REFERENCE_PREFIX = "kuali_dd_label("; |
64 | |
public static final String ATTRIBUTE_LABEL_BO_REFERENCE_SUFFIX = ")"; |
65 | |
public static final String ATTRIBUTE_SHORT_LABEL_BO_REFERENCE_PREFIX = "kuali_dd_short_label("; |
66 | |
public static final String ATTRIBUTE_SHORT_LABEL_BO_REFERENCE_SUFFIX = ")"; |
67 | |
private static final String KUALI_VALUES_FINDER_REFERENCE_PREFIX = "kuali_values_finder_class("; |
68 | |
private static final String KUALI_VALUES_FINDER_REFERENCE_SUFFIX = ")"; |
69 | |
public static final String notFound = "Label Not Found"; |
70 | |
|
71 | 0 | private String lastXPath = ""; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Element processConfigXML(Element root) { |
81 | 0 | return this.processConfigXML(root, null); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public Element processConfigXML(Element root, String[] xpathExpressionElements) { |
92 | |
|
93 | 0 | NodeList fields = root.getElementsByTagName("fieldDef"); |
94 | 0 | Element theTag = null; |
95 | 0 | String docContent = ""; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 0 | org.w3c.dom.Document xmlDoc = null; |
112 | 0 | if (!xmlDocumentContentExists(root)) { |
113 | 0 | fields = root.getElementsByTagName("fieldDef"); |
114 | 0 | xmlDoc = root.getOwnerDocument(); |
115 | |
} |
116 | 0 | for (int i = 0; i < fields.getLength(); i++) { |
117 | 0 | String name = null; |
118 | 0 | if (!xmlDocumentContentExists(root)) { |
119 | 0 | theTag = (Element) fields.item(i); |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | 0 | List<String> xPathTerms = getXPathTerms(theTag); |
128 | 0 | if (xPathTerms.size() != 0) { |
129 | 0 | Node iterNode = xmlDoc.createElement("xmlDocumentContent"); |
130 | |
|
131 | |
|
132 | 0 | xmlDoc.normalize(); |
133 | |
|
134 | 0 | iterNode.normalize(); |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 0 | for (int j = 0; j < xPathTerms.size(); j++) { |
141 | |
|
142 | 0 | iterNode.appendChild(xmlDoc.createElement(xPathTerms.get(j))); |
143 | 0 | xmlDoc.normalize(); |
144 | |
|
145 | 0 | iterNode = iterNode.getFirstChild(); |
146 | 0 | iterNode.normalize(); |
147 | |
|
148 | |
} |
149 | 0 | iterNode.setTextContent("%" + xPathTerms.get(xPathTerms.size() - 1) + "%"); |
150 | 0 | root.appendChild(iterNode); |
151 | |
} |
152 | |
} |
153 | 0 | theTag = (Element) fields.item(i); |
154 | |
|
155 | 0 | NodeList displayTagElements = theTag.getElementsByTagName("display"); |
156 | 0 | if (displayTagElements.getLength() == 1) { |
157 | 0 | Element displayTag = (Element) displayTagElements.item(0); |
158 | 0 | List valuesElementsToAdd = new ArrayList(); |
159 | 0 | for (int w = 0; w < displayTag.getChildNodes().getLength(); w++) { |
160 | 0 | Node displayTagChildNode = (Node) displayTag.getChildNodes().item(w); |
161 | 0 | if ((displayTagChildNode != null) && ("values".equals(displayTagChildNode.getNodeName()))) { |
162 | 0 | if (displayTagChildNode.getChildNodes().getLength() > 0) { |
163 | 0 | String valuesNodeText = displayTagChildNode.getFirstChild().getNodeValue(); |
164 | 0 | String potentialClassName = getPotentialKualiClassName(valuesNodeText, KUALI_VALUES_FINDER_REFERENCE_PREFIX, KUALI_VALUES_FINDER_REFERENCE_SUFFIX); |
165 | 0 | if (StringUtils.isNotBlank(potentialClassName)) { |
166 | |
try { |
167 | 0 | Class finderClass = Class.forName((String) potentialClassName); |
168 | 0 | KeyValuesFinder finder = (KeyValuesFinder) finderClass.newInstance(); |
169 | 0 | NamedNodeMap valuesNodeAttributes = displayTagChildNode.getAttributes(); |
170 | 0 | Node potentialSelectedAttribute = (valuesNodeAttributes != null) ? valuesNodeAttributes.getNamedItem("selected") : null; |
171 | 0 | for (Iterator iter = finder.getKeyValues().iterator(); iter.hasNext();) { |
172 | 0 | KeyValue keyValue = (KeyValue) iter.next(); |
173 | 0 | Element newValuesElement = root.getOwnerDocument().createElement("values"); |
174 | 0 | newValuesElement.appendChild(root.getOwnerDocument().createTextNode(keyValue.getKey())); |
175 | |
|
176 | 0 | newValuesElement.setAttribute("title", keyValue.getValue()); |
177 | 0 | if (potentialSelectedAttribute != null) { |
178 | 0 | newValuesElement.setAttribute("selected", potentialSelectedAttribute.getNodeValue()); |
179 | |
} |
180 | 0 | valuesElementsToAdd.add(newValuesElement); |
181 | 0 | } |
182 | 0 | } catch (ClassNotFoundException cnfe) { |
183 | 0 | String errorMessage = "Caught an exception trying to find class '" + potentialClassName + "'"; |
184 | 0 | LOG.error(errorMessage, cnfe); |
185 | 0 | throw new RuntimeException(errorMessage, cnfe); |
186 | 0 | } catch (InstantiationException ie) { |
187 | 0 | String errorMessage = "Caught an exception trying to instantiate class '" + potentialClassName + "'"; |
188 | 0 | LOG.error(errorMessage, ie); |
189 | 0 | throw new RuntimeException(errorMessage, ie); |
190 | 0 | } catch (IllegalAccessException iae) { |
191 | 0 | String errorMessage = "Caught an access exception trying to instantiate class '" + potentialClassName + "'"; |
192 | 0 | LOG.error(errorMessage, iae); |
193 | 0 | throw new RuntimeException(errorMessage, iae); |
194 | 0 | } |
195 | |
} else { |
196 | 0 | valuesElementsToAdd.add(displayTagChildNode.cloneNode(true)); |
197 | |
} |
198 | 0 | displayTag.removeChild(displayTagChildNode); |
199 | |
} |
200 | |
} |
201 | |
} |
202 | 0 | for (Iterator iter = valuesElementsToAdd.iterator(); iter.hasNext();) { |
203 | 0 | Element valuesElementToAdd = (Element) iter.next(); |
204 | 0 | displayTag.appendChild(valuesElementToAdd); |
205 | 0 | } |
206 | |
} |
207 | 0 | if ((xpathExpressionElements != null) && (xpathExpressionElements.length > 0)) { |
208 | 0 | NodeList fieldEvaluationElements = theTag.getElementsByTagName("fieldEvaluation"); |
209 | 0 | if (fieldEvaluationElements.getLength() == 1) { |
210 | 0 | Element fieldEvaluationTag = (Element) fieldEvaluationElements.item(0); |
211 | 0 | List tagsToAdd = new ArrayList(); |
212 | 0 | for (int w = 0; w < fieldEvaluationTag.getChildNodes().getLength(); w++) { |
213 | 0 | Node fieldEvaluationChildNode = (Node) fieldEvaluationTag.getChildNodes().item(w); |
214 | 0 | Element newTagToAdd = null; |
215 | 0 | if ((fieldEvaluationChildNode != null) && ("xpathexpression".equals(fieldEvaluationChildNode.getNodeName()))) { |
216 | 0 | newTagToAdd = root.getOwnerDocument().createElement("xpathexpression"); |
217 | 0 | newTagToAdd.appendChild(root.getOwnerDocument().createTextNode(generateNewXpathExpression(fieldEvaluationChildNode.getFirstChild().getNodeValue(), xpathExpressionElements))); |
218 | 0 | tagsToAdd.add(newTagToAdd); |
219 | 0 | fieldEvaluationTag.removeChild(fieldEvaluationChildNode); |
220 | |
} |
221 | |
} |
222 | 0 | for (Iterator iter = tagsToAdd.iterator(); iter.hasNext();) { |
223 | 0 | Element elementToAdd = (Element) iter.next(); |
224 | 0 | fieldEvaluationTag.appendChild(elementToAdd); |
225 | 0 | } |
226 | |
} |
227 | |
} |
228 | 0 | theTag.setAttribute("title", getBusinessObjectTitle(theTag)); |
229 | |
|
230 | |
} |
231 | 0 | if (LOG.isDebugEnabled()) { |
232 | 0 | LOG.debug(XmlJotter.jotNode(root)); |
233 | 0 | StringWriter xmlBuffer = new StringWriter(); |
234 | |
try { |
235 | |
|
236 | 0 | root.normalize(); |
237 | 0 | Source source = new DOMSource(root); |
238 | 0 | Result result = new StreamResult(xmlBuffer); |
239 | 0 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
240 | 0 | } catch (Exception e) { |
241 | 0 | LOG.debug(" Exception when printing debug XML output " + e); |
242 | 0 | } |
243 | 0 | LOG.debug(xmlBuffer.getBuffer()); |
244 | |
} |
245 | |
|
246 | 0 | return root; |
247 | |
} |
248 | |
|
249 | |
private String generateNewXpathExpression(String currentXpathExpression, String[] newXpathExpressionElements) { |
250 | 0 | StringBuffer returnableString = new StringBuffer(); |
251 | 0 | for (int i = 0; i < newXpathExpressionElements.length; i++) { |
252 | 0 | String newXpathElement = newXpathExpressionElements[i]; |
253 | 0 | returnableString.append(newXpathElement); |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | 0 | if (((i + 1) != newXpathExpressionElements.length) || (newXpathExpressionElements.length == 1)) { |
261 | 0 | returnableString.append(currentXpathExpression); |
262 | |
} |
263 | |
} |
264 | 0 | return returnableString.toString(); |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private String getXPathText(Element root) { |
274 | |
try { |
275 | 0 | String textContent = null; |
276 | 0 | Node node = (Node) xpath.evaluate(".//xpathexpression", root, XPathConstants.NODE); |
277 | 0 | if (node != null) { |
278 | 0 | textContent = node.getTextContent(); |
279 | |
} |
280 | 0 | return textContent; |
281 | 0 | } catch (XPathExpressionException e) { |
282 | 0 | LOG.error("No XPath expression text found in element xpathexpression of configXML for document. " + e); |
283 | 0 | return null; |
284 | |
|
285 | |
} |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
private boolean xmlDocumentContentExists(Element root) { |
295 | |
try { |
296 | 0 | if (((NodeList) xpath.evaluate("//xmlDocumentContent", root, XPathConstants.NODESET)).getLength() == 0) { |
297 | 0 | return false; |
298 | |
} |
299 | 0 | } catch (XPathExpressionException e) { |
300 | 0 | LOG.error("Error parsing xmlDocumentConfig. " + e); |
301 | 0 | return false; |
302 | 0 | } |
303 | 0 | return true; |
304 | |
} |
305 | |
|
306 | |
public static String getPotentialKualiClassName(String testString, String prefixIndicator, String suffixIndicator) { |
307 | 0 | if ((StringUtils.isNotBlank(testString)) && (testString.startsWith(prefixIndicator)) && (testString.endsWith(suffixIndicator))) { |
308 | 0 | return testString.substring(prefixIndicator.length(), testString.lastIndexOf(suffixIndicator)); |
309 | |
} |
310 | 0 | return null; |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
private String getBusinessObjectTitle(Element root) { |
334 | 0 | String businessObjectName = null; |
335 | 0 | String businessObjectText = root.getAttribute("title"); |
336 | 0 | String potentialClassNameLongLabel = getPotentialKualiClassName(businessObjectText, ATTRIBUTE_LABEL_BO_REFERENCE_PREFIX, ATTRIBUTE_LABEL_BO_REFERENCE_SUFFIX); |
337 | 0 | String potentialClassNameShortLabel = getPotentialKualiClassName(businessObjectText, ATTRIBUTE_SHORT_LABEL_BO_REFERENCE_PREFIX, ATTRIBUTE_SHORT_LABEL_BO_REFERENCE_SUFFIX); |
338 | |
|
339 | 0 | boolean requestedShortLabel = false; |
340 | |
|
341 | 0 | if (StringUtils.isNotBlank(potentialClassNameLongLabel)) { |
342 | 0 | businessObjectName = potentialClassNameLongLabel; |
343 | 0 | } else if (StringUtils.isNotBlank(potentialClassNameShortLabel)) { |
344 | 0 | businessObjectName = potentialClassNameShortLabel; |
345 | 0 | requestedShortLabel = true; |
346 | |
} |
347 | 0 | if (StringUtils.isNotBlank(businessObjectName)) { |
348 | 0 | DataDictionaryService DDService = KRADServiceLocatorWeb.getDataDictionaryService(); |
349 | |
|
350 | 0 | String title = null; |
351 | 0 | String targetVal = lastXPath; |
352 | |
|
353 | 0 | if (LOG.isErrorEnabled()) { |
354 | 0 | LOG.debug("Finding title in BO=" + businessObjectName + " ObjectName=" + targetVal); |
355 | |
} |
356 | |
|
357 | 0 | if (StringUtils.isNotBlank(targetVal)) { |
358 | |
|
359 | 0 | if (requestedShortLabel) { |
360 | 0 | title = DDService.getAttributeShortLabel(businessObjectName, targetVal); |
361 | |
} else { |
362 | 0 | title = DDService.getAttributeLabel(businessObjectName, targetVal); |
363 | |
} |
364 | 0 | if (StringUtils.isNotBlank(title)) { |
365 | 0 | return title; |
366 | |
} |
367 | |
} |
368 | |
|
369 | 0 | targetVal = getRuleData(root); |
370 | 0 | if (LOG.isErrorEnabled()) { |
371 | 0 | LOG.debug("Finding title in BO=" + businessObjectName + " ObjectName=" + targetVal); |
372 | |
} |
373 | 0 | if (StringUtils.isNotBlank(targetVal)) { |
374 | 0 | title = DDService.getAttributeLabel(businessObjectName, targetVal); |
375 | 0 | if (StringUtils.isNotBlank(title)) { |
376 | 0 | return title; |
377 | |
} |
378 | |
} |
379 | |
|
380 | |
|
381 | 0 | targetVal = root.getAttribute("name"); |
382 | 0 | if (LOG.isErrorEnabled()) { |
383 | 0 | LOG.debug("Finding title in BO=" + businessObjectName + " ObjectName=" + targetVal); |
384 | |
} |
385 | 0 | title = DDService.getAttributeLabel(businessObjectName, targetVal); |
386 | |
|
387 | 0 | if (StringUtils.isNotBlank(title)) { |
388 | 0 | return title; |
389 | |
} |
390 | 0 | } |
391 | |
|
392 | 0 | else if ((StringUtils.isNotBlank(businessObjectText)) && (StringUtils.isBlank(businessObjectName))) { |
393 | 0 | return businessObjectText; |
394 | |
} |
395 | 0 | return notFound; |
396 | |
|
397 | |
} |
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
private String getRuleData(Element root) { |
406 | 0 | String xPathRuleTarget = getXPathText(root); |
407 | |
|
408 | |
|
409 | 0 | if (StringUtils.isNotBlank(xPathRuleTarget)) { |
410 | 0 | Matcher ruleTarget = targetPattern.matcher(xPathRuleTarget); |
411 | 0 | if (ruleTarget.find()) { |
412 | 0 | xPathRuleTarget = ruleTarget.group(1); |
413 | |
} |
414 | |
} |
415 | 0 | return xPathRuleTarget; |
416 | |
} |
417 | |
|
418 | |
private List<String> getXPathTerms(Element myTag) { |
419 | |
|
420 | |
Matcher xPathTarget; |
421 | |
String firstMatch; |
422 | 0 | List<String> xPathTerms = new ArrayList(); |
423 | 0 | String allText = getXPathText(myTag); |
424 | 0 | if (StringUtils.isNotBlank(allText)) { |
425 | 0 | xPathTarget = xPathPattern.matcher(allText); |
426 | |
Matcher termTarget; |
427 | |
Matcher cleanTarget; |
428 | 0 | int theEnd = 0; |
429 | |
|
430 | 0 | xPathTarget.find(theEnd); |
431 | 0 | theEnd = xPathTarget.end() - 1; |
432 | 0 | firstMatch = xPathTarget.group(); |
433 | |
|
434 | |
|
435 | 0 | termTarget = termPattern.matcher(firstMatch); |
436 | 0 | int theEnd2 = 0; |
437 | 0 | while (termTarget.find(theEnd2)) { |
438 | 0 | theEnd2 = termTarget.end() - 1; |
439 | 0 | cleanTarget = cleanPattern.matcher(termTarget.group()); |
440 | 0 | cleanTarget.find(); |
441 | 0 | lastXPath = cleanTarget.group(); |
442 | 0 | xPathTerms.add(lastXPath); |
443 | |
|
444 | |
} |
445 | |
} |
446 | 0 | return xPathTerms; |
447 | |
} |
448 | |
|
449 | |
private String getLastXPath(Element root) { |
450 | 0 | List<String> tempList = getXPathTerms(root); |
451 | 0 | return tempList.get(tempList.size()); |
452 | |
} |
453 | |
} |