This project has retired. For details please refer to its Attic page.
BaseFile xref
View Javadoc
1   package org.apache.archiva.common.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.net.URI;
24  
25  /**
26   * BaseFile - convenient File object that tracks the Base Directory and can provide relative path values
27   * for the file object based on that Base Directory value.
28   *
29   *
30   */
31  public class BaseFile
32      extends File
33  {
34      private File baseDir;
35  
36      public BaseFile( File pathFile )
37      {
38          this( pathFile.getAbsolutePath() );
39      }
40  
41      public BaseFile( File repoDir, File pathFile )
42      {
43          this( repoDir, PathUtil.getRelative( repoDir.getAbsolutePath(), pathFile ) );
44      }
45  
46      public BaseFile( File parent, String child )
47      {
48          super( parent, child );
49          this.baseDir = parent;
50      }
51  
52      public BaseFile( String pathname )
53      {
54          super( pathname );
55  
56          // Calculate the top level directory.
57  
58          File parent = this;
59          while ( parent.getParentFile() != null )
60          {
61              parent = parent.getParentFile();
62          }
63  
64          this.baseDir = parent;
65      }
66  
67      public BaseFile( String repoDir, File pathFile )
68      {
69          this( new File( repoDir ), pathFile );
70      }
71  
72      public BaseFile( String parent, String child )
73      {
74          super( parent, child );
75          this.baseDir = new File( parent );
76      }
77  
78      public BaseFile( URI uri )
79      {
80          super( uri ); // only to satisfy java compiler.
81          throw new IllegalStateException(
82              "The " + BaseFile.class.getName() + " object does not support URI construction." );
83      }
84  
85      public File getBaseDir()
86      {
87          return baseDir;
88      }
89  
90      public String getRelativePath()
91      {
92          return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this );
93      }
94  
95      public void setBaseDir( File baseDir )
96      {
97          this.baseDir = baseDir;
98      }
99  
100     public void setBaseDir( String repoDir )
101     {
102         setBaseDir( new File( repoDir ) );
103     }
104 }