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