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