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 @Override 051 public InputStream exportXML(DocumentModel doc, CoreSession session) { 052 053 byte[] xmlExportByteArray = exportXMLAsByteArray(doc, session); 054 return new ByteArrayInputStream(xmlExportByteArray); 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 @Override 061 public InputSource exportXMLAsInputSource(DocumentModel doc, CoreSession session) { 062 063 return new InputSource(exportXML(doc, session)); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public final byte[] exportXMLAsByteArray(DocumentModel doc, CoreSession session) { 071 072 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 073 DocumentWriter documentWriter = new XMLDocumentWriter(outputStream); 074 DocumentReader documentReader = new TypedSingleDocumentReader(session, doc); 075 076 DocumentPipe pipe = new DocumentPipeImpl(); 077 pipe.setReader(documentReader); 078 pipe.setWriter(documentWriter); 079 080 try { 081 pipe.run(); 082 } catch (IOException e) { 083 throw new NuxeoException("Error while trying to export the document to XML.", e); 084 } finally { 085 if (documentReader != null) { 086 documentReader.close(); 087 } 088 if (documentWriter != null) { 089 documentWriter.close(); 090 } 091 } 092 093 return outputStream.toByteArray(); 094 } 095}