01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.xml.export;
18:
19: import java.io.StringReader;
20: import java.util.Iterator;
21:
22: import org.apache.log4j.Logger;
23: import org.jdom.Element;
24:
25: import edu.iu.uis.eden.edl.EDocLiteStyle;
26: import edu.iu.uis.eden.exception.InvalidXmlException;
27: import edu.iu.uis.eden.export.ExportDataSet;
28: import edu.iu.uis.eden.util.XmlHelper;
29: import edu.iu.uis.eden.xml.XmlConstants;
30:
31: /**
32: * Exports Style definitions to XML.
33: *
34: * @see edu.iu.uis.eden.edl.StyleService
35: * @see edu.iu.uis.eden.xml.StyleXmlParser
36: * @see EDocLiteStyle
37: *
38: * @author ahamid
39: * @author rkirkend
40: */
41: public class StyleXmlExporter implements XmlExporter, XmlConstants {
42: private static final Logger LOG = Logger
43: .getLogger(StyleXmlExporter.class);
44:
45: private ExportRenderer renderer = new ExportRenderer(
46: STYLE_NAMESPACE);
47:
48: public Element export(ExportDataSet dataSet) {
49: if (!dataSet.getStyles().isEmpty()) {
50: Element rootElement = renderer.renderElement(null,
51: STYLE_STYLES);
52: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
53: STYLE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
54: for (Iterator iter = dataSet.getStyles().iterator(); iter
55: .hasNext();) {
56: EDocLiteStyle edocLite = (EDocLiteStyle) iter.next();
57: exportStyle(rootElement, edocLite);
58: }
59: return rootElement;
60: }
61: return null;
62: }
63:
64: private void exportStyle(Element parentEl, EDocLiteStyle style) {
65: if (style == null) {
66: LOG.error("Attempted to export style which was not found");
67: return;
68: }
69:
70: Element styleWrapperEl = renderer.renderElement(parentEl,
71: STYLE_STYLE);
72: renderer.renderAttribute(styleWrapperEl, "name", style
73: .getName());
74:
75: try {
76: Element styleEl = XmlHelper.buildJDocument(
77: new StringReader(style.getXmlContent()))
78: .getRootElement();
79: styleWrapperEl.addContent(styleEl.detach());
80: } catch (InvalidXmlException e) {
81: throw new RuntimeException(
82: "Error building JDom document for style", e);
83: }
84: }
85: }
|