| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.xml; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.kuali.rice.core.api.impex.xml.XmlConstants; |
| 21 | |
import org.kuali.rice.core.api.util.xml.XmlException; |
| 22 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
| 23 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
| 24 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
| 25 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 26 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 27 | |
import org.w3c.dom.Element; |
| 28 | |
import org.w3c.dom.Node; |
| 29 | |
import org.w3c.dom.NodeList; |
| 30 | |
import org.xml.sax.InputSource; |
| 31 | |
|
| 32 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 33 | |
import javax.xml.xpath.XPath; |
| 34 | |
import javax.xml.xpath.XPathConstants; |
| 35 | |
import javax.xml.xpath.XPathExpressionException; |
| 36 | |
import java.io.IOException; |
| 37 | |
import java.io.InputStream; |
| 38 | |
import java.util.ArrayList; |
| 39 | |
import java.util.Iterator; |
| 40 | |
import java.util.List; |
| 41 | |
|
| 42 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.APPLICATION_ID; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | public class RuleAttributeXmlParser { |
| 53 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeXmlParser.class); |
| 54 | |
|
| 55 | |
|
| 56 | |
private static final String XPATH_RULE_ATTRIBUTES = "//" + XmlConstants.RULE_ATTRIBUTES + "/" + XmlConstants.RULE_ATTRIBUTE; |
| 57 | |
private static final String NAME = "name"; |
| 58 | |
private static final String CLASS_NAME = "className"; |
| 59 | |
private static final String LABEL = "label"; |
| 60 | |
private static final String DESCRIPTION = "description"; |
| 61 | |
private static final String TYPE = "type"; |
| 62 | |
private static final String CONFIG = "configuration"; |
| 63 | |
|
| 64 | |
public List parseRuleAttributes(InputStream input) throws IOException, XmlException { |
| 65 | |
try { |
| 66 | 0 | Element root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(input)).getDocumentElement(); |
| 67 | 0 | return parseRuleAttributes(root); |
| 68 | 0 | } catch (Exception e) { |
| 69 | 0 | throw new XmlException("error parsing xml data", e); |
| 70 | |
} |
| 71 | |
} |
| 72 | |
|
| 73 | |
public List parseRuleAttributes(Element element) throws XmlException { |
| 74 | 0 | List ruleAttributes = new ArrayList(); |
| 75 | |
try { |
| 76 | 0 | XPath xpath = XPathHelper.newXPath(); |
| 77 | 0 | NodeList nodeList = (NodeList)xpath.evaluate(XPATH_RULE_ATTRIBUTES, element, XPathConstants.NODESET); |
| 78 | 0 | for (int i = 0; i < nodeList.getLength(); i++) { |
| 79 | 0 | Node ruleAttributeNode = nodeList.item(i); |
| 80 | 0 | ruleAttributes.add(parseRuleAttribute(ruleAttributeNode)); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | for (Iterator iterator = ruleAttributes.iterator(); iterator.hasNext();) { |
| 84 | 0 | RuleAttribute ruleAttribute = (RuleAttribute) iterator.next(); |
| 85 | |
try { |
| 86 | 0 | RuleAttribute existingAttribute = KEWServiceLocator.getRuleAttributeService().findByName(ruleAttribute.getName()); |
| 87 | 0 | if (existingAttribute != null) { |
| 88 | 0 | ruleAttribute.setRuleAttributeId(existingAttribute.getRuleAttributeId()); |
| 89 | 0 | ruleAttribute.setVersionNumber(existingAttribute.getVersionNumber()); |
| 90 | |
} |
| 91 | 0 | KEWServiceLocator.getRuleAttributeService().save(ruleAttribute); |
| 92 | 0 | } catch (Exception e) { |
| 93 | 0 | LOG.error("Error saving rule attribute entered by XML", e); |
| 94 | 0 | } |
| 95 | 0 | } |
| 96 | 0 | } catch (XPathExpressionException e1) { |
| 97 | 0 | throw new XmlException("Could not find a rule attribute.", e1); |
| 98 | 0 | } |
| 99 | 0 | return ruleAttributes; |
| 100 | |
} |
| 101 | |
|
| 102 | |
private RuleAttribute parseRuleAttribute(Node ruleAttributeNode) throws XmlException { |
| 103 | 0 | String name = ""; |
| 104 | 0 | String className = ""; |
| 105 | 0 | String label = ""; |
| 106 | 0 | String description = ""; |
| 107 | 0 | String type = ""; |
| 108 | 0 | String applicationId = null; |
| 109 | 0 | Node xmlConfig = null; |
| 110 | 0 | for (int i = 0; i < ruleAttributeNode.getChildNodes().getLength(); i++) { |
| 111 | 0 | Node childNode = ruleAttributeNode.getChildNodes().item(i); |
| 112 | 0 | if(NAME.equals(childNode.getNodeName())){ |
| 113 | 0 | name = childNode.getFirstChild().getNodeValue(); |
| 114 | 0 | } else if(CLASS_NAME.equals(childNode.getNodeName())){ |
| 115 | 0 | className = childNode.getFirstChild().getNodeValue(); |
| 116 | 0 | } else if(LABEL.equals(childNode.getNodeName())){ |
| 117 | 0 | label = childNode.getFirstChild().getNodeValue(); |
| 118 | 0 | } else if(DESCRIPTION.equals(childNode.getNodeName())){ |
| 119 | 0 | description = childNode.getFirstChild().getNodeValue(); |
| 120 | 0 | } else if(TYPE.equals(childNode.getNodeName())){ |
| 121 | 0 | type = childNode.getFirstChild().getNodeValue(); |
| 122 | 0 | } else if(XmlConstants.ROUTING_CONFIG.equals(childNode.getNodeName()) || XmlConstants.SEARCHING_CONFIG.equals(childNode.getNodeName()) || |
| 123 | |
XmlConstants.SEARCH_RESULT_CONFIG.equals(childNode.getNodeName()) || XmlConstants.RESOLVER_CONFIG.equals(childNode.getNodeName()) || |
| 124 | |
CONFIG.equals(childNode.getNodeName())){ |
| 125 | 0 | xmlConfig = childNode; |
| 126 | 0 | } else if (XmlConstants.SERVICE_NAMESPACE.equals(childNode.getNodeName())) { |
| 127 | 0 | applicationId = childNode.getFirstChild().getNodeValue(); |
| 128 | 0 | LOG.warn(XmlConstants.SERVICE_NAMESPACE + " element was set on rule attribute type XML but is deprecated and will be removed in a future version, please use " + APPLICATION_ID + " instead."); |
| 129 | 0 | } else if (XmlConstants.APPLICATION_ID.equals(childNode.getNodeName())) { |
| 130 | 0 | applicationId = childNode.getFirstChild().getNodeValue(); |
| 131 | |
} |
| 132 | |
} |
| 133 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(name)) { |
| 134 | 0 | throw new XmlException("RuleAttribute must have a name"); |
| 135 | |
} |
| 136 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(className)) { |
| 137 | 0 | throw new XmlException("RuleAttribute must have a className"); |
| 138 | |
} |
| 139 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(label)) { |
| 140 | 0 | LOG.warn("Label empty defaulting to name"); |
| 141 | 0 | label = name; |
| 142 | |
} |
| 143 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(type)) { |
| 144 | 0 | LOG.debug("No type specified, default to " + KEWConstants.RULE_ATTRIBUTE_TYPE); |
| 145 | 0 | type = KEWConstants.RULE_ATTRIBUTE_TYPE; |
| 146 | |
|
| 147 | |
} |
| 148 | 0 | RuleAttribute ruleAttribute = new RuleAttribute(); |
| 149 | 0 | ruleAttribute.setName(name.trim()); |
| 150 | 0 | ruleAttribute.setClassName(className.trim()); |
| 151 | 0 | ruleAttribute.setType(type.trim()); |
| 152 | 0 | ruleAttribute.setLabel(label.trim()); |
| 153 | |
|
| 154 | 0 | if (StringUtils.isEmpty(description)) { |
| 155 | 0 | description = label; |
| 156 | |
} |
| 157 | 0 | ruleAttribute.setDescription(description.trim()); |
| 158 | 0 | if (applicationId != null) |
| 159 | |
{ |
| 160 | 0 | applicationId = applicationId.trim(); |
| 161 | |
} |
| 162 | 0 | ruleAttribute.setApplicationId(applicationId); |
| 163 | |
|
| 164 | 0 | if(xmlConfig != null){ |
| 165 | 0 | ruleAttribute.setXmlConfigData(XmlJotter.jotNode(xmlConfig)); |
| 166 | |
} else { |
| 167 | 0 | if(KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(type)){ |
| 168 | 0 | throw new XmlException("A routing config must be present to be of type: "+type); |
| 169 | 0 | } else if(KEWConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE.equals(type)){ |
| 170 | 0 | throw new XmlException("A searching config must be present to be of type: "+type); |
| 171 | 0 | } else if(KEWConstants.SEARCH_RESULT_XML_PROCESSOR_ATTRIBUTE_TYPE.equals(type)){ |
| 172 | 0 | throw new XmlException("A searching config must be present to be of type: "+type); |
| 173 | |
} |
| 174 | |
} |
| 175 | 0 | return ruleAttribute; |
| 176 | |
} |
| 177 | |
} |