1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.impex.xml; |
18 | |
|
19 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DATA_ELEMENT; |
20 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_LOCATION_ATTR; |
21 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_NAMESPACE; |
22 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.WORKFLOW_NAMESPACE; |
23 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.WORKFLOW_SCHEMA_LOCATION; |
24 | |
|
25 | |
import java.io.IOException; |
26 | |
import java.io.StringWriter; |
27 | |
|
28 | |
import org.jdom.Document; |
29 | |
import org.jdom.Element; |
30 | |
import org.jdom.output.Format; |
31 | |
import org.jdom.output.XMLOutputter; |
32 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
33 | |
import org.kuali.rice.core.api.impex.xml.XmlExporterService; |
34 | |
import org.kuali.rice.core.framework.impex.xml.XmlExporter; |
35 | |
import org.kuali.rice.core.framework.impex.xml.XmlImpexRegistry; |
36 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class XmlExporterServiceImpl implements XmlExporterService { |
45 | |
|
46 | |
private XmlImpexRegistry xmlImpexRegistry; |
47 | |
|
48 | |
public byte[] export(ExportDataSet dataSet) { |
49 | 0 | if (dataSet == null) { |
50 | 0 | throw new IllegalArgumentException("Xml Exporter cannot handle NULL data."); |
51 | |
} |
52 | 0 | Element rootElement = new Element(DATA_ELEMENT, WORKFLOW_NAMESPACE); |
53 | 0 | rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE); |
54 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
55 | 0 | Document document = new Document(rootElement); |
56 | 0 | boolean shouldPrettyPrint = true; |
57 | 0 | for (XmlExporter exporter : xmlImpexRegistry.getExporters()) { |
58 | 0 | Element exportedElement = exporter.export(dataSet); |
59 | 0 | if (exportedElement != null) { |
60 | 0 | if (!exporter.supportPrettyPrint()) { |
61 | 0 | shouldPrettyPrint = false; |
62 | |
} |
63 | 0 | appendIfNotEmpty(rootElement, exportedElement); |
64 | |
} |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
Format f; |
69 | 0 | if (!shouldPrettyPrint) { |
70 | 0 | f = Format.getRawFormat(); |
71 | 0 | f.setExpandEmptyElements(false); |
72 | 0 | f.setTextMode(Format.TextMode.PRESERVE); |
73 | |
} else { |
74 | 0 | f = Format.getPrettyFormat(); |
75 | |
} |
76 | 0 | XMLOutputter outputer = new XMLOutputter(f); |
77 | 0 | StringWriter writer = new StringWriter(); |
78 | |
try { |
79 | 0 | outputer.output(document, writer); |
80 | 0 | } catch (IOException e) { |
81 | 0 | throw new WorkflowRuntimeException("Could not write XML data export.", e); |
82 | 0 | } |
83 | 0 | return writer.toString().getBytes(); |
84 | |
} |
85 | |
|
86 | |
private void appendIfNotEmpty(Element parent, Element child) { |
87 | 0 | if (child != null) { |
88 | 0 | parent.addContent(child); |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
public void setXmlImpexRegistry(XmlImpexRegistry xmlImpexRegistry) { |
93 | 0 | this.xmlImpexRegistry = xmlImpexRegistry; |
94 | 0 | } |
95 | |
|
96 | |
} |