You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseFile.java 2.6KB

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