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