001/*
002 * (C) Copyright 2006-2008 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 */
019
020package org.nuxeo.ecm.platform.importer.source;
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties;
032import org.nuxeo.ecm.platform.importer.properties.MetadataCollector;
033
034/**
035 * {@link SourceNode} implementation that manages Meta-data from properties files
036 *
037 * @author Thierry Delprat
038 */
039public class FileWithMetadataSourceNode extends FileSourceNode {
040
041    protected static MetadataCollector collector = new MetadataCollector();
042
043    public static String METADATA_FILENAME = "metadata.properties";
044
045    private static final Log log = LogFactory.getLog(FileWithMetadataSourceNode.class);
046
047    public FileWithMetadataSourceNode(File file) {
048        super(file);
049    }
050
051    public FileWithMetadataSourceNode(String path) {
052        super(path);
053    }
054
055    @Override
056    public BlobHolder getBlobHolder() throws IOException {
057        BlobHolder bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), collector.getProperties(file.getPath()));
058        return bh;
059    }
060
061    @Override
062    public List<SourceNode> getChildren() {
063
064        List<SourceNode> children = new ArrayList<SourceNode>();
065
066        for (File child : file.listFiles()) {
067            if (METADATA_FILENAME.equals(child.getName())) {
068                try {
069                    collector.addPropertyFile(child);
070                } catch (IOException e) {
071                    log.error("Error during properties parsing", e);
072                }
073                break;
074            }
075        }
076
077        for (File child : file.listFiles()) {
078            if (!METADATA_FILENAME.equals(child.getName())) {
079                children.add(new FileWithMetadataSourceNode(child));
080            }
081        }
082        return children;
083    }
084
085}