001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp;
017:
018: import java.io.Serializable;
019: import java.util.Calendar;
020:
021: /***
022: * The FTPFile class is used to represent information about files stored
023: * on an FTP server. Because there is no standard representation for
024: * file information on FTP servers, it may not always be possible to
025: * extract all the information that can be represented by FTPFile, or
026: * it may even be possible to extract more information. In cases where
027: * more information can be extracted, you will want to subclass FTPFile
028: * and implement your own {@link org.apache.commons.net.ftp.FTPFileListParser}
029: * to extract the information.
030: * However, most FTP servers return file information in a format that
031: * can be completely parsed by
032: * {@link org.apache.commons.net.ftp.DefaultFTPFileListParser}
033: * and stored in FTPFile.
034: * <p>
035: * <p>
036: * @author Daniel F. Savarese
037: * @see FTPFileListParser
038: * @see DefaultFTPFileListParser
039: * @see FTPClient#listFiles
040: ***/
041:
042: public class FTPFile implements Serializable {
043: /** A constant indicating an FTPFile is a file. ***/
044: public static final int FILE_TYPE = 0;
045: /** A constant indicating an FTPFile is a directory. ***/
046: public static final int DIRECTORY_TYPE = 1;
047: /** A constant indicating an FTPFile is a symbolic link. ***/
048: public static final int SYMBOLIC_LINK_TYPE = 2;
049: /** A constant indicating an FTPFile is of unknown type. ***/
050: public static final int UNKNOWN_TYPE = 3;
051:
052: /** A constant indicating user access permissions. ***/
053: public static final int USER_ACCESS = 0;
054: /** A constant indicating group access permissions. ***/
055: public static final int GROUP_ACCESS = 1;
056: /** A constant indicating world access permissions. ***/
057: public static final int WORLD_ACCESS = 2;
058:
059: /** A constant indicating file/directory read permission. ***/
060: public static final int READ_PERMISSION = 0;
061: /** A constant indicating file/directory write permission. ***/
062: public static final int WRITE_PERMISSION = 1;
063: /**
064: * A constant indicating file execute permission or directory listing
065: * permission.
066: ***/
067: public static final int EXECUTE_PERMISSION = 2;
068:
069: int _type, _hardLinkCount;
070: long _size;
071: String _rawListing, _user, _group, _name, _link;
072: Calendar _date;
073: boolean[] _permissions[];
074:
075: /*** Creates an empty FTPFile. ***/
076: public FTPFile() {
077: _permissions = new boolean[3][3];
078: _rawListing = null;
079: _type = UNKNOWN_TYPE;
080: _hardLinkCount = 0;
081: _size = 0;
082: _user = null;
083: _group = null;
084: _date = null;
085: _name = null;
086: }
087:
088: /***
089: * Set the original FTP server raw listing from which the FTPFile was
090: * created.
091: * <p>
092: * @param rawListing The raw FTP server listing.
093: ***/
094: public void setRawListing(String rawListing) {
095: _rawListing = rawListing;
096: }
097:
098: /***
099: * Get the original FTP server raw listing used to initialize the FTPFile.
100: * <p>
101: * @return The original FTP server raw listing used to initialize the
102: * FTPFile.
103: ***/
104: public String getRawListing() {
105: return _rawListing;
106: }
107:
108: /***
109: * Determine if the file is a directory.
110: * <p>
111: * @return True if the file is of type <code>DIRECTORY_TYPE</code>, false if
112: * not.
113: ***/
114: public boolean isDirectory() {
115: return (_type == DIRECTORY_TYPE);
116: }
117:
118: /***
119: * Determine if the file is a regular file.
120: * <p>
121: * @return True if the file is of type <code>FILE_TYPE</code>, false if
122: * not.
123: ***/
124: public boolean isFile() {
125: return (_type == FILE_TYPE);
126: }
127:
128: /***
129: * Determine if the file is a symbolic link.
130: * <p>
131: * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
132: * not.
133: ***/
134: public boolean isSymbolicLink() {
135: return (_type == SYMBOLIC_LINK_TYPE);
136: }
137:
138: /***
139: * Determine if the type of the file is unknown.
140: * <p>
141: * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
142: * not.
143: ***/
144: public boolean isUnknown() {
145: return (_type == UNKNOWN_TYPE);
146: }
147:
148: /***
149: * Set the type of the file (<code>DIRECTORY_TYPE</code>,
150: * <code>FILE_TYPE</code>, etc.).
151: * <p>
152: * @param type The integer code representing the type of the file.
153: ***/
154: public void setType(int type) {
155: _type = type;
156: }
157:
158: /***
159: * Return the type of the file (one of the <code>_TYPE</code> constants),
160: * e.g., if it is a directory, a regular file, or a symbolic link.
161: * <p>
162: * @return The type of the file.
163: ***/
164: public int getType() {
165: return _type;
166: }
167:
168: /***
169: * Set the name of the file.
170: * <p>
171: * @param name The name of the file.
172: ***/
173: public void setName(String name) {
174: _name = name;
175: }
176:
177: /***
178: * Return the name of the file.
179: * <p>
180: * @return The name of the file.
181: ***/
182: public String getName() {
183: return _name;
184: }
185:
186: /**
187: * Set the file size in bytes.
188: * @param size The file size in bytes.
189: */
190: public void setSize(long size) {
191: _size = size;
192: }
193:
194: /***
195: * Return the file size in bytes.
196: * <p>
197: * @return The file size in bytes.
198: ***/
199: public long getSize() {
200: return _size;
201: }
202:
203: /***
204: * Set the number of hard links to this file. This is not to be
205: * confused with symbolic links.
206: * <p>
207: * @param links The number of hard links to this file.
208: ***/
209: public void setHardLinkCount(int links) {
210: _hardLinkCount = links;
211: }
212:
213: /***
214: * Return the number of hard links to this file. This is not to be
215: * confused with symbolic links.
216: * <p>
217: * @return The number of hard links to this file.
218: ***/
219: public int getHardLinkCount() {
220: return _hardLinkCount;
221: }
222:
223: /***
224: * Set the name of the group owning the file. This may be
225: * a string representation of the group number.
226: * <p>
227: * @param group The name of the group owning the file.
228: ***/
229: public void setGroup(String group) {
230: _group = group;
231: }
232:
233: /***
234: * Returns the name of the group owning the file. Sometimes this will be
235: * a string representation of the group number.
236: * <p>
237: * @return The name of the group owning the file.
238: ***/
239: public String getGroup() {
240: return _group;
241: }
242:
243: /***
244: * Set the name of the user owning the file. This may be
245: * a string representation of the user number;
246: * <p>
247: * @param user The name of the user owning the file.
248: ***/
249: public void setUser(String user) {
250: _user = user;
251: }
252:
253: /***
254: * Returns the name of the user owning the file. Sometimes this will be
255: * a string representation of the user number.
256: * <p>
257: * @return The name of the user owning the file.
258: ***/
259: public String getUser() {
260: return _user;
261: }
262:
263: /***
264: * If the FTPFile is a symbolic link, use this method to set the name of the
265: * file being pointed to by the symbolic link.
266: * <p>
267: * @param link The file pointed to by the symbolic link.
268: ***/
269: public void setLink(String link) {
270: _link = link;
271: }
272:
273: /***
274: * If the FTPFile is a symbolic link, this method returns the name of the
275: * file being pointed to by the symbolic link. Otherwise it returns null.
276: * <p>
277: * @return The file pointed to by the symbolic link (null if the FTPFile
278: * is not a symbolic link).
279: ***/
280: public String getLink() {
281: return _link;
282: }
283:
284: /***
285: * Set the file timestamp. This usually the last modification time.
286: * The parameter is not cloned, so do not alter its value after calling
287: * this method.
288: * <p>
289: * @param date A Calendar instance representing the file timestamp.
290: ***/
291: public void setTimestamp(Calendar date) {
292: _date = date;
293: }
294:
295: /***
296: * Returns the file timestamp. This usually the last modification time.
297: * <p>
298: * @return A Calendar instance representing the file timestamp.
299: ***/
300: public Calendar getTimestamp() {
301: return _date;
302: }
303:
304: /***
305: * Set if the given access group (one of the <code> _ACCESS </code>
306: * constants) has the given access permission (one of the
307: * <code> _PERMISSION </code> constants) to the file.
308: * <p>
309: * @param access The access group (one of the <code> _ACCESS </code>
310: * constants)
311: * @param permission The access permission (one of the
312: * <code> _PERMISSION </code> constants)
313: * @param value True if permission is allowed, false if not.
314: ***/
315: public void setPermission(int access, int permission, boolean value) {
316: _permissions[access][permission] = value;
317: }
318:
319: /***
320: * Determines if the given access group (one of the <code> _ACCESS </code>
321: * constants) has the given access permission (one of the
322: * <code> _PERMISSION </code> constants) to the file.
323: * <p>
324: * @param access The access group (one of the <code> _ACCESS </code>
325: * constants)
326: * @param permission The access permission (one of the
327: * <code> _PERMISSION </code> constants)
328: ***/
329: public boolean hasPermission(int access, int permission) {
330: return _permissions[access][permission];
331: }
332:
333: /***
334: * Returns a string representation of the FTPFile information. This
335: * will be the raw FTP server listing that was used to initialize the
336: * FTPFile instance.
337: * <p>
338: * @return A string representation of the FTPFile information.
339: ***/
340: public String toString() {
341: return _rawListing;
342: }
343:
344: }
|