Coverage Report - org.kuali.rice.core.impl.impex.xml.XmlExporterServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlExporterServiceImpl
0%
0/32
0%
0/12
4
 
 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.core.impl.impex.xml;
 17  
 
 18  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DATA_ELEMENT;
 19  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_LOCATION_ATTR;
 20  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SCHEMA_NAMESPACE;
 21  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.WORKFLOW_NAMESPACE;
 22  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.WORKFLOW_SCHEMA_LOCATION;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.io.StringWriter;
 26  
 
 27  
 import org.jdom.Document;
 28  
 import org.jdom.Element;
 29  
 import org.jdom.output.Format;
 30  
 import org.jdom.output.XMLOutputter;
 31  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 32  
 import org.kuali.rice.core.api.impex.xml.XmlExporterService;
 33  
 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
 34  
 import org.kuali.rice.core.framework.impex.xml.XmlImpexRegistry;
 35  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 36  
 
 37  
 /**
 38  
  * An implementation of the XmlExporterService which can be configured with a set of
 39  
  * services that know how to export various pieces of the {@link ExportDataSet} to XML.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  0
 public class XmlExporterServiceImpl implements XmlExporterService {
 44  
 
 45  
          private XmlImpexRegistry xmlImpexRegistry;
 46  
 
 47  
     public byte[] export(ExportDataSet dataSet) {
 48  0
         if (dataSet == null) {
 49  0
             throw new IllegalArgumentException("Xml Exporter cannot handle NULL data.");
 50  
         }
 51  0
         Element rootElement = new Element(DATA_ELEMENT, WORKFLOW_NAMESPACE);
 52  0
         rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE);
 53  0
         rootElement.setAttribute(SCHEMA_LOCATION_ATTR, WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 54  0
         Document document = new Document(rootElement);
 55  0
         boolean shouldPrettyPrint = true;
 56  0
         for (XmlExporter exporter : xmlImpexRegistry.getExporters()) {
 57  0
                 Element exportedElement = exporter.export(dataSet);
 58  0
                 if (exportedElement != null) {
 59  0
                         if (!exporter.supportPrettyPrint()) {
 60  0
                                 shouldPrettyPrint = false;
 61  
                         }
 62  0
                         appendIfNotEmpty(rootElement, exportedElement);
 63  
                 }
 64  0
         }
 65  
 
 66  
         // TODO: KULRICE-4420 - this needs cleanup
 67  
         Format f;
 68  0
         if (!shouldPrettyPrint) {
 69  0
             f = Format.getRawFormat();
 70  0
             f.setExpandEmptyElements(false);
 71  0
             f.setTextMode(Format.TextMode.PRESERVE);
 72  
         } else {
 73  0
             f = Format.getPrettyFormat();
 74  
         }
 75  0
         XMLOutputter outputer = new XMLOutputter(f);
 76  0
         StringWriter writer = new StringWriter();
 77  
         try {
 78  0
             outputer.output(document, writer);
 79  0
         } catch (IOException e) {
 80  0
             throw new WorkflowRuntimeException("Could not write XML data export.", e);
 81  0
         }
 82  0
         return writer.toString().getBytes();
 83  
     }
 84  
 
 85  
     private void appendIfNotEmpty(Element parent, Element child) {
 86  0
         if (child != null) {
 87  0
             parent.addContent(child);
 88  
         }
 89  0
     }
 90  
 
 91  
     public void setXmlImpexRegistry(XmlImpexRegistry xmlImpexRegistry) {
 92  0
             this.xmlImpexRegistry = xmlImpexRegistry;
 93  0
     }
 94  
 
 95  
 }