001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    
020    package com.mucommander.file;
021    
022    /**
023     * This class represents a generic archive entry. It provides getters and setters for common archive entry attributes
024     * and allows to encapsulate the entry object of a 3rd party library.
025     *
026     * <p><b>Important</b>: the path of archive entries must use the '/' character as a path delimiter, and be relative
027     * to the archive's root, i.e. must not start with a leading '/'.</p>
028     *
029     * @author Maxence Bernard 
030     */
031    public class ArchiveEntry extends SimpleFileAttributes {
032    
033        /** Encapsulated entry object */
034        protected Object entryObject;
035    
036    
037        /**
038         * Creates a new ArchiveEntry with all attributes set to their default value.
039         */
040        public ArchiveEntry() {
041        }
042    
043        /**
044         * Creates a new ArchiveEntry using the supplied path and directory attributes.
045         *
046         * @param path the entry's path
047         * @param directory true if the entry is a directory
048         */
049        public ArchiveEntry(String path, boolean directory) {
050            setPath(path);
051            setDirectory(directory);
052        }
053    
054        /**
055         * Creates a new ArchiveEntry using the values of the supplied attributes.
056         *
057         * @param path the entry's path
058         * @param directory true if the entry is a directory
059         * @param date the entry's date
060         * @param size the entry's size
061         */
062        public ArchiveEntry(String path, boolean directory, long date, long size) {
063            setPath(path);
064            setDate(date);
065            setSize(size);
066            setDirectory(directory);
067        }
068    
069    
070        /**
071         * Returns the depth of this entry based on the number of path delimiters ('/') its path contains.
072         * Top-level entries have a depth of 0 (minimum depth).
073         *
074         * @return the depth of this entry
075         */
076        public int getDepth() {
077            return getDepth(getPath());
078        }
079    
080        /**
081         * Returns the depth of the specified entry path, based on the number of path delimiters ('/') it contains.
082         * Top-level entries have a depth of 0 (minimum depth).
083         *
084         * @param entryPath the path for which to calculate the depth
085         * @return the depth of the given entry path
086         */
087        public static int getDepth(String entryPath) {
088            int depth = 0;
089            int pos=0;
090    
091            while ((pos=entryPath.indexOf('/', pos+1))!=-1)
092                depth++;
093    
094            // Directories in archives end with a '/'
095            if(entryPath.charAt(entryPath.length()-1)=='/')
096                depth--;
097    
098            return depth;
099        }
100    
101        /**
102         * Extracts this entry's filename from its path and returns it.
103         *
104         * @return this entry's filename
105         */
106        public String getName() {
107            String path = getPath();
108            int len = path.length();
109            // Remove trailing '/' if any
110            if(path.charAt(len-1)=='/')
111                path = path.substring(0, --len);
112    
113            int lastSlash = path.lastIndexOf('/');
114            return lastSlash==-1?
115              path:
116              path.substring(lastSlash+1, len);
117        }
118    
119        /**
120         * Returns an archive format-dependent object providing extra information about this entry, typically an object from
121         * a 3rd party library ; <code>null</code> if this entry has none.
122         *
123         * @return an object providing extra information about this entry, null if this entry has none
124         */
125        public Object getEntryObject() {
126            return entryObject;
127        }
128    
129        /**
130         * Sets an archive format-dependent object providing extra information about this entry, typically an object from
131         * a 3rd party library ; <code>null</code> for none.
132         *
133         * @param entryObject an object providing extra information about this entry, null for none
134         */
135        public void setEntryObject(Object entryObject) {
136            this.entryObject = entryObject;
137        }
138    
139    
140        ////////////////////////
141        // Overridden methods //
142        ////////////////////////
143    
144        /**
145         * Returns the file permissions of this entry. This method is overridden to return default permissions
146         * ({@link FilePermissions#DEFAULT_DIRECTORY_PERMISSIONS} for directories, {@link FilePermissions#DEFAULT_FILE_PERMISSIONS}
147         * for regular files), when none have been set.
148         *
149         * @return the file permissions of this entry
150         */
151        public FilePermissions getPermissions() {
152            FilePermissions permissions = super.getPermissions();
153            if(permissions==null)
154                return isDirectory()?FilePermissions.DEFAULT_DIRECTORY_PERMISSIONS:FilePermissions.DEFAULT_FILE_PERMISSIONS;
155    
156            return permissions;
157        }
158    }