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 *     btatar
011 *
012 * $Id: XMLDirectoryWriter.java 30235 2008-02-18 15:35:09Z fguillaume $
013 */
014
015package org.nuxeo.ecm.core.io.impl.plugins;
016
017import java.io.File;
018import java.io.FileOutputStream;
019import java.io.IOException;
020import java.util.Map;
021
022import org.dom4j.Document;
023import org.dom4j.io.OutputFormat;
024import org.dom4j.io.XMLWriter;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.io.DocumentTranslationMap;
027import org.nuxeo.ecm.core.io.ExportedDocument;
028import org.nuxeo.ecm.core.io.impl.AbstractDocumentWriter;
029
030/**
031 * This class plays a role in the export pipe.It is used to generate xml files that have a nuxeo specific format.Each
032 * file contains information about a document model such as,general information like name, uid or document type, and
033 * information about the schemas that the document includes.
034 *
035 * @author btatar
036 */
037// XXX AT: is this stil useful?
038public class XMLDirectoryWriter extends AbstractDocumentWriter {
039
040    private File destination;
041
042    public XMLDirectoryWriter(String destinationPath) {
043        this(new File(destinationPath));
044    }
045
046    public XMLDirectoryWriter(File destination) {
047        this.destination = destination;
048    }
049
050    /**
051     * Gives the destination where the XML file will be generated.
052     */
053    public Object getDestination() {
054        return destination;
055    }
056
057    /**
058     * Sets the destination where the XML file will be generated.
059     */
060    public void setDestination(File destination) {
061        this.destination = destination;
062    }
063
064    @Override
065    public void close() {
066        destination = null;
067    }
068
069    @Override
070    public DocumentTranslationMap write(ExportedDocument doc) throws IOException {
071
072        File file = new File(getDestination() + File.separator + doc.getPath().toString());
073        if (!file.mkdirs()) {
074            throw new IOException("Cannot create target directory: " + file.getAbsolutePath());
075        }
076        OutputFormat format = AbstractDocumentWriter.createPrettyPrint();
077        XMLWriter writer = null;
078        try {
079            writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + "document.xml"),
080                    format);
081            writer.write(doc.getDocument());
082        } finally {
083            if (writer != null) {
084                writer.close();
085            }
086        }
087        Map<String, Blob> blobs = doc.getBlobs();
088        for (Map.Entry<String, Blob> entry : blobs.entrySet()) {
089            String blobPath = file.getAbsolutePath() + File.separator + entry.getKey();
090            entry.getValue().transferTo(new File(blobPath));
091        }
092
093        // write external documents
094        for (Map.Entry<String, Document> entry : doc.getDocuments().entrySet()) {
095            writer = null;
096            try {
097                writer = new XMLWriter(new FileOutputStream(file.getAbsolutePath() + File.separator + entry.getKey()
098                        + ".xml"), format);
099                writer.write(entry.getValue());
100            } finally {
101                if (writer != null) {
102                    writer.close();
103                }
104            }
105        }
106
107        return null;
108    }
109
110}