View Javadoc

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  public class XmlExporterServiceImpl implements XmlExporterService, XmlConstants {
44  
45      private List serviceOrder;
46  
47      public byte[] export(ExportDataSet dataSet) {
48          if (dataSet == null) {
49              throw new IllegalArgumentException("Xml Exporter cannot handle NULL data.");
50          }
51          Element rootElement = new Element(DATA_ELEMENT, WORKFLOW_NAMESPACE);
52          rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE);
53          rootElement.setAttribute(SCHEMA_LOCATION_ATTR, WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
54          Document document = new Document(rootElement);
55          for (Iterator iterator = serviceOrder.iterator(); iterator.hasNext();) {
56              XmlExporter exporter = (XmlExporter)KEWServiceLocator.getService((String) iterator.next());
57              appendIfNotEmpty(rootElement, exporter.export(dataSet));
58          }
59  
60          // TODO: KULRICE-4420 - this needs cleanup
61          Format f;
62          if (!dataSet.getEdocLites().isEmpty() || !dataSet.getStyles().isEmpty()) {
63              f = Format.getRawFormat();
64              f.setExpandEmptyElements(false);
65              f.setTextMode(Format.TextMode.PRESERVE);
66          } else {
67              f = Format.getPrettyFormat();
68          }
69          XMLOutputter outputer = new XMLOutputter(f);
70          StringWriter writer = new StringWriter();
71          try {
72              outputer.output(document, writer);
73          } catch (IOException e) {
74              throw new WorkflowRuntimeException("Could not write XML data export.", e);
75          }
76          return writer.toString().getBytes();
77      }
78  
79      private void appendIfNotEmpty(Element parent, Element child) {
80          if (child != null) {
81              parent.addContent(child);
82          }
83      }
84  
85      public void setServiceOrder(List serviceOrder) throws BeanInitializationException {
86          this.serviceOrder = serviceOrder;
87      }
88  
89  }