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 edition of the last insert. */
50 private int lastInsertEdition;
52 /** The current hash of the file. */
53 private String currentHash;
55 /** The custom key. */
56 private String customKey;
58 /** The changed name. */
59 private String changedName;
61 /** The default MIME type. */
62 private final String defaultMimeType;
64 /** The current MIME type. */
65 private String mimeType;
68 * Creates new file options.
70 * @param defaultMimeType
71 * The default MIME type of the file
73 public FileOption(String defaultMimeType) {
74 insert = DEFAULT_INSERT;
75 insertRedirect = DEFAULT_INSERT_REDIRECT;
76 customKey = DEFAULT_CUSTOM_KEY;
77 changedName = DEFAULT_CHANGED_NAME;
78 this.defaultMimeType = defaultMimeType;
79 mimeType = defaultMimeType;
83 * Returns the custom key. The custom key is only used when
84 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
87 * @return The custom key
89 public String getCustomKey() {
94 * Sets the custom key. The custom key is only used when {@link #isInsert()}
95 * and {@link #isInsertRedirect()} both return {@code true}.
100 public void setCustomKey(String customKey) {
101 if (customKey == null) {
104 this.customKey = customKey;
109 * Returns whether the file should be inserted. If a file is not inserted
110 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
111 * be inserted at all.
113 * @see #setCustomKey(String)
114 * @return <code>true</code> if the file should be inserted,
115 * <code>false</code> otherwise
117 public boolean isInsert() {
122 * Sets whether the file should be inserted. If a file is not inserted and
123 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
127 * <code>true</code> if the file should be inserted,
128 * <code>false</code> otherwise
130 public void setInsert(boolean insert) {
131 this.insert = insert;
135 * Returns whether a redirect to a different key should be inserted. This
136 * will only matter if {@link #isInsert()} returns {@code false}. The key
137 * that should be redirected to still needs to be specified via
138 * {@link #setCustomKey(String)}.
140 * @return {@code true} if a redirect should be inserted, {@code false}
143 public boolean isInsertRedirect() {
144 return insertRedirect;
148 * Sets whether a redirect should be inserted. This will only matter if
149 * {@link #isInsert()} returns {@code false}, i.e. it has been
150 * {@link #setInsert(boolean)} to {@code false}. The key that should be
151 * redirected to still needs to be specified via
152 * {@link #setCustomKey(String)}.
154 * @param insertRedirect
155 * {@code true} if a redirect should be inserted, {@code false}
158 public void setInsertRedirect(boolean insertRedirect) {
159 this.insertRedirect = insertRedirect;
163 * Returns the hash of the file when it was last inserted
165 * @return The last hash of the file
167 public String getLastInsertHash() {
168 return lastInsertHash;
172 * Sets the hash of the file when it was last inserted.
174 * @param lastInsertHash
175 * The last hash of the file
176 * @return These file options
178 public FileOption setLastInsertHash(String lastInsertHash) {
179 this.lastInsertHash = lastInsertHash;
184 * Returns the last edition at which this file was inserted.
186 * @return The last insert edition of this file
188 public int getLastInsertEdition() {
189 return lastInsertEdition;
193 * Sets the last insert edition of this file.
195 * @param lastInsertEdition
196 * The last insert edition of this file
197 * @return These file options
199 public FileOption setLastInsertEdition(int lastInsertEdition) {
200 this.lastInsertEdition = lastInsertEdition;
205 * Returns the current hash of the file. This value is ony a temporary value
206 * that is copied to {@link #getLastInsertHash()} when a project has
207 * finished inserting.
209 * @see Project#copyHashes()
210 * @return The current hash of the file
212 public String getCurrentHash() {
217 * Sets the current hash of the file.
220 * The current hash of the file
221 * @return These file options
223 public FileOption setCurrentHash(String currentHash) {
224 this.currentHash = currentHash;
229 * Returns whether this file has a changed name. Use
230 * {@link #getChangedName()} is this method returns {@code true}.
232 * @return {@code true} if this file has a changed name, {@code false}
235 public boolean hasChangedName() {
236 return (changedName != null) && (changedName.length() > 0);
240 * Returns the changed name for this file. This method will return {@code
241 * null} or an empty {@link String} if this file should not be renamed.
243 * @return The changed name, or {@code null} if this file should not be
246 public String getChangedName() {
251 * Sets the changed name for this file. Setting the changed file to {@code
252 * null} or an empty {@link String} will disable renaming.
255 * The new changed name for this file
257 public void setChangedName(String changedName) {
258 this.changedName = changedName;
262 * Sets the MIME type of the file. Setting the MIME type to
263 * <code>null</code> will set the MIME type to the default MIME type.
266 * The MIME type of the file
268 public void setMimeType(String mimeType) {
269 if (mimeType == null) {
270 this.mimeType = defaultMimeType;
272 this.mimeType = mimeType;
277 * Returns the MIME type of the file. If no custom MIME type has been set,
278 * the default MIME type is returned.
280 * @return The MIME type of the file
282 public String getMimeType() {
287 * Returns whether the options for this file have been modified, i.e. are
288 * not at their default values.
290 * @return <code>true</code> if the options have been modified,
291 * <code>false</code> if they are at default values
293 public boolean isCustom() {
294 if (insert != DEFAULT_INSERT) {
297 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
300 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
303 if (!defaultMimeType.equals(mimeType)) {
306 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {