Coverage Report - org.kuali.rice.kew.xml.export.XmlExporterServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlExporterServiceImpl
0%
0/28
0%
0/10
3.667
 
 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.kew.xml.export;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.StringWriter;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.jdom.Document;
 25  
 import org.jdom.Element;
 26  
 import org.jdom.output.Format;
 27  
 import org.jdom.output.Format.TextMode;
 28  
 import org.jdom.output.XMLOutputter;
 29  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 30  
 import org.kuali.rice.kew.export.ExportDataSet;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kew.xml.XmlConstants;
 33  
 import org.kuali.rice.kns.exception.ExportNotSupportedException;
 34  
 import org.springframework.beans.factory.BeanInitializationException;
 35  
 
 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, XmlConstants {
 44  
 
 45  
     private List serviceOrder;
 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
         for (Iterator iterator = serviceOrder.iterator(); iterator.hasNext();) {
 56  0
             XmlExporter exporter = (XmlExporter)KEWServiceLocator.getService((String) iterator.next());
 57  0
             appendIfNotEmpty(rootElement, exporter.export(dataSet));
 58  0
         }
 59  
 
 60  
         // TODO: KULRICE-4420 - this needs cleanup
 61  
         Format f;
 62  0
         if (!dataSet.getEdocLites().isEmpty() || !dataSet.getStyles().isEmpty()) {
 63  0
             f = Format.getRawFormat();
 64  0
             f.setExpandEmptyElements(false);
 65  0
             f.setTextMode(Format.TextMode.PRESERVE);
 66  
         } else {
 67  0
             f = Format.getPrettyFormat();
 68  
         }
 69  0
         XMLOutputter outputer = new XMLOutputter(f);
 70  0
         StringWriter writer = new StringWriter();
 71  
         try {
 72  0
             outputer.output(document, writer);
 73  0
         } catch (IOException e) {
 74  0
             throw new WorkflowRuntimeException("Could not write XML data export.", e);
 75  0
         }
 76  0
         return writer.toString().getBytes();
 77  
     }
 78  
 
 79  
     private void appendIfNotEmpty(Element parent, Element child) {
 80  0
         if (child != null) {
 81  0
             parent.addContent(child);
 82  
         }
 83  0
     }
 84  
 
 85  
     public void setServiceOrder(List serviceOrder) throws BeanInitializationException {
 86  0
         this.serviceOrder = serviceOrder;
 87  0
     }
 88  
 
 89  
 }