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 | 0 | public class XmlExporterServiceImpl implements XmlExporterService, XmlConstants { |
44 | |
|
45 | |
private List serviceOrder; |
46 | |
|
47 | |
public byte[] export(ExportDataSet dataSet) { |
48 | 0 | if (dataSet == null) { |
49 | 0 | throw new IllegalArgumentException("Xml Exporter cannot handle NULL data."); |
50 | |
} |
51 | 0 | Element rootElement = new Element(DATA_ELEMENT, WORKFLOW_NAMESPACE); |
52 | 0 | rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE); |
53 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
54 | 0 | Document document = new Document(rootElement); |
55 | 0 | for (Iterator iterator = serviceOrder.iterator(); iterator.hasNext();) { |
56 | 0 | XmlExporter exporter = (XmlExporter)KEWServiceLocator.getService((String) iterator.next()); |
57 | 0 | appendIfNotEmpty(rootElement, exporter.export(dataSet)); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
Format f; |
62 | 0 | if (!dataSet.getEdocLites().isEmpty() || !dataSet.getStyles().isEmpty()) { |
63 | 0 | f = Format.getRawFormat(); |
64 | 0 | f.setExpandEmptyElements(false); |
65 | 0 | f.setTextMode(Format.TextMode.PRESERVE); |
66 | |
} else { |
67 | 0 | f = Format.getPrettyFormat(); |
68 | |
} |
69 | 0 | XMLOutputter outputer = new XMLOutputter(f); |
70 | 0 | StringWriter writer = new StringWriter(); |
71 | |
try { |
72 | 0 | outputer.output(document, writer); |
73 | 0 | } catch (IOException e) { |
74 | 0 | throw new WorkflowRuntimeException("Could not write XML data export.", e); |
75 | 0 | } |
76 | 0 | return writer.toString().getBytes(); |
77 | |
} |
78 | |
|
79 | |
private void appendIfNotEmpty(Element parent, Element child) { |
80 | 0 | if (child != null) { |
81 | 0 | parent.addContent(child); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
public void setServiceOrder(List serviceOrder) throws BeanInitializationException { |
86 | 0 | this.serviceOrder = serviceOrder; |
87 | 0 | } |
88 | |
|
89 | |
} |