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 insert a redirect. */
44 private boolean insertRedirect;
46 /** The hash of the last insert. */
47 private String lastInsertHash;
49 /** The current hash of the file. */
50 private String currentHash;
52 /** The custom key. */
53 private String customKey;
55 /** The changed name. */
56 private String changedName;
58 /** The default MIME type. */
59 private final String defaultMimeType;
61 /** The current MIME type. */
62 private String mimeType;
65 * Creates new file options.
67 * @param defaultMimeType
68 * The default MIME type of the file
70 public FileOption(String defaultMimeType) {
71 insert = DEFAULT_INSERT;
72 insertRedirect = DEFAULT_INSERT_REDIRECT;
73 customKey = DEFAULT_CUSTOM_KEY;
74 changedName = DEFAULT_CHANGED_NAME;
75 this.defaultMimeType = defaultMimeType;
76 mimeType = defaultMimeType;
80 * Returns the custom key. The custom key is only used when
81 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
84 * @return The custom key
86 public String getCustomKey() {
91 * Sets the custom key. The custom key is only used when {@link #isInsert()}
92 * and {@link #isInsertRedirect()} both return {@code true}.
97 public void setCustomKey(String customKey) {
98 if (customKey == null) {
101 this.customKey = customKey;
106 * Returns whether the file should be inserted. If a file is not inserted
107 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
108 * be inserted at all.
110 * @see #setCustomKey(String)
111 * @return <code>true</code> if the file should be inserted,
112 * <code>false</code> otherwise
114 public boolean isInsert() {
119 * Sets whether the file should be inserted. If a file is not inserted and
120 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
124 * <code>true</code> if the file should be inserted,
125 * <code>false</code> otherwise
127 public void setInsert(boolean insert) {
128 this.insert = insert;
132 * Returns whether a redirect to a different key should be inserted. This
133 * will only matter if {@link #isInsert()} returns {@code false}. The key
134 * that should be redirected to still needs to be specified via
135 * {@link #setCustomKey(String)}.
137 * @return {@code true} if a redirect should be inserted, {@code false}
140 public boolean isInsertRedirect() {
141 return insertRedirect;
145 * Sets whether a redirect should be inserted. This will only matter if
146 * {@link #isInsert()} returns {@code false}, i.e. it has been
147 * {@link #setInsert(boolean)} to {@code false}. The key that should be
148 * redirected to still needs to be specified via
149 * {@link #setCustomKey(String)}.
151 * @param insertRedirect
152 * {@code true} if a redirect should be inserted, {@code false}
155 public void setInsertRedirect(boolean insertRedirect) {
156 this.insertRedirect = insertRedirect;
160 * Returns the hash of the file when it was last inserted
162 * @return The last hash of the file
164 public String getLastInsertHash() {
165 return lastInsertHash;
169 * Sets the hash of the file when it was last inserted.
171 * @param lastInsertHash
172 * The last hash of the file
173 * @return These file options
175 public FileOption setLastInsertHash(String lastInsertHash) {
176 this.lastInsertHash = lastInsertHash;
181 * Returns the current hash of the file. This value is ony a temporary value
182 * that is copied to {@link #getLastInsertHash()} when a project has
183 * finished inserting.
185 * @see Project#copyHashes()
186 * @return The current hash of the file
188 public String getCurrentHash() {
193 * Sets the current hash of the file.
196 * The current hash of the file
197 * @return These file options
199 public FileOption setCurrentHash(String currentHash) {
200 this.currentHash = currentHash;
205 * Returns whether this file has a changed name. Use
206 * {@link #getChangedName()} is this method returns {@code true}.
208 * @return {@code true} if this file has a changed name, {@code false}
211 public boolean hasChangedName() {
212 return (changedName != null) && (changedName.length() > 0);
216 * Returns the changed name for this file. This method will return {@code
217 * null} or an empty {@link String} if this file should not be renamed.
219 * @return The changed name, or {@code null} if this file should not be
222 public String getChangedName() {
227 * Sets the changed name for this file. Setting the changed file to {@code
228 * null} or an empty {@link String} will disable renaming.
231 * The new changed name for this file
233 public void setChangedName(String changedName) {
234 this.changedName = changedName;
238 * Sets the MIME type of the file. Setting the MIME type to
239 * <code>null</code> will set the MIME type to the default MIME type.
242 * The MIME type of the file
244 public void setMimeType(String mimeType) {
245 if (mimeType == null) {
246 this.mimeType = defaultMimeType;
248 this.mimeType = mimeType;
253 * Returns the MIME type of the file. If no custom MIME type has been set,
254 * the default MIME type is returned.
256 * @return The MIME type of the file
258 public String getMimeType() {
263 * Returns whether the options for this file have been modified, i.e. are
264 * not at their default values.
266 * @return <code>true</code> if the options have been modified,
267 * <code>false</code> if they are at default values
269 public boolean isCustom() {
270 if (insert != DEFAULT_INSERT) {
273 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
276 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
279 if (!defaultMimeType.equals(mimeType)) {
282 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {