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: XMLDirectoryReader.java 29029 2008-01-14 18:38:14Z ldoguin $
013 */
014
015package org.nuxeo.ecm.core.io.impl.plugins;
016
017import java.io.BufferedInputStream;
018import java.io.File;
019import java.io.FileFilter;
020import java.io.FileInputStream;
021import java.io.IOException;
022
023import org.dom4j.Document;
024import org.dom4j.DocumentException;
025import org.dom4j.io.SAXReader;
026import org.nuxeo.common.utils.FileTreeIterator;
027import org.nuxeo.common.utils.FileUtils;
028import org.nuxeo.common.utils.Path;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.io.ExportConstants;
031import org.nuxeo.ecm.core.io.ExportedDocument;
032import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader;
033import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
034
035/**
036 * @author bs@nuxeo.com
037 */
038public class XMLDirectoryReader extends AbstractDocumentReader {
039
040    private File source;
041
042    private FileTreeIterator iterator;
043
044    public XMLDirectoryReader(String sourcePath) {
045        this(new File(sourcePath));
046    }
047
048    public XMLDirectoryReader(File source) {
049        this.source = source;
050        iterator = new FileTreeIterator(source);
051        iterator.setFilter(new FileFilter() {
052            @Override
053            public boolean accept(File pathname) {
054                return pathname.isDirectory();
055            }
056        });
057    }
058
059    public Object getSource() {
060        return source;
061    }
062
063    public void setSource(File source) {
064        this.source = source;
065    }
066
067    @Override
068    public void close() {
069        source = null;
070        iterator = null;
071    }
072
073    @Override
074    public ExportedDocument read() throws IOException {
075        if (iterator.hasNext()) {
076            File dir = iterator.next();
077            if (dir == null) {
078                return null;
079            }
080            // read document files
081            ExportedDocument xdoc = new ExportedDocumentImpl();
082            for (File file : dir.listFiles()) {
083                if (file.isFile()) {
084                    String name = file.getName();
085                    if (ExportConstants.DOCUMENT_FILE.equals(name)) {
086                        Document doc = loadXML(file);
087                        xdoc.setDocument(doc);
088                        /*
089                         * NXP-1688 Rux: the path was somehow left over when migrated from core 1.3.4 to 1.4.0. Pull
090                         * back.
091                         */
092                        xdoc.setPath(computeRelativePath(dir));
093                    } else if (name.endsWith(".xml")) {
094                        xdoc.putDocument(FileUtils.getFileNameNoExt(file.getName()), loadXML(file));
095                    } else { // presume a blob
096                        xdoc.putBlob(file.getName(), Blobs.createBlob(file));
097                    }
098                }
099            }
100            return xdoc;
101        }
102        return null;
103    }
104
105    /*
106     * NXP-1688 Rux: the path was somehow left over when migrated from core 1.3.4 to 1.4.0. Pull back.
107     */
108    private Path computeRelativePath(File file) {
109        /* NXP-2507 Rux: preserve directory structure with slashes instead OS name separator */
110        String subPathS = file.getAbsolutePath().substring(source.getAbsolutePath().length());
111        subPathS = subPathS.replace(File.separatorChar, '/');
112        return new Path(subPathS);
113    }
114
115    private static Document loadXML(File file) throws IOException {
116        BufferedInputStream in = null;
117        try {
118            in = new BufferedInputStream(new FileInputStream(file));
119            return new SAXReader().read(in);
120        } catch (DocumentException e) {
121            IOException ioe = new IOException("Failed to read file document " + file + ": " + e.getMessage());
122            ioe.setStackTrace(e.getStackTrace());
123            throw ioe;
124        } finally {
125            if (in != null) {
126                in.close();
127            }
128        }
129    }
130
131}