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