2 * jSite - FileOption.java - Copyright © 2006–2011 David Roden
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
22 * Container for various file options.
24 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26 public class FileOption {
28 /** The default for the insert state. */
29 private static final boolean DEFAULT_INSERT = true;
31 /** The default for the insert redirect state. */
32 private static final boolean DEFAULT_INSERT_REDIRECT = true;
34 /** The default for the custom key. */
35 private static final String DEFAULT_CUSTOM_KEY = "CHK@";
37 /** The default changed name. */
38 private static final String DEFAULT_CHANGED_NAME = null;
40 /** The insert state. */
41 private boolean insert;
43 /** Whether to force an insert. */
44 private boolean forceInsert;
46 /** Whether to insert a redirect. */
47 private boolean insertRedirect;
49 /** The hash of the last insert. */
50 private String lastInsertHash;
52 /** The edition of the last insert. */
53 private int lastInsertEdition;
55 /** The current hash of the file. */
56 private String currentHash;
58 /** The custom key. */
59 private String customKey;
61 /** The changed name. */
62 private String changedName;
64 /** The default MIME type. */
65 private final String defaultMimeType;
67 /** The current MIME type. */
68 private String mimeType;
71 * Creates new file options.
73 * @param defaultMimeType
74 * The default MIME type of the file
76 public FileOption(String defaultMimeType) {
77 insert = DEFAULT_INSERT;
78 insertRedirect = DEFAULT_INSERT_REDIRECT;
79 customKey = DEFAULT_CUSTOM_KEY;
80 changedName = DEFAULT_CHANGED_NAME;
81 this.defaultMimeType = defaultMimeType;
82 mimeType = defaultMimeType;
86 * Returns the custom key. The custom key is only used when
87 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
90 * @return The custom key
92 public String getCustomKey() {
97 * Sets the custom key. The custom key is only used when {@link #isInsert()}
98 * and {@link #isInsertRedirect()} both return {@code true}.
103 public void setCustomKey(String customKey) {
104 if (customKey == null) {
107 this.customKey = customKey;
112 * Returns whether the file should be inserted. If a file is not inserted
113 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
114 * be inserted at all.
116 * @see #setCustomKey(String)
117 * @return <code>true</code> if the file should be inserted,
118 * <code>false</code> otherwise
120 public boolean isInsert() {
125 * Sets whether the file should be inserted. If a file is not inserted and
126 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
130 * <code>true</code> if the file should be inserted,
131 * <code>false</code> otherwise
133 public void setInsert(boolean insert) {
134 this.insert = insert;
138 * Returns whether the insert of this file should be forced, even if its
139 * current hash matches the last insert hash.
141 * @return {@code true} to force the insert of this file, {@code false}
144 public boolean isForceInsert() {
149 * Sets whether to force the insert of this file, even if its current hash
150 * matches the last insert hash.
153 * {@code true} to force the insert of this file, {@code false}
155 * @return These file options
157 public FileOption setForceInsert(boolean forceInsert) {
158 this.forceInsert = forceInsert;
163 * Returns whether a redirect to a different key should be inserted. This
164 * will only matter if {@link #isInsert()} returns {@code false}. The key
165 * that should be redirected to still needs to be specified via
166 * {@link #setCustomKey(String)}.
168 * @return {@code true} if a redirect should be inserted, {@code false}
171 public boolean isInsertRedirect() {
172 return insertRedirect;
176 * Sets whether a redirect should be inserted. This will only matter if
177 * {@link #isInsert()} returns {@code false}, i.e. it has been
178 * {@link #setInsert(boolean)} to {@code false}. The key that should be
179 * redirected to still needs to be specified via
180 * {@link #setCustomKey(String)}.
182 * @param insertRedirect
183 * {@code true} if a redirect should be inserted, {@code false}
186 public void setInsertRedirect(boolean insertRedirect) {
187 this.insertRedirect = insertRedirect;
191 * Returns the hash of the file when it was last inserted
193 * @return The last hash of the file
195 public String getLastInsertHash() {
196 return lastInsertHash;
200 * Sets the hash of the file when it was last inserted.
202 * @param lastInsertHash
203 * The last hash of the file
204 * @return These file options
206 public FileOption setLastInsertHash(String lastInsertHash) {
207 this.lastInsertHash = lastInsertHash;
212 * Returns the last edition at which this file was inserted.
214 * @return The last insert edition of this file
216 public int getLastInsertEdition() {
217 return lastInsertEdition;
221 * Sets the last insert edition of this file.
223 * @param lastInsertEdition
224 * The last insert edition of this file
225 * @return These file options
227 public FileOption setLastInsertEdition(int lastInsertEdition) {
228 this.lastInsertEdition = lastInsertEdition;
233 * Returns the current hash of the file. This value is ony a temporary value
234 * that is copied to {@link #getLastInsertHash()} when a project has
235 * finished inserting.
237 * @see Project#onSuccessfulInsert()
238 * @return The current hash of the file
240 public String getCurrentHash() {
245 * Sets the current hash of the file.
248 * The current hash of the file
249 * @return These file options
251 public FileOption setCurrentHash(String currentHash) {
252 this.currentHash = currentHash;
257 * Returns whether this file has a changed name. Use
258 * {@link #getChangedName()} is this method returns {@code true}.
260 * @return {@code true} if this file has a changed name, {@code false}
263 public boolean hasChangedName() {
264 return (changedName != null) && (changedName.length() > 0);
268 * Returns the changed name for this file. This method will return {@code
269 * null} or an empty {@link String} if this file should not be renamed.
271 * @return The changed name, or {@code null} if this file should not be
274 public String getChangedName() {
279 * Sets the changed name for this file. Setting the changed file to {@code
280 * null} or an empty {@link String} will disable renaming.
283 * The new changed name for this file
285 public void setChangedName(String changedName) {
286 this.changedName = changedName;
290 * Sets the MIME type of the file. Setting the MIME type to
291 * <code>null</code> will set the MIME type to the default MIME type.
294 * The MIME type of the file
296 public void setMimeType(String mimeType) {
297 if (mimeType == null) {
298 this.mimeType = defaultMimeType;
300 this.mimeType = mimeType;
305 * Returns the MIME type of the file. If no custom MIME type has been set,
306 * the default MIME type is returned.
308 * @return The MIME type of the file
310 public String getMimeType() {
315 * Returns whether the options for this file have been modified, i.e. are
316 * not at their default values.
318 * @return <code>true</code> if the options have been modified,
319 * <code>false</code> if they are at default values
321 public boolean isCustom() {
322 if (insert != DEFAULT_INSERT) {
325 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
328 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
331 if (!defaultMimeType.equals(mimeType)) {
334 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {