001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer
016 */
017package org.nuxeo.ecm.core.io.impl;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.ecm.core.io.DocumentPipe;
028import org.nuxeo.ecm.core.io.DocumentReader;
029import org.nuxeo.ecm.core.io.DocumentWriter;
030import org.nuxeo.ecm.core.io.DocumentXMLExporter;
031import org.nuxeo.ecm.core.io.impl.plugins.TypedSingleDocumentReader;
032import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;
033import org.xml.sax.InputSource;
034
035/**
036 * Default implementation of a {@link DocumentXMLExporter}.
037 *
038 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
039 * @since 5.6
040 */
041public class DocumentXMLExporterImpl implements DocumentXMLExporter {
042
043    private static final long serialVersionUID = 4086449614391137730L;
044
045    /**
046     * {@inheritDoc}
047     */
048    public InputStream exportXML(DocumentModel doc, CoreSession session) {
049
050        byte[] xmlExportByteArray = exportXMLAsByteArray(doc, session);
051        return new ByteArrayInputStream(xmlExportByteArray);
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    public InputSource exportXMLAsInputSource(DocumentModel doc, CoreSession session) {
058
059        InputStream xmlExportInputStream = exportXML(doc, session);
060        return new InputSource(xmlExportInputStream);
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    public final byte[] exportXMLAsByteArray(DocumentModel doc, CoreSession session) {
067
068        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
069        DocumentWriter documentWriter = new XMLDocumentWriter(outputStream);
070        DocumentReader documentReader = new TypedSingleDocumentReader(session, doc);
071
072        DocumentPipe pipe = new DocumentPipeImpl();
073        pipe.setReader(documentReader);
074        pipe.setWriter(documentWriter);
075
076        try {
077            pipe.run();
078        } catch (IOException e) {
079            throw new NuxeoException("Error while trying to export the document to XML.", e);
080        } finally {
081            if (documentReader != null) {
082                documentReader.close();
083            }
084            if (documentWriter != null) {
085                documentWriter.close();
086            }
087        }
088
089        return outputStream.toByteArray();
090    }
091}