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