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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: XMLDocumentTreeWriter.java 29029 2008-01-14 18:38:14Z ldoguin $
020 */
021
022package org.nuxeo.ecm.core.io.impl.plugins;
023
024import java.io.File;
025import java.io.IOException;
026import java.io.OutputStream;
027import java.io.UnsupportedEncodingException;
028import java.util.Collection;
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
041public class XMLDocumentTreeWriter extends XMLDocumentWriter {
042
043    private static final Log log = LogFactory.getLog(XMLDocumentTreeWriter.class);
044
045    private XMLWriter writer;
046
047    public XMLDocumentTreeWriter(File file) throws IOException {
048        super(file);
049    }
050
051    public XMLDocumentTreeWriter(OutputStream out) {
052        super(out);
053    }
054
055    protected XMLWriter initWriter() {
056        if (writer == null) {
057            try {
058                OutputFormat format = AbstractDocumentWriter.createCompactFormat();
059                format.setSuppressDeclaration(true);
060                writer = new XMLWriter(out, format);
061            } catch (UnsupportedEncodingException e) {
062                // XXX
063            }
064        }
065
066        return writer;
067    }
068
069    @Override
070    public DocumentTranslationMap write(ExportedDocument doc) throws IOException {
071
072        initWriter();
073        writer.write(doc.getDocument());
074        // out.write(doc.getDocument().asXML().getBytes());
075
076        // keep location unchanged
077        DocumentLocation oldLoc = doc.getSourceLocation();
078        String oldServerName = oldLoc.getServerName();
079        DocumentRef oldDocRef = oldLoc.getDocRef();
080        DocumentTranslationMap map = new DocumentTranslationMapImpl(oldServerName, oldServerName);
081        map.put(oldDocRef, oldDocRef);
082        return map;
083    }
084
085    @Override
086    public DocumentTranslationMap write(ExportedDocument[] docs) throws IOException {
087        initWriter();
088        out.write("<documents>".getBytes());
089        out.flush();
090        DocumentTranslationMap map = super.write(docs);
091        writer.flush();
092        out.write("</documents>".getBytes());
093        return map;
094    }
095
096    @Override
097    public DocumentTranslationMap write(Collection<ExportedDocument> docs) throws IOException {
098        initWriter();
099        out.write("<documents>".getBytes());
100        out.flush();
101        DocumentTranslationMap map = super.write(docs);
102        writer.flush();
103        out.write("</documents>".getBytes());
104        return map;
105    }
106
107    @Override
108    public void close() {
109        if (writer != null) {
110            try {
111                writer.close();
112            } catch (IOException e) {
113                // XXX
114            }
115        }
116        if (out != null) {
117            try {
118                out.close();
119            } catch (IOException e) {
120                log.error(e);
121            }
122        }
123    }
124
125}