1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.edl.impl.xml.export; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.jdom.Element; |
20 | |
import org.jdom.Namespace; |
21 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
22 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
23 | |
import org.kuali.rice.core.api.style.Style; |
24 | |
import org.kuali.rice.core.api.style.StyleService; |
25 | |
import org.kuali.rice.core.api.util.xml.XmlHelper; |
26 | |
import org.kuali.rice.core.api.util.xml.XmlRenderer; |
27 | |
import org.kuali.rice.core.framework.impex.xml.XmlExporter; |
28 | |
import org.kuali.rice.edl.impl.bo.EDocLiteAssociation; |
29 | |
import org.kuali.rice.edl.impl.bo.EDocLiteDefinition; |
30 | |
import org.kuali.rice.edl.impl.service.EDocLiteService; |
31 | |
import org.kuali.rice.edl.impl.service.EdlServiceLocator; |
32 | |
|
33 | |
import java.io.StringReader; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
|
37 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.*; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class EDocLiteXmlExporter implements XmlExporter { |
46 | |
|
47 | 0 | private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class); |
48 | |
|
49 | 0 | private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE); |
50 | |
|
51 | |
@Override |
52 | |
public boolean supportPrettyPrint() { |
53 | 0 | return false; |
54 | |
} |
55 | |
|
56 | |
public Element export(ExportDataSet exportDataSet) { |
57 | 0 | EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet); |
58 | 0 | if (!dataSet.getEdocLites().isEmpty()) { |
59 | 0 | Element rootElement = renderer.renderElement(null, EDL_EDOCLITE); |
60 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
61 | |
|
62 | 0 | List<EDocLiteAssociation> assocList = dataSet.getEdocLites(); |
63 | |
|
64 | 0 | for (EDocLiteAssociation e : assocList) { |
65 | 0 | exportEdlDefinitions(rootElement, e); |
66 | |
} |
67 | 0 | for (EDocLiteAssociation e : assocList) { |
68 | 0 | exportStyles(rootElement, e); |
69 | |
} |
70 | 0 | for (EDocLiteAssociation e : assocList) { |
71 | 0 | exportAssociations(rootElement, e); |
72 | |
} |
73 | 0 | return rootElement; |
74 | |
} |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | |
private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) { |
79 | |
|
80 | |
try { |
81 | 0 | EDocLiteService edlService = EdlServiceLocator.getEDocLiteService(); |
82 | 0 | if (edl.getDefinition() != null) { |
83 | 0 | EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition()); |
84 | 0 | if (def == null) { |
85 | 0 | LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found"); |
86 | 0 | return; |
87 | |
} |
88 | 0 | Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement(); |
89 | 0 | setNamespace(defEl, EDL_NAMESPACE); |
90 | 0 | parentEl.addContent(defEl.detach()); |
91 | |
} |
92 | 0 | } catch (Exception e) { |
93 | 0 | throw new RuntimeException(e); |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | |
private void exportStyles(Element parentEl, EDocLiteAssociation edl) { |
98 | |
|
99 | |
try { |
100 | 0 | StyleService styleService = CoreApiServiceLocator.getStyleService(); |
101 | |
|
102 | 0 | if (edl.getStyle() != null) { |
103 | 0 | Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE); |
104 | 0 | renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle()); |
105 | 0 | Style style = styleService.getStyle(edl.getStyle()); |
106 | 0 | if (style == null) { |
107 | 0 | LOG.error("Attempted to export style " + edl.getStyle() + " which was not found"); |
108 | 0 | return; |
109 | |
} |
110 | 0 | Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement(); |
111 | 0 | styleWrapperEl.addContent(styleEl.detach()); |
112 | |
} |
113 | 0 | } catch (Exception e) { |
114 | 0 | throw new RuntimeException(e); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
private void exportAssociations(Element parentEl, EDocLiteAssociation edl) { |
119 | |
try { |
120 | 0 | Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION); |
121 | 0 | renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName()); |
122 | 0 | if (edl.getDefinition() != null) { |
123 | 0 | renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition()); |
124 | |
} |
125 | 0 | if (edl.getStyle() != null) { |
126 | 0 | renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle()); |
127 | |
} |
128 | |
|
129 | 0 | renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString()); |
130 | 0 | } catch (Exception e) { |
131 | 0 | throw new RuntimeException(e); |
132 | 0 | } |
133 | 0 | } |
134 | |
|
135 | |
private void setNamespace(Element element, Namespace namespace) { |
136 | 0 | element.setNamespace(namespace); |
137 | 0 | for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) { |
138 | 0 | setNamespace((Element)iter.next(), namespace); |
139 | |
} |
140 | 0 | } |
141 | |
} |