001    package org.apache.archiva.common.utils;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.net.URI;
024    
025    /**
026     * BaseFile - convenient File object that tracks the Base Directory and can provide relative path values
027     * for the file object based on that Base Directory value.
028     *
029     *
030     */
031    public class BaseFile
032        extends File
033    {
034        private File baseDir;
035    
036        public BaseFile( File pathFile )
037        {
038            this( pathFile.getAbsolutePath() );
039        }
040    
041        public BaseFile( File repoDir, File pathFile )
042        {
043            this( repoDir, PathUtil.getRelative( repoDir.getAbsolutePath(), pathFile ) );
044        }
045    
046        public BaseFile( File parent, String child )
047        {
048            super( parent, child );
049            this.baseDir = parent;
050        }
051    
052        public BaseFile( String pathname )
053        {
054            super( pathname );
055    
056            // Calculate the top level directory.
057    
058            File parent = this;
059            while ( parent.getParentFile() != null )
060            {
061                parent = parent.getParentFile();
062            }
063    
064            this.baseDir = parent;
065        }
066    
067        public BaseFile( String repoDir, File pathFile )
068        {
069            this( new File( repoDir ), pathFile );
070        }
071    
072        public BaseFile( String parent, String child )
073        {
074            super( parent, child );
075            this.baseDir = new File( parent );
076        }
077    
078        public BaseFile( URI uri )
079        {
080            super( uri ); // only to satisfy java compiler.
081            throw new IllegalStateException(
082                "The " + BaseFile.class.getName() + " object does not support URI construction." );
083        }
084    
085        public File getBaseDir()
086        {
087            return baseDir;
088        }
089    
090        public String getRelativePath()
091        {
092            return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this );
093        }
094    
095        public void setBaseDir( File baseDir )
096        {
097            this.baseDir = baseDir;
098        }
099    
100        public void setBaseDir( String repoDir )
101        {
102            setBaseDir( new File( repoDir ) );
103        }
104    }