View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.impex.ExportDataSet;
22  import org.kuali.rice.coreservice.api.style.Style;
23  import org.kuali.rice.coreservice.api.style.StyleService;
24  import org.kuali.rice.core.api.util.xml.XmlHelper;
25  import org.kuali.rice.core.api.util.xml.XmlRenderer;
26  import org.kuali.rice.core.framework.impex.xml.XmlExporter;
27  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
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   * Exports EDocLite definitions to XML.
40   *
41   * @see EDocLiteDefinition
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class EDocLiteXmlExporter implements XmlExporter {
46  
47  	private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
48  
49  	private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
50  
51  	@Override
52  	public boolean supportPrettyPrint() {
53  		return false;
54  	}
55  	
56  	public Element export(ExportDataSet exportDataSet) {
57  		EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet);
58  		if (!dataSet.getEdocLites().isEmpty()) {
59  			Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
60  			rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
61  			// create output in order of all edl followed by all stylesheets, followed by all associations, this is so multiple edoclite's can be ingested in a single xml file.
62  			List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
63  			// loop thru same list 3 times.
64  			for (EDocLiteAssociation e : assocList) {
65  				exportEdlDefinitions(rootElement, e);
66  			}
67  			for (EDocLiteAssociation e : assocList) {
68  				exportStyles(rootElement, e);
69  			}
70  			for (EDocLiteAssociation e : assocList) {
71  				exportAssociations(rootElement, e);
72  			}
73  			return rootElement;
74  		}
75  		return null;
76  	}
77  
78  	private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
79  
80  		try {
81  			EDocLiteService edlService = EdlServiceLocator.getEDocLiteService();
82  			if (edl.getDefinition() != null) {  //this probably shouldn't be supported on the entry side...
83  				EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
84  				if (def == null) {
85  					LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
86  					return;
87  				}
88  				Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
89  				setNamespace(defEl, EDL_NAMESPACE);
90  				parentEl.addContent(defEl.detach());
91  			}
92  		} catch (Exception e) {
93  			throw new RuntimeException(e);
94  		}
95  	}
96  	
97  	private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
98  
99  		try {
100 			StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
101 
102 			if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
103 				Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE);
104 				renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle());
105 				Style style = styleService.getStyle(edl.getStyle());
106 				if (style == null) {
107 					LOG.error("Attempted to export style " + edl.getStyle() + " which was not found");
108 					return;
109 				}
110 				Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
111 				styleWrapperEl.addContent(styleEl.detach());
112 			}
113 		} catch (Exception e) {
114 			throw new RuntimeException(e);
115 		}
116 	}
117 	
118 	private void exportAssociations(Element parentEl, EDocLiteAssociation edl) {
119 		try {
120 			Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION);
121 			renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName());
122 			if (edl.getDefinition() != null) {
123 				renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition());
124 			}
125 			if (edl.getStyle() != null) {
126 				renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle());
127 			}
128 
129 			renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString());
130 		} catch (Exception e) {
131 			throw new RuntimeException(e);
132 		}
133 	}
134 
135 	private void setNamespace(Element element, Namespace namespace) {
136 		element.setNamespace(namespace);
137 		for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) {
138 			setNamespace((Element)iter.next(), namespace);
139 		}
140 	}
141 }