1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.style; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.jdom.Element; |
21 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
22 | |
import org.kuali.rice.core.api.util.xml.XmlException; |
23 | |
import org.kuali.rice.core.api.util.xml.XmlHelper; |
24 | |
import org.kuali.rice.core.api.util.xml.XmlRenderer; |
25 | |
import org.kuali.rice.core.framework.impex.xml.XmlExporter; |
26 | |
|
27 | |
import java.io.StringReader; |
28 | |
import java.util.Iterator; |
29 | |
|
30 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.*; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class StyleXmlExporter implements XmlExporter { |
42 | 0 | private static final Logger LOG = Logger.getLogger(StyleXmlExporter.class); |
43 | |
|
44 | 0 | private XmlRenderer renderer = new XmlRenderer(STYLE_NAMESPACE); |
45 | |
|
46 | |
@Override |
47 | |
public boolean supportPrettyPrint() { |
48 | 0 | return false; |
49 | |
} |
50 | |
|
51 | |
public Element export(ExportDataSet exportDataSet) { |
52 | 0 | StyleExportDataSet dataSet = StyleExportDataSet.fromExportDataSet(exportDataSet); |
53 | 0 | if (!dataSet.getStyles().isEmpty()) { |
54 | 0 | Element rootElement = renderer.renderElement(null, STYLE_STYLES); |
55 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, STYLE_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
56 | 0 | for (Iterator<StyleBo> iter = dataSet.getStyles().iterator(); iter.hasNext();) { |
57 | 0 | StyleBo edocLite = iter.next(); |
58 | 0 | exportStyle(rootElement, edocLite); |
59 | 0 | } |
60 | 0 | return rootElement; |
61 | |
} |
62 | 0 | return null; |
63 | |
} |
64 | |
|
65 | |
private void exportStyle(Element parentEl, StyleBo style) { |
66 | 0 | if (style == null) { |
67 | 0 | LOG.error("Attempted to export style which was not found"); |
68 | 0 | return; |
69 | |
} |
70 | |
|
71 | 0 | Element styleWrapperEl = renderer.renderElement(parentEl, STYLE_STYLE); |
72 | 0 | renderer.renderAttribute(styleWrapperEl, "name", style.getName()); |
73 | |
|
74 | |
try { |
75 | 0 | Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement(); |
76 | 0 | styleWrapperEl.addContent(styleEl.detach()); |
77 | 0 | } catch (XmlException e) { |
78 | 0 | throw new RuntimeException("Error building JDom document for style", e); |
79 | 0 | } |
80 | 0 | } |
81 | |
} |