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