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 custom key. */
47 private String customKey;
49 /** The changed name. */
50 private String changedName;
52 /** The default MIME type. */
53 private final String defaultMimeType;
55 /** The current MIME type. */
56 private String mimeType;
59 * Creates new file options.
61 * @param defaultMimeType
62 * The default MIME type of the file
64 public FileOption(String defaultMimeType) {
65 insert = DEFAULT_INSERT;
66 insertRedirect = DEFAULT_INSERT_REDIRECT;
67 customKey = DEFAULT_CUSTOM_KEY;
68 changedName = DEFAULT_CHANGED_NAME;
69 this.defaultMimeType = defaultMimeType;
70 mimeType = defaultMimeType;
74 * Returns the custom key. The custom key is only used when
75 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
78 * @return The custom key
80 public String getCustomKey() {
85 * Sets the custom key. The custom key is only used when {@link #isInsert()}
86 * and {@link #isInsertRedirect()} both return {@code true}.
91 public void setCustomKey(String customKey) {
92 if (customKey == null) {
95 this.customKey = customKey;
100 * Returns whether the file should be inserted. If a file is not inserted
101 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
102 * be inserted at all.
104 * @see #setCustomKey(String)
105 * @return <code>true</code> if the file should be inserted,
106 * <code>false</code> otherwise
108 public boolean isInsert() {
113 * Sets whether the file should be inserted. If a file is not inserted and
114 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
118 * <code>true</code> if the file should be inserted,
119 * <code>false</code> otherwise
121 public void setInsert(boolean insert) {
122 this.insert = insert;
126 * Returns whether a redirect to a different key should be inserted. This
127 * will only matter if {@link #isInsert()} returns {@code false}. The key
128 * that should be redirected to still needs to be specified via
129 * {@link #setCustomKey(String)}.
131 * @return {@code true} if a redirect should be inserted, {@code false}
134 public boolean isInsertRedirect() {
135 return insertRedirect;
139 * Sets whether a redirect should be inserted. This will only matter if
140 * {@link #isInsert()} returns {@code false}, i.e. it has been
141 * {@link #setInsert(boolean)} to {@code false}. The key that should be
142 * redirected to still needs to be specified via
143 * {@link #setCustomKey(String)}.
145 * @param insertRedirect
146 * {@code true} if a redirect should be inserted, {@code false}
149 public void setInsertRedirect(boolean insertRedirect) {
150 this.insertRedirect = insertRedirect;
154 * Returns whether this file has a changed name. Use
155 * {@link #getChangedName()} is this method returns {@code true}.
157 * @return {@code true} if this file has a changed name, {@code false}
160 public boolean hasChangedName() {
161 return (changedName != null) && (changedName.length() > 0);
165 * Returns the changed name for this file. This method will return {@code
166 * null} or an empty {@link String} if this file should not be renamed.
168 * @return The changed name, or {@code null} if this file should not be
171 public String getChangedName() {
176 * Sets the changed name for this file. Setting the changed file to {@code
177 * null} or an empty {@link String} will disable renaming.
180 * The new changed name for this file
182 public void setChangedName(String changedName) {
183 this.changedName = changedName;
187 * Sets the MIME type of the file. Setting the MIME type to
188 * <code>null</code> will set the MIME type to the default MIME type.
191 * The MIME type of the file
193 public void setMimeType(String mimeType) {
194 if (mimeType == null) {
195 this.mimeType = defaultMimeType;
197 this.mimeType = mimeType;
202 * Returns the MIME type of the file. If no custom MIME type has been set,
203 * the default MIME type is returned.
205 * @return The MIME type of the file
207 public String getMimeType() {
212 * Returns whether the options for this file have been modified, i.e. are
213 * not at their default values.
215 * @return <code>true</code> if the options have been modified,
216 * <code>false</code> if they are at default values
218 public boolean isCustom() {
219 if (insert != DEFAULT_INSERT) {
222 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
225 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
228 if (!defaultMimeType.equals(mimeType)) {
231 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {