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