Coverage Report - org.kuali.rice.edl.impl.xml.export.EDocLiteXmlExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
EDocLiteXmlExporter
0%
0/59
0%
0/22
4.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.CoreApiServiceLocator;
 22  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 23  
 import org.kuali.rice.core.api.style.Style;
 24  
 import org.kuali.rice.core.api.style.StyleService;
 25  
 import org.kuali.rice.core.api.util.xml.XmlHelper;
 26  
 import org.kuali.rice.core.api.util.xml.XmlRenderer;
 27  
 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
 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  0
 public class EDocLiteXmlExporter implements XmlExporter {
 46  
 
 47  0
         private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
 48  
 
 49  0
         private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
 50  
 
 51  
         @Override
 52  
         public boolean supportPrettyPrint() {
 53  0
                 return false;
 54  
         }
 55  
         
 56  
         public Element export(ExportDataSet exportDataSet) {
 57  0
                 EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet);
 58  0
                 if (!dataSet.getEdocLites().isEmpty()) {
 59  0
                         Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
 60  0
                         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  0
                         List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
 63  
                         // loop thru same list 3 times.
 64  0
                         for (EDocLiteAssociation e : assocList) {
 65  0
                                 exportEdlDefinitions(rootElement, e);
 66  
                         }
 67  0
                         for (EDocLiteAssociation e : assocList) {
 68  0
                                 exportStyles(rootElement, e);
 69  
                         }
 70  0
                         for (EDocLiteAssociation e : assocList) {
 71  0
                                 exportAssociations(rootElement, e);
 72  
                         }
 73  0
                         return rootElement;
 74  
                 }
 75  0
                 return null;
 76  
         }
 77  
 
 78  
         private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
 79  
 
 80  
                 try {
 81  0
                         EDocLiteService edlService = EdlServiceLocator.getEDocLiteService();
 82  0
                         if (edl.getDefinition() != null) {  //this probably shouldn't be supported on the entry side...
 83  0
                                 EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
 84  0
                                 if (def == null) {
 85  0
                                         LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
 86  0
                                         return;
 87  
                                 }
 88  0
                                 Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
 89  0
                                 setNamespace(defEl, EDL_NAMESPACE);
 90  0
                                 parentEl.addContent(defEl.detach());
 91  
                         }
 92  0
                 } catch (Exception e) {
 93  0
                         throw new RuntimeException(e);
 94  0
                 }
 95  0
         }
 96  
         
 97  
         private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
 98  
 
 99  
                 try {
 100  0
                         StyleService styleService = CoreApiServiceLocator.getStyleService();
 101  
 
 102  0
                         if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
 103  0
                                 Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE);
 104  0
                                 renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle());
 105  0
                                 Style style = styleService.getStyle(edl.getStyle());
 106  0
                                 if (style == null) {
 107  0
                                         LOG.error("Attempted to export style " + edl.getStyle() + " which was not found");
 108  0
                                         return;
 109  
                                 }
 110  0
                                 Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
 111  0
                                 styleWrapperEl.addContent(styleEl.detach());
 112  
                         }
 113  0
                 } catch (Exception e) {
 114  0
                         throw new RuntimeException(e);
 115  0
                 }
 116  0
         }
 117  
         
 118  
         private void exportAssociations(Element parentEl, EDocLiteAssociation edl) {
 119  
                 try {
 120  0
                         Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION);
 121  0
                         renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName());
 122  0
                         if (edl.getDefinition() != null) {
 123  0
                                 renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition());
 124  
                         }
 125  0
                         if (edl.getStyle() != null) {
 126  0
                                 renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle());
 127  
                         }
 128  
 
 129  0
                         renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString());
 130  0
                 } catch (Exception e) {
 131  0
                         throw new RuntimeException(e);
 132  0
                 }
 133  0
         }
 134  
 
 135  
         private void setNamespace(Element element, Namespace namespace) {
 136  0
                 element.setNamespace(namespace);
 137  0
                 for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) {
 138  0
                         setNamespace((Element)iter.next(), namespace);
 139  
                 }
 140  0
         }
 141  
 }