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