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