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