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     */
022    package org.granite.scan;
023    
024    import java.io.IOException;
025    import java.io.InputStream;
026    
027    import org.jboss.virtual.VirtualFile;
028    
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public class VFSFileScannedItem extends AbstractScannedItem {
034    
035        private final VirtualFile root;
036        private final VirtualFile file;
037        
038        private String relativePath = null;
039    
040        
041        public VFSFileScannedItem(Scanner scanner, VFSFileScannedItem 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            try {
050                    return file.getSize();
051            }
052            catch (IOException e) {
053                    throw new RuntimeException("Could not get size fo file " + file, e);
054            }
055        }
056    
057        public InputStream getInputStream() throws IOException {
058            return file.openStream();
059        }
060    
061        public String getName() {
062            return file.getName();
063        }
064        
065        public String getAbsolutePath() {
066            return file.getPathName();
067        }
068    
069            public String getRelativePath() {
070                    if (relativePath == null) {
071                            try {
072                            StringBuffer sb = new StringBuffer();
073                            for (VirtualFile f = file; f != null && !root.equals(f); f = f.getParent()) {
074                                if (sb.length() > 0)
075                                    sb.insert(0, '/');
076                                sb.insert(0, f.getName());
077                            }
078                            relativePath = sb.toString();
079                            }
080                            catch (IOException e) {
081                                    throw new RuntimeException("Could not get path for file " + file, e);
082                            }
083                    }
084                    return relativePath;
085            }
086    
087            
088        @Override
089        public boolean equals(Object obj) {
090            if (obj == this)
091                return true;
092            if (!(obj instanceof VFSFileScannedItem))
093                return false;
094            return file.equals(((VFSFileScannedItem)obj).file) && root.equals(((VFSFileScannedItem)obj).root);
095        }
096    
097        @Override
098        public int hashCode() {
099            return root.hashCode() + (31 * file.hashCode());
100        }
101    }