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 Library General Public License as published by
009  the Free Software Foundation; either version 2 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 Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library 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.virtual.VirtualFile;
027
028
029/**
030 * @author Franck WOLFF
031 */
032public class VFSFileScannedItem extends AbstractScannedItem {
033
034    private final VirtualFile root;
035    private final VirtualFile file;
036    
037    private String relativePath = null;
038
039    
040    public VFSFileScannedItem(Scanner scanner, VFSFileScannedItem 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        try {
049                return file.getSize();
050        }
051        catch (IOException e) {
052                throw new RuntimeException("Could not get size fo file " + file, e);
053        }
054    }
055
056    public InputStream getInputStream() throws IOException {
057        return file.openStream();
058    }
059
060    public String getName() {
061        return file.getName();
062    }
063    
064    public String getAbsolutePath() {
065        return file.getPathName();
066    }
067
068        public String getRelativePath() {
069                if (relativePath == null) {
070                        try {
071                        StringBuffer sb = new StringBuffer();
072                        for (VirtualFile f = file; f != null && !root.equals(f); f = f.getParent()) {
073                            if (sb.length() > 0)
074                                sb.insert(0, '/');
075                            sb.insert(0, f.getName());
076                        }
077                        relativePath = sb.toString();
078                        }
079                        catch (IOException e) {
080                                throw new RuntimeException("Could not get path for file " + file, e);
081                        }
082                }
083                return relativePath;
084        }
085
086        
087    @Override
088    public boolean equals(Object obj) {
089        if (obj == this)
090            return true;
091        if (!(obj instanceof VFSFileScannedItem))
092            return false;
093        return file.equals(((VFSFileScannedItem)obj).file) && root.equals(((VFSFileScannedItem)obj).root);
094    }
095
096    @Override
097    public int hashCode() {
098        return root.hashCode() + (31 * file.hashCode());
099    }
100}