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: XMLZipReader.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.InputStream;
027import java.util.Enumeration;
028import java.util.zip.ZipEntry;
029import java.util.zip.ZipFile;
030
031import org.dom4j.Document;
032import org.nuxeo.common.utils.Path;
033import org.nuxeo.ecm.core.io.ExportConstants;
034import org.nuxeo.ecm.core.io.ExportedDocument;
035import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader;
036import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class XMLZipReader extends AbstractDocumentReader {
042
043    private ZipFile zip;
044
045    public XMLZipReader(ZipFile zip) {
046        this.zip = zip;
047    }
048
049    public XMLZipReader(String source) throws IOException {
050        this(new ZipFile(source));
051    }
052
053    public XMLZipReader(File source) throws IOException {
054        this(new ZipFile(source));
055    }
056
057    @Override
058    // the zip entry order is the same as one used when creating the zip
059    public ExportedDocument read() throws IOException {
060        Enumeration<? extends ZipEntry> entries = zip.entries();
061        while (entries.hasMoreElements()) {
062            ZipEntry entry = entries.nextElement();
063            if (entry.isDirectory()) {
064                return createDocument(entry);
065            }
066        }
067        return null;
068    }
069
070    @Override
071    public void close() {
072        if (zip != null) {
073            try {
074                zip.close();
075            } catch (IOException e) {
076                // do nothing
077            } finally {
078                zip = null;
079            }
080        }
081    }
082
083    private ExportedDocument createDocument(ZipEntry dirEntry) throws IOException {
084        ExportedDocument xdoc = new ExportedDocumentImpl();
085        String dirPath = dirEntry.getName();
086        // TODO -> some processing on the path?
087        xdoc.setPath(new Path(dirPath).removeTrailingSeparator());
088        // read the main document
089        ZipEntry entry = zip.getEntry(dirPath + ExportConstants.DOCUMENT_FILE);
090        InputStream in = zip.getInputStream(entry);
091        try {
092            Document doc = readXML(in);
093            doc.setDocument(doc);
094        } finally {
095            in.close();
096        }
097
098        return null;
099    }
100
101    public Document readXML(InputStream in) {
102        return null;
103    }
104
105}