1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.xml.export;
18
19 import java.io.IOException;
20 import java.io.StringWriter;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.jdom.Document;
25 import org.jdom.Element;
26 import org.jdom.output.Format;
27 import org.jdom.output.Format.TextMode;
28 import org.jdom.output.XMLOutputter;
29 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
30 import org.kuali.rice.kew.export.ExportDataSet;
31 import org.kuali.rice.kew.service.KEWServiceLocator;
32 import org.kuali.rice.kew.xml.XmlConstants;
33 import org.kuali.rice.kns.exception.ExportNotSupportedException;
34 import org.springframework.beans.factory.BeanInitializationException;
35
36
37
38
39
40
41
42
43 public class XmlExporterServiceImpl implements XmlExporterService, XmlConstants {
44
45 private List serviceOrder;
46
47 public byte[] export(ExportDataSet dataSet) {
48 if (dataSet == null) {
49 throw new IllegalArgumentException("Xml Exporter cannot handle NULL data.");
50 }
51 Element rootElement = new Element(DATA_ELEMENT, WORKFLOW_NAMESPACE);
52 rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE);
53 rootElement.setAttribute(SCHEMA_LOCATION_ATTR, WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
54 Document document = new Document(rootElement);
55 for (Iterator iterator = serviceOrder.iterator(); iterator.hasNext();) {
56 XmlExporter exporter = (XmlExporter)KEWServiceLocator.getService((String) iterator.next());
57 appendIfNotEmpty(rootElement, exporter.export(dataSet));
58 }
59
60
61 Format f;
62 if (!dataSet.getEdocLites().isEmpty() || !dataSet.getStyles().isEmpty()) {
63 f = Format.getRawFormat();
64 f.setExpandEmptyElements(false);
65 f.setTextMode(Format.TextMode.PRESERVE);
66 } else {
67 f = Format.getPrettyFormat();
68 }
69 XMLOutputter outputer = new XMLOutputter(f);
70 StringWriter writer = new StringWriter();
71 try {
72 outputer.output(document, writer);
73 } catch (IOException e) {
74 throw new WorkflowRuntimeException("Could not write XML data export.", e);
75 }
76 return writer.toString().getBytes();
77 }
78
79 private void appendIfNotEmpty(Element parent, Element child) {
80 if (child != null) {
81 parent.addContent(child);
82 }
83 }
84
85 public void setServiceOrder(List serviceOrder) throws BeanInitializationException {
86 this.serviceOrder = serviceOrder;
87 }
88
89 }