001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id: XMLDocumentWriter.java 29029 2008-01-14 18:38:14Z ldoguin $
013 */
014
015package org.nuxeo.ecm.core.io.impl.plugins;
016
017import java.io.BufferedOutputStream;
018import java.io.File;
019import java.io.FileOutputStream;
020import java.io.IOException;
021import java.io.OutputStream;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.dom4j.io.OutputFormat;
026import org.dom4j.io.XMLWriter;
027import org.nuxeo.ecm.core.api.DocumentLocation;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.io.DocumentTranslationMap;
030import org.nuxeo.ecm.core.io.ExportedDocument;
031import org.nuxeo.ecm.core.io.impl.AbstractDocumentWriter;
032import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl;
033
034/**
035 * Writes to a file or output stream the XML corresponding to the document content.
036 * <p>
037 * Note that additional xml descriptors (like relations.xml, workflow.xml etc) are ignored
038 * <p>
039 * Also blobs are not handled specially. The value existing in the blob data element will be written down. By default
040 * blobs are referred as external references, so if their content is not written in the XML document. If you want to
041 * encode blobs as base64 inside the document you must use the {@link InlineBlobTransformer}
042 * <p>
043 * In order to write Blobs are encoded as Base64 and included in the XML document
044 *
045 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
046 */
047public class XMLDocumentWriter extends AbstractDocumentWriter {
048
049    private static final Log log = LogFactory.getLog(XMLDocumentWriter.class);
050
051    protected final OutputStream out;
052
053    public XMLDocumentWriter(File file) throws IOException {
054        this(new BufferedOutputStream(new FileOutputStream(file)));
055    }
056
057    public XMLDocumentWriter(OutputStream out) {
058        this.out = out;
059    }
060
061    @Override
062    public DocumentTranslationMap write(ExportedDocument doc) throws IOException {
063
064        OutputFormat format = AbstractDocumentWriter.createPrettyPrint();
065        XMLWriter writer = null;
066        try {
067            writer = new XMLWriter(out, format);
068            writer.write(doc.getDocument());
069        } finally {
070            if (writer != null) {
071                writer.close();
072            }
073        }
074
075        // keep location unchanged
076        DocumentLocation oldLoc = doc.getSourceLocation();
077        String oldServerName = oldLoc.getServerName();
078        DocumentRef oldDocRef = oldLoc.getDocRef();
079        DocumentTranslationMap map = new DocumentTranslationMapImpl(oldServerName, oldServerName);
080        map.put(oldDocRef, oldDocRef);
081        return map;
082    }
083
084    @Override
085    public void close() {
086        if (out != null) {
087            try {
088                out.close();
089            } catch (IOException e) {
090                log.error(e);
091            }
092        }
093    }
094
095}