1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.types;
  18. /**
  19. * describes a File or a ZipEntry
  20. *
  21. * this class is meant to be used by classes needing to record path
  22. * and date/time information about a file, a zip entry or some similar
  23. * resource (URL, archive in a version control repository, ...)
  24. *
  25. * @since Ant 1.5.2
  26. */
  27. public class Resource implements Cloneable, Comparable {
  28. private String name = null;
  29. private boolean exists = true;
  30. private long lastmodified = 0;
  31. private boolean directory = false;
  32. /**
  33. * default constructor
  34. */
  35. public Resource() {
  36. }
  37. /**
  38. * only sets the name.
  39. *
  40. * <p>This is a dummy, used for not existing resources.</p>
  41. *
  42. * @param name relative path of the resource. Expects
  43. * "/" to be used as the directory separator.
  44. */
  45. public Resource(String name) {
  46. this(name, false, 0, false);
  47. }
  48. /**
  49. * sets the name, lastmodified flag, and exists flag
  50. *
  51. * @param name relative path of the resource. Expects
  52. * "/" to be used as the directory separator.
  53. */
  54. public Resource(String name, boolean exists, long lastmodified) {
  55. this(name, exists, lastmodified, false);
  56. }
  57. /**
  58. * @param name relative path of the resource. Expects
  59. * "/" to be used as the directory separator.
  60. */
  61. public Resource(String name, boolean exists, long lastmodified,
  62. boolean directory) {
  63. this.name = name;
  64. this.exists = exists;
  65. this.lastmodified = lastmodified;
  66. this.directory = directory;
  67. }
  68. /**
  69. * name attribute will contain the path of a file relative to the
  70. * root directory of its fileset or the recorded path of a zip
  71. * entry.
  72. *
  73. * <p>example for a file with fullpath /var/opt/adm/resource.txt
  74. * in a file set with root dir /var/opt it will be
  75. * adm/resource.txt.</p>
  76. *
  77. * <p>"/" will be used as the directory separator.</p>
  78. */
  79. public String getName() {
  80. return name;
  81. }
  82. /**
  83. * @param name relative path of the resource. Expects
  84. * "/" to be used as the directory separator.
  85. */
  86. public void setName(String name) {
  87. this.name = name;
  88. }
  89. /**
  90. * the exists attribute tells whether a file exists
  91. */
  92. public boolean isExists() {
  93. return exists;
  94. }
  95. public void setExists(boolean exists) {
  96. this.exists = exists;
  97. }
  98. /**
  99. * tells the modification time in milliseconds since 01.01.1970 of
  100. *
  101. * @return 0 if the resource does not exist to mirror the behavior
  102. * of {@link java.io.File File}.
  103. */
  104. public long getLastModified() {
  105. return !exists || lastmodified < 0 ? 0 : lastmodified;
  106. }
  107. public void setLastModified(long lastmodified) {
  108. this.lastmodified = lastmodified;
  109. }
  110. /**
  111. * tells if the resource is a directory
  112. * @return boolean flag indicating if the resource is a directory
  113. */
  114. public boolean isDirectory() {
  115. return directory;
  116. }
  117. public void setDirectory(boolean directory) {
  118. this.directory = directory;
  119. }
  120. /**
  121. * @return copy of this
  122. */
  123. public Object clone() {
  124. try {
  125. return super.clone();
  126. } catch (CloneNotSupportedException e) {
  127. throw new Error("CloneNotSupportedException for a "
  128. + "Clonable Resource caught?");
  129. }
  130. }
  131. /**
  132. * delegates to a comparison of names.
  133. *
  134. * @since Ant 1.6
  135. */
  136. public int compareTo(Object other) {
  137. if (!(other instanceof Resource)) {
  138. throw new IllegalArgumentException("Can only be compared with "
  139. + "Resources");
  140. }
  141. Resource r = (Resource) other;
  142. return getName().compareTo(r.getName());
  143. }
  144. }