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