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-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.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 org.kuali.rice.edl.impl.xml.export;
 18  
 
 19  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_ACTIVE;
 20  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_ASSOCIATION;
 21  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_DEFINITION;
 22  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_DOC_TYPE;
 23  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_EDOCLITE;
 24  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_NAMESPACE;
 25  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_SCHEMA_LOCATION;
 26  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EDL_STYLE;
 27  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_LOCATION_ATTR;
 28  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_NAMESPACE;
 29  
 
 30  
 import java.io.StringReader;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 import org.apache.log4j.Logger;
 35  
 import org.jdom.Element;
 36  
 import org.jdom.Namespace;
 37  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 38  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 39  
 import org.kuali.rice.core.api.style.Style;
 40  
 import org.kuali.rice.core.api.style.StyleService;
 41  
 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
 42  
 import org.kuali.rice.core.util.xml.XmlHelper;
 43  
 import org.kuali.rice.core.util.xml.XmlRenderer;
 44  
 import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
 45  
 import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
 46  
 import org.kuali.rice.edl.impl.service.EDocLiteService;
 47  
 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
 48  
 /**
 49  
  * Exports EDocLite definitions to XML.
 50  
  *
 51  
  * @see EDocLiteDefinition
 52  
  *
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 public class EDocLiteXmlExporter implements XmlExporter {
 56  
 
 57  0
         private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
 58  
 
 59  0
         private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
 60  
 
 61  
         @Override
 62  
         public boolean supportPrettyPrint() {
 63  0
                 return false;
 64  
         }
 65  
         
 66  
         public Element export(ExportDataSet exportDataSet) {
 67  0
                 EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet);
 68  0
                 if (!dataSet.getEdocLites().isEmpty()) {
 69  0
                         Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
 70  0
                         rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 71  
                         // 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.
 72  0
                         List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
 73  
                         // loop thru same list 3 times.
 74  0
                         for (EDocLiteAssociation e : assocList) {
 75  0
                                 exportEdlDefinitions(rootElement, e);
 76  
                         }
 77  0
                         for (EDocLiteAssociation e : assocList) {
 78  0
                                 exportStyles(rootElement, e);
 79  
                         }
 80  0
                         for (EDocLiteAssociation e : assocList) {
 81  0
                                 exportAssociations(rootElement, e);
 82  
                         }
 83  0
                         return rootElement;
 84  
                 }
 85  0
                 return null;
 86  
         }
 87  
 
 88  
         private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
 89  
 
 90  
                 try {
 91  0
                         EDocLiteService edlService = EdlServiceLocator.getEDocLiteService();
 92  0
                         if (edl.getDefinition() != null) {  //this probably shouldn't be supported on the entry side...
 93  0
                                 EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
 94  0
                                 if (def == null) {
 95  0
                                         LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
 96  0
                                         return;
 97  
                                 }
 98  0
                                 Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
 99  0
                                 setNamespace(defEl, EDL_NAMESPACE);
 100  0
                                 parentEl.addContent(defEl.detach());
 101  
                         }
 102  0
                 } catch (Exception e) {
 103  0
                         throw new RuntimeException(e);
 104  0
                 }
 105  0
         }
 106  
         
 107  
         private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
 108  
 
 109  
                 try {
 110  0
                         StyleService styleService = CoreApiServiceLocator.getStyleService();
 111  
 
 112  0
                         if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
 113  0
                                 Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE);
 114  0
                                 renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle());
 115  0
                                 Style style = styleService.getStyle(edl.getStyle());
 116  0
                                 if (style == null) {
 117  0
                                         LOG.error("Attempted to export style " + edl.getStyle() + " which was not found");
 118  0
                                         return;
 119  
                                 }
 120  0
                                 Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
 121  0
                                 styleWrapperEl.addContent(styleEl.detach());
 122  
                         }
 123  0
                 } catch (Exception e) {
 124  0
                         throw new RuntimeException(e);
 125  0
                 }
 126  0
         }
 127  
         
 128  
         private void exportAssociations(Element parentEl, EDocLiteAssociation edl) {
 129  
                 try {
 130  0
                         Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION);
 131  0
                         renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName());
 132  0
                         if (edl.getDefinition() != null) {
 133  0
                                 renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition());
 134  
                         }
 135  0
                         if (edl.getStyle() != null) {
 136  0
                                 renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle());
 137  
                         }
 138  
 
 139  0
                         renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString());
 140  0
                 } catch (Exception e) {
 141  0
                         throw new RuntimeException(e);
 142  0
                 }
 143  0
         }
 144  
 
 145  
         private void setNamespace(Element element, Namespace namespace) {
 146  0
                 element.setNamespace(namespace);
 147  0
                 for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) {
 148  0
                         setNamespace((Element)iter.next(), namespace);
 149  
                 }
 150  0
         }
 151  
 }