001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.scan;
023
024import java.io.IOException;
025import java.io.InputStream;
026
027import org.jboss.vfs.VirtualFile;
028
029
030/**
031 * @author Franck WOLFF
032 */
033public class VFS3FileScannedItem extends AbstractScannedItem {
034
035    private final VirtualFile root;
036    private final VirtualFile file;
037    
038    private String relativePath = null;
039
040    
041    public VFS3FileScannedItem(Scanner scanner, VFS3FileScannedItem marker, VirtualFile root, VirtualFile file) {
042        super(scanner, marker);
043
044        this.root = root;
045        this.file = file;
046    }
047
048    public long getSize() {
049                return file.getSize();
050    }
051
052    public InputStream getInputStream() throws IOException {
053        return file.openStream();
054    }
055
056    public String getName() {
057        return file.getName();
058    }
059    
060    public String getAbsolutePath() {
061        return file.getPathName();
062    }
063
064        public String getRelativePath() {
065                if (relativePath == null) {
066                StringBuffer sb = new StringBuffer();
067                for (VirtualFile f = file; f != null && !root.equals(f); f = f.getParent()) {
068                    if (sb.length() > 0)
069                        sb.insert(0, '/');
070                    sb.insert(0, f.getName());
071                }
072                relativePath = sb.toString();
073                }
074                return relativePath;
075        }
076
077        
078    @Override
079    public boolean equals(Object obj) {
080        if (obj == this)
081            return true;
082        if (!(obj instanceof VFS3FileScannedItem))
083            return false;
084        return file.equals(((VFS3FileScannedItem)obj).file) && root.equals(((VFS3FileScannedItem)obj).root);
085    }
086
087    @Override
088    public int hashCode() {
089        return root.hashCode() + (31 * file.hashCode());
090    }
091}