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    public BlobHolder getBlobHolder() throws IOException {
051        return new SimpleBlobHolder(Blobs.createBlob(file));
052    }
053
054    public List<SourceNode> getChildren() throws IOException {
055
056        List<SourceNode> children = new ArrayList<SourceNode>();
057
058        for (File child : file.listFiles()) {
059            children.add(new FileSourceNode(child));
060        }
061        return children;
062    }
063
064    public boolean isFolderish() {
065        return file.isDirectory();
066    }
067
068    public String getName() {
069        return file.getName();
070    }
071
072    public String getSourcePath() {
073        return file.getAbsolutePath();
074    }
075
076    public File getFile() {
077        return file;
078    }
079
080    public static String getFileNameNoExt(File file) {
081        String name = file.getName();
082        int p = name.lastIndexOf('.');
083        if (p == -1) {
084            return name;
085        }
086        return name.substring(0, p);
087    }
088
089}