commit | author | age
|
6e70be
|
1 |
/* |
U |
2 |
WebBox - Dein Server. |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.filecms.data; |
|
20 |
|
|
21 |
import java.io.Serializable; |
|
22 |
|
|
23 |
/** |
|
24 |
* A reference to a file consisting of the file's absolute path and additional |
|
25 |
* information about whether or not the referenced file is a directory, is hidden, etc. |
|
26 |
* Note that FileRef only references a file, the file itself and its contents are not |
|
27 |
* modelled by this class. |
|
28 |
* |
|
29 |
* @author Ulrich Hilger, http://uhilger.de |
|
30 |
* @author Bereitgestellt unter den Bedingungen der |
|
31 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
32 |
* General Public License</a> |
|
33 |
* |
|
34 |
* @version 2, 12.01.2008 |
|
35 |
*/ |
|
36 |
public class FileRef implements Serializable { |
|
37 |
|
|
38 |
public static final long serialVersionUID = 42L; |
|
39 |
private String absolutePath; |
|
40 |
private Boolean isDirectory; |
|
41 |
private Boolean isHidden; |
|
42 |
private Long lastModified; |
|
43 |
private Long length; |
81abd5
|
44 |
private String mimetype = "text"; |
6e70be
|
45 |
|
U |
46 |
/** |
|
47 |
* create a new instance of <code>FileRef</code>. Note that the created FileRef is |
|
48 |
* only a reference to a file, the file itself will not be created. |
|
49 |
* |
|
50 |
* @param absolutePath the absolute path that denotes this instance of <code>FileRef</code> |
|
51 |
* @param isDirectory whether or not this file is a directory |
|
52 |
* @param isHidden whether or not this file is a hidden file |
|
53 |
* @param lastModified the date/time this file was last modified measured in milliseconds |
|
54 |
* since the epoch (00:00:00 GMT, January 1, 1970) |
|
55 |
* @param length the length of this file in bytes |
|
56 |
*/ |
|
57 |
public FileRef(String absolutePath, boolean isDirectory, boolean isHidden, |
|
58 |
long lastModified, long length) |
|
59 |
{ |
|
60 |
super(); |
|
61 |
this.absolutePath = absolutePath; |
|
62 |
this.isDirectory = isDirectory; |
|
63 |
this.isHidden = isHidden; |
|
64 |
this.lastModified = lastModified; |
|
65 |
this.length = length; |
81abd5
|
66 |
if(isDirectory()) { |
U |
67 |
this.mimetype = " "; |
|
68 |
} |
6e70be
|
69 |
} |
U |
70 |
|
|
71 |
/** |
|
72 |
* Create a new instance of class <code>FileRef</code> with a given absolute path. |
|
73 |
* Other characteristics of the file are created with default settings |
|
74 |
* (i.e. not a directory, not hidden, unknown modification date |
|
75 |
* and length). Note that the created FileRef is only a reference to a file, the file |
|
76 |
* itself will not be created. |
|
77 |
* |
|
78 |
* @param absolutePath the absolute path that denotes this instance |
|
79 |
* of <code>FileRef</code> |
|
80 |
*/ |
|
81 |
public FileRef(String absolutePath) { |
|
82 |
this(absolutePath, false, false, 0, 0); |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* Create a new instance of class <code>FileRef</code> with a given absolute path |
|
87 |
* and an indicator whether or not the new FileRef denotes a directory. |
|
88 |
* Other characteristics of the file are created with default settings |
|
89 |
* (i.e. not hidden, unknown modification date and length). Note that the created |
|
90 |
* FileRef is only a reference to a file, the file itself will not be created. |
|
91 |
* |
|
92 |
* @param absolutePath the absolute path that denotes this instance |
|
93 |
* of <code>FileRef</code> |
|
94 |
* @param isDirectory true, if the file to create should denote a directory |
|
95 |
*/ |
|
96 |
public FileRef(String absolutePath, boolean isDirectory) { |
|
97 |
this(absolutePath, isDirectory, false, 0, 0); |
|
98 |
} |
|
99 |
|
|
100 |
public String getMimetype() { |
|
101 |
return mimetype; |
|
102 |
} |
|
103 |
|
|
104 |
public void setMimetype(String mimetype) { |
81abd5
|
105 |
if(mimetype != null && mimetype.length() > 1) { |
U |
106 |
this.mimetype = mimetype; |
|
107 |
} |
6e70be
|
108 |
} |
U |
109 |
|
|
110 |
/** |
|
111 |
* get the absolute path that denotes this file |
|
112 |
* @return the path |
|
113 |
*/ |
|
114 |
public String getAbsolutePath() { |
|
115 |
return absolutePath; |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* Tests whether the file denoted by this abstract pathname is a |
|
120 |
* directory. |
|
121 |
* |
|
122 |
* @return <code>true</code> if this file is a directory, |
|
123 |
* <code>false</code> otherwise |
|
124 |
*/ |
|
125 |
public boolean isDirectory() { |
|
126 |
return isDirectory; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Tests whether the file denoted by this abstract pathname is a |
|
131 |
* file. |
|
132 |
* |
|
133 |
* @return <code>true</code> if this file is a file, |
|
134 |
* <code>false</code> otherwise |
|
135 |
*/ |
|
136 |
public boolean isFile() { |
|
137 |
return !isDirectory; |
|
138 |
} |
|
139 |
|
|
140 |
/** |
|
141 |
* Tests whether the file denoted by this abstract pathname is a |
|
142 |
* hidden file. |
|
143 |
* |
|
144 |
* @return <code>true</code> if this file is a hidden file, |
|
145 |
* <code>false</code> otherwise |
|
146 |
*/ |
|
147 |
public boolean isHidden() { |
|
148 |
return isHidden; |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* Returns the time that this file was |
|
153 |
* last modified. |
|
154 |
* |
|
155 |
* @return A <code>long</code> value representing the time the file was |
|
156 |
* last modified, measured in milliseconds since the epoch |
|
157 |
* (00:00:00 GMT, January 1, 1970) |
|
158 |
*/ |
|
159 |
public long getLastModified() { |
|
160 |
return lastModified; |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* Returns the length of this file. |
|
165 |
* The return value is unspecified if this file denotes a directory. |
|
166 |
* |
|
167 |
* @return The length, in bytes, of this file |
|
168 |
*/ |
|
169 |
public long getLength() { |
|
170 |
return length; |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* get the name of this file without path |
|
175 |
* @param separatorChar the string that is used to separate directories and file names in |
|
176 |
* path expressions |
|
177 |
* @return the name of this file without path |
|
178 |
*/ |
|
179 |
public String getName(String separatorChar) { |
|
180 |
String path = getAbsolutePath(); |
|
181 |
String name = path.substring(path.lastIndexOf(separatorChar) + 1); |
|
182 |
if(name == null || name.length() < 1) { |
|
183 |
name = path; |
|
184 |
} |
|
185 |
return name; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* get a string representation of this instance of FileRef |
|
190 |
* @return the string representation of this FileRef object |
|
191 |
*/ |
|
192 |
public String toString() { |
|
193 |
return getAbsolutePath(); |
|
194 |
} |
|
195 |
|
|
196 |
/** |
|
197 |
* Indicates whether some object is equal to this instance of class FileRef. Two |
|
198 |
* GenericFiles are regarded as equal when their absolute paths are equal case independently. |
|
199 |
* |
|
200 |
* @param obj the object to compare with this instance of FileRef |
|
201 |
* @return true, if obj is equal to this instance of FileRef, false if not |
|
202 |
*/ |
|
203 |
public boolean equals(Object obj) { |
|
204 |
boolean isEqual = false; |
|
205 |
if(obj != null && obj instanceof FileRef) { |
|
206 |
isEqual = ((FileRef) obj).getAbsolutePath().toLowerCase().equals( |
|
207 |
this.getAbsolutePath().toLowerCase()); |
|
208 |
} |
|
209 |
return isEqual; |
|
210 |
} |
|
211 |
} |