1 | /*
|
---|
2 | * Copyright 2003-2005 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 | /**
|
---|
20 | * Describes a File or a ZipEntry.
|
---|
21 | *
|
---|
22 | * This class is meant to be used by classes needing to record path
|
---|
23 | * and date/time information about a file, a zip entry or some similar
|
---|
24 | * resource (URL, archive in a version control repository, ...).
|
---|
25 | *
|
---|
26 | * @since Ant 1.5.2
|
---|
27 | */
|
---|
28 | public class Resource implements Cloneable, Comparable {
|
---|
29 | /** Constant unknown size */
|
---|
30 | public static final long UNKNOWN_SIZE = -1;
|
---|
31 |
|
---|
32 | private String name = null;
|
---|
33 | private boolean exists = true;
|
---|
34 | private long lastmodified = 0;
|
---|
35 | private boolean directory = false;
|
---|
36 | private long size = UNKNOWN_SIZE;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Default constructor.
|
---|
40 | */
|
---|
41 | public Resource() {
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Only sets the name.
|
---|
46 | *
|
---|
47 | * <p>This is a dummy, used for not existing resources.</p>
|
---|
48 | *
|
---|
49 | * @param name relative path of the resource. Expects
|
---|
50 | * "/" to be used as the directory separator.
|
---|
51 | */
|
---|
52 | public Resource(String name) {
|
---|
53 | this(name, false, 0, false);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Sets the name, lastmodified flag, and exists flag.
|
---|
58 | *
|
---|
59 | * @param name relative path of the resource. Expects
|
---|
60 | * "/" to be used as the directory separator.
|
---|
61 | * @param exists if true, this resource exists.
|
---|
62 | * @param lastmodified the last modification time of this resource.
|
---|
63 | */
|
---|
64 | public Resource(String name, boolean exists, long lastmodified) {
|
---|
65 | this(name, exists, lastmodified, false);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Sets the name, lastmodified flag, exists flag, and directory flag.
|
---|
70 | *
|
---|
71 | * @param name relative path of the resource. Expects
|
---|
72 | * "/" to be used as the directory separator.
|
---|
73 | * @param exists if true the resource exists
|
---|
74 | * @param lastmodified the last modification time of the resource
|
---|
75 | * @param directory if true, this resource is a directory
|
---|
76 | */
|
---|
77 | public Resource(String name, boolean exists, long lastmodified,
|
---|
78 | boolean directory) {
|
---|
79 | this(name, exists, lastmodified, directory, UNKNOWN_SIZE);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Sets the name, lastmodified flag, exists flag, directory flag, and size.
|
---|
84 | *
|
---|
85 | * @param name relative path of the resource. Expects
|
---|
86 | * "/" to be used as the directory separator.
|
---|
87 | * @param exists if true the resource exists
|
---|
88 | * @param lastmodified the last modification time of the resource
|
---|
89 | * @param directory if true, this resource is a directory
|
---|
90 | * @param size the size of this resource.
|
---|
91 | */
|
---|
92 | public Resource(String name, boolean exists, long lastmodified,
|
---|
93 | boolean directory, long size) {
|
---|
94 | this.name = name;
|
---|
95 | setName(name);
|
---|
96 | setExists(exists);
|
---|
97 | setLastModified(lastmodified);
|
---|
98 | setDirectory(directory);
|
---|
99 | setSize(size);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Name attribute will contain the path of a file relative to the
|
---|
104 | * root directory of its fileset or the recorded path of a zip
|
---|
105 | * entry.
|
---|
106 | *
|
---|
107 | * <p>example for a file with fullpath /var/opt/adm/resource.txt
|
---|
108 | * in a file set with root dir /var/opt it will be
|
---|
109 | * adm/resource.txt.</p>
|
---|
110 | *
|
---|
111 | * <p>"/" will be used as the directory separator.</p>
|
---|
112 | * @return the name of this resource.
|
---|
113 | */
|
---|
114 | public String getName() {
|
---|
115 | return name;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Set the name of this Resource.
|
---|
120 | * @param name relative path of the resource. Expects
|
---|
121 | * "/" to be used as the directory separator.
|
---|
122 | */
|
---|
123 | public void setName(String name) {
|
---|
124 | this.name = name;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * The exists attribute tells whether a file exists.
|
---|
129 | * @return true if this resource exists.
|
---|
130 | */
|
---|
131 | public boolean isExists() {
|
---|
132 | return exists;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Set the exists attribute.
|
---|
137 | * @param exists if true, this resource exists.
|
---|
138 | */
|
---|
139 | public void setExists(boolean exists) {
|
---|
140 | this.exists = exists;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Tells the modification time in milliseconds since 01.01.1970 .
|
---|
145 | *
|
---|
146 | * @return 0 if the resource does not exist to mirror the behavior
|
---|
147 | * of {@link java.io.File File}.
|
---|
148 | */
|
---|
149 | public long getLastModified() {
|
---|
150 | return !exists || lastmodified < 0 ? 0L : lastmodified;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Set the last modification attribute.
|
---|
155 | * @param lastmodified the modification time in milliseconds since 01.01.1970.
|
---|
156 | */
|
---|
157 | public void setLastModified(long lastmodified) {
|
---|
158 | this.lastmodified = lastmodified;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Tells if the resource is a directory.
|
---|
163 | * @return boolean flag indicating if the resource is a directory.
|
---|
164 | */
|
---|
165 | public boolean isDirectory() {
|
---|
166 | return directory;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Set the directory attribute.
|
---|
171 | * @param directory if true, this resource is a directory.
|
---|
172 | */
|
---|
173 | public void setDirectory(boolean directory) {
|
---|
174 | this.directory = directory;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Set the size of this Resource.
|
---|
179 | * @param size the size, as a long.
|
---|
180 | * @since Ant 1.6.3
|
---|
181 | */
|
---|
182 | public void setSize(long size) {
|
---|
183 | this.size = (size > UNKNOWN_SIZE) ? size : UNKNOWN_SIZE;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Get the size of this Resource.
|
---|
188 | * @return the size, as a long, 0 if the Resource does not exist (for
|
---|
189 | * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
|
---|
190 | * @since Ant 1.6.3
|
---|
191 | */
|
---|
192 | public long getSize() {
|
---|
193 | return (exists) ? size : 0L;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Clone this Resource.
|
---|
198 | * @return copy of this.
|
---|
199 | */
|
---|
200 | public Object clone() {
|
---|
201 | try {
|
---|
202 | return super.clone();
|
---|
203 | } catch (CloneNotSupportedException e) {
|
---|
204 | throw new Error("CloneNotSupportedException for a "
|
---|
205 | + "Clonable Resource caught?");
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Delegates to a comparison of names.
|
---|
211 | * @param other the object to compare to.
|
---|
212 | * @return a negative integer, zero, or a positive integer as this Resource
|
---|
213 | * is less than, equal to, or greater than the specified Resource.
|
---|
214 | * @since Ant 1.6
|
---|
215 | */
|
---|
216 | public int compareTo(Object other) {
|
---|
217 | if (!(other instanceof Resource)) {
|
---|
218 | throw new IllegalArgumentException("Can only be compared with "
|
---|
219 | + "Resources");
|
---|
220 | }
|
---|
221 | Resource r = (Resource) other;
|
---|
222 | return getName().compareTo(r.getName());
|
---|
223 | }
|
---|
224 |
|
---|
225 | }
|
---|