| 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 java.io.InputStream; |
| 20 |
|
import java.util.ArrayList; |
| 21 |
|
import java.util.Collection; |
| 22 |
|
import java.util.Iterator; |
| 23 |
|
|
| 24 |
|
import javax.xml.parsers.DocumentBuilder; |
| 25 |
|
import javax.xml.xpath.XPath; |
| 26 |
|
import javax.xml.xpath.XPathConstants; |
| 27 |
|
import javax.xml.xpath.XPathExpressionException; |
| 28 |
|
import javax.xml.xpath.XPathFactory; |
| 29 |
|
|
| 30 |
|
import org.apache.log4j.Logger; |
| 31 |
|
import org.kuali.rice.core.api.impex.xml.XmlIngestionException; |
| 32 |
|
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
| 33 |
|
import org.kuali.rice.core.api.style.Style; |
| 34 |
|
import org.kuali.rice.core.api.style.StyleService; |
| 35 |
|
import org.kuali.rice.core.util.XmlJotter; |
| 36 |
|
import org.kuali.rice.core.xml.XmlException; |
| 37 |
|
import org.kuali.rice.edl.impl.EDLXmlUtils; |
| 38 |
|
import org.kuali.rice.edl.impl.bo.EDocLiteAssociation; |
| 39 |
|
import org.kuali.rice.edl.impl.bo.EDocLiteDefinition; |
| 40 |
|
import org.kuali.rice.edl.impl.service.EDocLiteService; |
| 41 |
|
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
| 42 |
|
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 43 |
|
import org.w3c.dom.Document; |
| 44 |
|
import org.w3c.dom.Element; |
| 45 |
|
import org.w3c.dom.Node; |
| 46 |
|
import org.w3c.dom.NodeList; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@author |
| 53 |
|
|
|
|
|
| 0% |
Uncovered Elements: 144 (144) |
Complexity: 38 |
Complexity Density: 0.4 |
|
| 54 |
|
public class EDocLiteXmlParser { |
| 55 |
|
|
| 56 |
|
private static final Logger LOG = Logger.getLogger(EDocLiteXmlParser.class); |
| 57 |
|
|
|
|
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 9 |
Complexity Density: 0.31 |
|
| 58 |
0
|
public static void loadXml(InputStream inputStream, String principalId) {... |
| 59 |
0
|
DocumentBuilder db = EDLXmlUtils.getDocumentBuilder(); |
| 60 |
0
|
XPath xpath = XPathFactory.newInstance().newXPath(); |
| 61 |
0
|
Document doc; |
| 62 |
|
|
| 63 |
|
|
| 64 |
0
|
try { |
| 65 |
0
|
doc = db.parse(inputStream); |
| 66 |
|
} catch (Exception e) { |
| 67 |
0
|
throw generateException("Error parsing EDocLite XML file", e); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
0
|
NodeList edls; |
| 76 |
0
|
try { |
| 77 |
0
|
edls = (NodeList) xpath.evaluate("//edoclite", doc.getFirstChild(), XPathConstants.NODESET); |
| 78 |
|
} catch (XPathExpressionException e) { |
| 79 |
0
|
throw generateException("Error evaluating XPath expression", e); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
0
|
for (int i = 0; i < edls.getLength(); i++) { |
| 83 |
0
|
Node edl = edls.item(i); |
| 84 |
0
|
NodeList children = edl.getChildNodes(); |
| 85 |
0
|
for (int j = 0; j < children.getLength(); j++) { |
| 86 |
0
|
Node node = children.item(j); |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
0
|
if (node.getNodeType() == Node.ELEMENT_NODE) { |
| 93 |
0
|
Element e = (Element) node; |
| 94 |
0
|
if ("style".equals(node.getNodeName())) { |
| 95 |
0
|
LOG.debug("Digesting EDocLiteStyle: " + e.getAttribute("name")); |
| 96 |
0
|
Style style = parseStyle(e); |
| 97 |
0
|
getStyleService().saveStyle(style); |
| 98 |
0
|
} else if ("edl".equals(node.getNodeName())) { |
| 99 |
0
|
LOG.debug("Digesting EDocLiteDefinition: " + e.getAttribute("name")); |
| 100 |
0
|
EDocLiteDefinition def = parseEDocLiteDefinition(e); |
| 101 |
0
|
getEDLService().saveEDocLiteDefinition(def); |
| 102 |
0
|
} else if ("association".equals(node.getNodeName())) { |
| 103 |
0
|
LOG.debug("Digesting EDocLiteAssociation: " + e.getAttribute("name")); |
| 104 |
0
|
EDocLiteAssociation assoc = parseEDocLiteAssociation(e); |
| 105 |
0
|
getEDLService().saveEDocLiteAssociation(assoc); |
| 106 |
|
} else { |
| 107 |
|
|
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
} |
| 116 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 117 |
0
|
private static XmlIngestionException generateException(String error, Throwable cause) {... |
| 118 |
0
|
throw new XmlIngestionException(error, cause); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
@param |
| 125 |
|
|
| 126 |
|
@return |
| 127 |
|
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 128 |
0
|
private static EDocLiteAssociation parseEDocLiteAssociation(Element e) {... |
| 129 |
0
|
String docType = EDLXmlUtils.getChildElementTextValue(e, "docType"); |
| 130 |
0
|
if (docType == null) { |
| 131 |
0
|
throw generateMissingChildException("association", "docType"); |
| 132 |
|
} |
| 133 |
0
|
EDocLiteAssociation assoc = new EDocLiteAssociation(); |
| 134 |
0
|
assoc.setEdlName(docType); |
| 135 |
0
|
assoc.setDefinition(EDLXmlUtils.getChildElementTextValue(e, "definition")); |
| 136 |
0
|
assoc.setStyle(EDLXmlUtils.getChildElementTextValue(e, "style")); |
| 137 |
0
|
assoc.setActiveInd(Boolean.valueOf(EDLXmlUtils.getChildElementTextValue(e, "active"))); |
| 138 |
0
|
return assoc; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
@param |
| 145 |
|
|
| 146 |
|
@return |
| 147 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 8 |
Complexity Density: 0.47 |
|
| 148 |
0
|
private static Style parseStyle(Element e) {... |
| 149 |
0
|
String name = e.getAttribute("name"); |
| 150 |
0
|
if (name == null || name.length() == 0) { |
| 151 |
0
|
throw generateMissingAttribException("style", "name"); |
| 152 |
|
} |
| 153 |
0
|
Style.Builder styleBuilder = Style.Builder.create(name); |
| 154 |
0
|
Element stylesheet = null; |
| 155 |
0
|
NodeList children = e.getChildNodes(); |
| 156 |
0
|
for (int i = 0; i < children.getLength(); i++) { |
| 157 |
0
|
Node child = children.item(i); |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
0
|
if (child.getNodeType() == Node.ELEMENT_NODE && "xsl:stylesheet".equals(child.getNodeName())) { |
| 162 |
0
|
stylesheet = (Element) child; |
| 163 |
0
|
break; |
| 164 |
|
} |
| 165 |
|
} |
| 166 |
0
|
if (stylesheet == null) { |
| 167 |
0
|
throw generateMissingChildException("style", "xsl:stylesheet"); |
| 168 |
|
} |
| 169 |
0
|
try { |
| 170 |
0
|
styleBuilder.setXmlContent(XmlJotter.jotNode(stylesheet, true)); |
| 171 |
|
} catch (XmlException te) { |
| 172 |
0
|
throw generateSerializationException("style", te); |
| 173 |
|
} |
| 174 |
0
|
return styleBuilder.build(); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
@param |
| 181 |
|
|
| 182 |
|
@return |
| 183 |
|
|
|
|
|
| 0% |
Uncovered Elements: 51 (51) |
Complexity: 13 |
Complexity Density: 0.37 |
|
| 184 |
0
|
private static EDocLiteDefinition parseEDocLiteDefinition(Element e) {... |
| 185 |
0
|
EDocLiteDefinition def = new EDocLiteDefinition(); |
| 186 |
0
|
String name = e.getAttribute("name"); |
| 187 |
0
|
if (name == null || name.length() == 0) { |
| 188 |
0
|
throw generateMissingAttribException(EDLXmlUtils.EDL_E, "name"); |
| 189 |
|
} |
| 190 |
0
|
def.setName(name); |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
0
|
XPath xpath = XPathFactory.newInstance().newXPath(); |
| 196 |
0
|
NodeList fields; |
| 197 |
0
|
try { |
| 198 |
0
|
fields = (NodeList) xpath.evaluate("fieldDef", e, XPathConstants.NODESET); |
| 199 |
|
} catch (XPathExpressionException xpee) { |
| 200 |
0
|
throw new XmlIngestionException("Invalid EDocLiteDefinition", xpee); |
| 201 |
|
} |
| 202 |
|
|
| 203 |
0
|
if (fields != null) { |
| 204 |
0
|
Collection invalidAttributes = new ArrayList(5); |
| 205 |
0
|
for (int i = 0; i < fields.getLength(); i++) { |
| 206 |
0
|
Node node = (Node) fields.item(i); |
| 207 |
|
|
| 208 |
0
|
if (node instanceof Element) { |
| 209 |
0
|
Element field = (Element) node; |
| 210 |
|
|
| 211 |
0
|
String fieldName = field.getAttribute("name"); |
| 212 |
0
|
String attribute = field.getAttribute("attributeName"); |
| 213 |
0
|
if (attribute != null && attribute.length() > 0) { |
| 214 |
0
|
RuleAttribute ruleAttrib = KEWServiceLocator.getRuleAttributeService().findByName(attribute); |
| 215 |
0
|
if (ruleAttrib == null) { |
| 216 |
0
|
LOG.error("Invalid attribute referenced in EDocLite definition: " + attribute); |
| 217 |
0
|
invalidAttributes.add("Attribute '" + attribute + "' referenced in field '" + fieldName + "' not found"); |
| 218 |
|
} |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
|
} |
| 222 |
0
|
if (invalidAttributes.size() > 0) { |
| 223 |
0
|
LOG.error("Invalid attributes referenced in EDocLite definition"); |
| 224 |
0
|
StringBuffer message = new StringBuffer("EDocLite definition contains references to non-existent attributes;\n"); |
| 225 |
0
|
Iterator it = invalidAttributes.iterator(); |
| 226 |
0
|
while (it.hasNext()) { |
| 227 |
0
|
message.append(it.next()); |
| 228 |
0
|
message.append("\n"); |
| 229 |
|
} |
| 230 |
0
|
throw new XmlIngestionException(message.toString()); |
| 231 |
|
} |
| 232 |
|
} |
| 233 |
|
|
| 234 |
0
|
try { |
| 235 |
0
|
def.setXmlContent(XmlJotter.jotNode(e, true)); |
| 236 |
|
} catch (XmlException te) { |
| 237 |
0
|
throw generateSerializationException(EDLXmlUtils.EDL_E, te); |
| 238 |
|
} |
| 239 |
0
|
return def; |
| 240 |
|
} |
| 241 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 242 |
0
|
private static XmlIngestionException generateMissingAttribException(String element, String attrib) {... |
| 243 |
0
|
return generateException("EDocLite '" + element + "' element must contain a '" + attrib + "' attribute", null); |
| 244 |
|
} |
| 245 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 246 |
0
|
private static XmlIngestionException generateMissingChildException(String element, String child) {... |
| 247 |
0
|
return generateException("EDocLite '" + element + "' element must contain a '" + child + "' child element", null); |
| 248 |
|
} |
| 249 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 250 |
0
|
private static XmlIngestionException generateSerializationException(String element, XmlException cause) {... |
| 251 |
0
|
return generateException("Error serializing EDocLite '" + element + "' element", cause); |
| 252 |
|
} |
| 253 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 254 |
0
|
private static EDocLiteService getEDLService() {... |
| 255 |
0
|
return KEWServiceLocator.getEDocLiteService(); |
| 256 |
|
} |
| 257 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 258 |
0
|
private static StyleService getStyleService() {... |
| 259 |
0
|
return CoreApiServiceLocator.getStyleService(); |
| 260 |
|
} |
| 261 |
|
} |