001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.platform.scanimporter.processor;
020
021import java.io.File;
022import java.io.FileFilter;
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
035import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
036import org.nuxeo.ecm.platform.importer.source.FileSourceNode;
037import org.nuxeo.ecm.platform.importer.source.SourceNode;
038import org.nuxeo.ecm.platform.scanimporter.service.ScanFileBlobHolder;
039import org.nuxeo.ecm.platform.scanimporter.service.ScannedFileMapperService;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * Custom implementation of {@link SourceNode}. Uses XML descriptors to get meta-data and binary files location.
044 *
045 * @author Thierry Delprat
046 */
047public class ScanedFileSourceNode extends FileSourceNode {
048
049    private static final Log log = LogFactory.getLog(ScanedFileSourceNode.class);
050
051    protected Map<String, Serializable> properties;
052
053    protected ScanFileBlobHolder bh;
054
055    public static boolean useXMLMapping = true;
056
057    public ScanedFileSourceNode(File file) {
058        super(file);
059    }
060
061    public ScanedFileSourceNode(File file, ScanFileBlobHolder bh) {
062        super(file);
063        this.bh = bh;
064    }
065
066    public ScanedFileSourceNode(File file, Map<String, Serializable> properties) {
067        super(file);
068        this.properties = properties;
069    }
070
071    @Override
072    public BlobHolder getBlobHolder() throws IOException {
073        if (bh == null) {
074            return new SimpleBlobHolder(Blobs.createBlob(file));
075        } else {
076            return bh;
077        }
078    }
079
080    @Override
081    public List<SourceNode> getChildren() throws IOException {
082        List<SourceNode> children = new ArrayList<SourceNode>();
083
084        ScannedFileMapperService sfms = Framework.getLocalService(ScannedFileMapperService.class);
085        for (File child : file.listFiles()) {
086            if (child.getName().endsWith(".xml") && useXMLMapping) {
087                try {
088
089                    ScanFileBlobHolder bh = sfms.parseMetaData(child);
090
091                    if (bh != null) {
092                        children.add(new ScanedFileSourceNode(child, bh));
093                    } else {
094                        log.error(child.getAbsolutePath() + " can not be parsed ");
095                    }
096                } catch (IOException e) {
097                    log.error("Error during properties parsing", e);
098                }
099            } else if (child.isDirectory()) {
100                if (child.listFiles(new DirectoryFilter()).length > 0) {
101                    children.add(new ScanedFileSourceNode(child));
102                } else {
103                    if (useXMLMapping) {
104                        if (child.list(new XmlMetaDataFileFilter()).length > 0) {
105                            children.add(new ScanedFileSourceNode(child));
106                        }
107                    } else {
108                        if (child.list().length > 0) {
109                            children.add(new ScanedFileSourceNode(child));
110                        }
111                    }
112                }
113            } else if (!useXMLMapping) {
114                ScanFileBlobHolder bh = new ScanFileBlobHolder(Blobs.createBlob(child), sfms.getTargetLeafType());
115                children.add(new ScanedFileSourceNode(child, bh));
116            }
117        }
118        return children;
119    }
120
121    @Override
122    public String getName() {
123        if (bh != null) {
124            Blob blob = bh.getBlob();
125            if (blob != null && blob.getFilename() != null) {
126                return blob.getFilename();
127            }
128        }
129        return file.getName();
130    }
131
132    public String getDescriptorFileName() {
133        return file.getAbsolutePath();
134    }
135
136    private class DirectoryFilter implements FileFilter {
137
138        @Override
139        public boolean accept(File file) {
140            return file.isDirectory();
141        }
142
143    }
144}