001/** 002 * Copyright 2005-2015 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.edl.impl.xml.export; 017 018import org.apache.logging.log4j.Logger; 019import org.apache.logging.log4j.LogManager; 020import org.jdom2.Element; 021import org.jdom2.Namespace; 022import org.kuali.rice.core.api.impex.ExportDataSet; 023import org.kuali.rice.coreservice.api.style.Style; 024import org.kuali.rice.coreservice.api.style.StyleService; 025import org.kuali.rice.core.api.util.xml.XmlHelper; 026import org.kuali.rice.core.api.util.xml.XmlRenderer; 027import org.kuali.rice.core.framework.impex.xml.XmlExporter; 028import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator; 029import org.kuali.rice.edl.impl.bo.EDocLiteAssociation; 030import org.kuali.rice.edl.impl.bo.EDocLiteDefinition; 031import org.kuali.rice.edl.impl.service.EDocLiteService; 032import org.kuali.rice.edl.impl.service.EdlServiceLocator; 033 034import java.io.StringReader; 035import java.util.Iterator; 036import java.util.List; 037 038import static org.kuali.rice.core.api.impex.xml.XmlConstants.*; 039/** 040 * Exports EDocLite definitions to XML. 041 * 042 * @see EDocLiteDefinition 043 * 044 * @author Kuali Rice Team (rice.collab@kuali.org) 045 */ 046public class EDocLiteXmlExporter implements XmlExporter { 047 048 private static final Logger LOG = LogManager.getLogger(EDocLiteXmlExporter.class); 049 050 private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE); 051 052 @Override 053 public boolean supportPrettyPrint() { 054 return false; 055 } 056 057 public Element export(ExportDataSet exportDataSet) { 058 EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet); 059 if (!dataSet.getEdocLites().isEmpty()) { 060 Element rootElement = renderer.renderElement(null, EDL_EDOCLITE); 061 rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE); 062 // 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. 063 List<EDocLiteAssociation> assocList = dataSet.getEdocLites(); 064 // loop thru same list 3 times. 065 for (EDocLiteAssociation e : assocList) { 066 exportEdlDefinitions(rootElement, e); 067 } 068 for (EDocLiteAssociation e : assocList) { 069 exportStyles(rootElement, e); 070 } 071 for (EDocLiteAssociation e : assocList) { 072 exportAssociations(rootElement, e); 073 } 074 return rootElement; 075 } 076 return null; 077 } 078 079 private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) { 080 081 try { 082 EDocLiteService edlService = EdlServiceLocator.getEDocLiteService(); 083 if (edl.getDefinition() != null) { //this probably shouldn't be supported on the entry side... 084 EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition()); 085 if (def == null) { 086 LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found"); 087 return; 088 } 089 Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement(); 090 setNamespace(defEl, EDL_NAMESPACE); 091 parentEl.addContent(defEl.detach()); 092 } 093 } catch (Exception e) { 094 throw new RuntimeException(e); 095 } 096 } 097 098 private void exportStyles(Element parentEl, EDocLiteAssociation edl) { 099 100 try { 101 StyleService styleService = CoreServiceApiServiceLocator.getStyleService(); 102 103 if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side... 104 Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE); 105 renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle()); 106 Style style = styleService.getStyle(edl.getStyle()); 107 if (style == null) { 108 LOG.error("Attempted to export style " + edl.getStyle() + " which was not found"); 109 return; 110 } 111 Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement(); 112 styleWrapperEl.addContent(styleEl.detach()); 113 } 114 } catch (Exception e) { 115 throw new RuntimeException(e); 116 } 117 } 118 119 private void exportAssociations(Element parentEl, EDocLiteAssociation edl) { 120 try { 121 Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION); 122 renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName()); 123 if (edl.getDefinition() != null) { 124 renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition()); 125 } 126 if (edl.getStyle() != null) { 127 renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle()); 128 } 129 130 renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString()); 131 } catch (Exception e) { 132 throw new RuntimeException(e); 133 } 134 } 135 136 private void setNamespace(Element element, Namespace namespace) { 137 element.setNamespace(namespace); 138 for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) { 139 setNamespace((Element)iter.next(), namespace); 140 } 141 } 142}