001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer
018 */
019package org.nuxeo.ecm.core.io.impl;
020
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.core.io.DocumentPipe;
030import org.nuxeo.ecm.core.io.DocumentReader;
031import org.nuxeo.ecm.core.io.DocumentWriter;
032import org.nuxeo.ecm.core.io.DocumentXMLExporter;
033import org.nuxeo.ecm.core.io.impl.plugins.TypedSingleDocumentReader;
034import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;
035import org.xml.sax.InputSource;
036
037/**
038 * Default implementation of a {@link DocumentXMLExporter}.
039 *
040 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
041 * @since 5.6
042 */
043public class DocumentXMLExporterImpl implements DocumentXMLExporter {
044
045    private static final long serialVersionUID = 4086449614391137730L;
046
047    /**
048     * {@inheritDoc}
049     */
050    public InputStream exportXML(DocumentModel doc, CoreSession session) {
051
052        byte[] xmlExportByteArray = exportXMLAsByteArray(doc, session);
053        return new ByteArrayInputStream(xmlExportByteArray);
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    public InputSource exportXMLAsInputSource(DocumentModel doc, CoreSession session) {
060
061        InputStream xmlExportInputStream = exportXML(doc, session);
062        return new InputSource(xmlExportInputStream);
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    public final byte[] exportXMLAsByteArray(DocumentModel doc, CoreSession session) {
069
070        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
071        DocumentWriter documentWriter = new XMLDocumentWriter(outputStream);
072        DocumentReader documentReader = new TypedSingleDocumentReader(session, doc);
073
074        DocumentPipe pipe = new DocumentPipeImpl();
075        pipe.setReader(documentReader);
076        pipe.setWriter(documentWriter);
077
078        try {
079            pipe.run();
080        } catch (IOException e) {
081            throw new NuxeoException("Error while trying to export the document to XML.", e);
082        } finally {
083            if (documentReader != null) {
084                documentReader.close();
085            }
086            if (documentWriter != null) {
087                documentWriter.close();
088            }
089        }
090
091        return outputStream.toByteArray();
092    }
093}