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.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
032
033/**
034 * Simple Filesystem based {@link SourceNode}
035 *
036 * @author Thierry Delprat
037 */
038public class FileSourceNode implements SourceNode {
039
040    protected File file;
041
042    public FileSourceNode(File file) {
043        this.file = file;
044    }
045
046    public FileSourceNode(String path) {
047        this.file = new File(path);
048    }
049
050    @Override
051    public BlobHolder getBlobHolder() throws IOException {
052        return new SimpleBlobHolder(Blobs.createBlob(file));
053    }
054
055    @Override
056    public List<SourceNode> getChildren() throws IOException {
057
058        List<SourceNode> children = new ArrayList<>();
059
060        for (File child : file.listFiles()) {
061            children.add(new FileSourceNode(child));
062        }
063        return children;
064    }
065
066    @Override
067    public boolean isFolderish() {
068        return file.isDirectory();
069    }
070
071    @Override
072    public String getName() {
073        return file.getName();
074    }
075
076    @Override
077    public String getSourcePath() {
078        return file.getAbsolutePath();
079    }
080
081    public File getFile() {
082        return file;
083    }
084
085    public static String getFileNameNoExt(File file) {
086        String name = file.getName();
087        int p = name.lastIndexOf('.');
088        if (p == -1) {
089            return name;
090        }
091        return name.substring(0, p);
092    }
093
094}