01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.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.XMLOutputter;
28: import org.springframework.beans.factory.BeanInitializationException;
29:
30: import edu.iu.uis.eden.EdenConstants;
31: import edu.iu.uis.eden.KEWServiceLocator;
32: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
33: import edu.iu.uis.eden.export.ExportDataSet;
34: import edu.iu.uis.eden.export.ExportFormat;
35: import edu.iu.uis.eden.export.ExportNotSupportedException;
36: import edu.iu.uis.eden.xml.XmlConstants;
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 ewestfal
43: */
44: public class XmlExporterServiceImpl implements XmlExporterService,
45: XmlConstants {
46:
47: private List serviceOrder;
48:
49: public List getSupportedFormats() {
50: return EdenConstants.STANDARD_FORMATS;
51: }
52:
53: public byte[] export(ExportFormat format, ExportDataSet dataSet) {
54: if (!getSupportedFormats().contains(format)) {
55: throw new ExportNotSupportedException(
56: "Xml Exporter does not support the format "
57: + format.getFormatName());
58: }
59: if (dataSet == null) {
60: throw new ExportNotSupportedException(
61: "Xml Exporter cannot handle NULL data.");
62: }
63: Element rootElement = new Element(DATA_ELEMENT,
64: WORKFLOW_NAMESPACE);
65: rootElement.addNamespaceDeclaration(SCHEMA_NAMESPACE);
66: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
67: WORKFLOW_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
68: Document document = new Document(rootElement);
69: for (Iterator iterator = serviceOrder.iterator(); iterator
70: .hasNext();) {
71: XmlExporter exporter = (XmlExporter) KEWServiceLocator
72: .getService((String) iterator.next());
73: appendIfNotEmpty(rootElement, exporter.export(dataSet));
74: }
75:
76: XMLOutputter outputer = new XMLOutputter(Format
77: .getPrettyFormat());
78: StringWriter writer = new StringWriter();
79: try {
80: outputer.output(document, writer);
81: } catch (IOException e) {
82: throw new WorkflowRuntimeException(
83: "Could not write XML data export.", e);
84: }
85: return writer.toString().getBytes();
86: }
87:
88: private void appendIfNotEmpty(Element parent, Element child) {
89: if (child != null) {
90: parent.addContent(child);
91: }
92: }
93:
94: public void setServiceOrder(List serviceOrder)
95: throws BeanInitializationException {
96: this.serviceOrder = serviceOrder;
97: }
98:
99: }
|