2 * jSite - a tool for uploading websites into Freenet Copyright (C) 2006 David
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.application;
23 * Container for various file options.
25 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27 public class FileOption {
29 /** The default for the insert state. */
30 private static final boolean DEFAULT_INSERT = true;
32 /** The default for the insert redirect state. */
33 private static final boolean DEFAULT_INSERT_REDIRECT = true;
35 /** The default for the custom key. */
36 private static final String DEFAULT_CUSTOM_KEY = "CHK@";
38 /** The default changed name. */
39 private static final String DEFAULT_CHANGED_NAME = null;
41 /** The default container. */
42 private static final String DEFAULT_CONTAINER = "";
44 /** The default edition range. */
45 private static final int DEFAULT_EDITION_RANGE = 3;
47 /** The default for the replace edition state. */
48 private static final boolean DEFAULT_REPLACE_EDITION = false;
50 /** The insert state. */
51 private boolean insert;
53 /** Whether to insert a redirect. */
54 private boolean insertRedirect;
56 /** The custom key. */
57 private String customKey;
59 /** The changed name. */
60 private String changedName;
62 /** The default MIME type. */
63 private final String defaultMimeType;
65 /** The current MIME type. */
66 private String mimeType;
69 private String container;
71 /** The edition range. */
72 private int editionRange;
74 /** The replace edition state. */
75 private boolean replaceEdition;
78 * Creates new file options.
80 * @param defaultMimeType
81 * The default MIME type of the file
83 public FileOption(String defaultMimeType) {
84 insert = DEFAULT_INSERT;
85 insertRedirect = DEFAULT_INSERT_REDIRECT;
86 customKey = DEFAULT_CUSTOM_KEY;
87 changedName = DEFAULT_CHANGED_NAME;
88 this.defaultMimeType = defaultMimeType;
89 mimeType = defaultMimeType;
90 container = DEFAULT_CONTAINER;
91 editionRange = DEFAULT_EDITION_RANGE;
92 replaceEdition = DEFAULT_REPLACE_EDITION;
96 * Returns the custom key. The custom key is only used when
97 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
100 * @return The custom key
102 public String getCustomKey() {
107 * Sets the custom key. The custom key is only used when {@link #isInsert()}
108 * and {@link #isInsertRedirect()} both return {@code true}.
113 public void setCustomKey(String customKey) {
114 if (customKey == null) {
117 this.customKey = customKey;
122 * Returns whether the file should be inserted. If a file is not inserted
123 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
124 * be inserted at all.
126 * @see #setCustomKey(String)
127 * @return <code>true</code> if the file should be inserted,
128 * <code>false</code> otherwise
130 public boolean isInsert() {
135 * Sets whether the file should be inserted. If a file is not inserted and
136 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
140 * <code>true</code> if the file should be inserted,
141 * <code>false</code> otherwise
143 public void setInsert(boolean insert) {
144 this.insert = insert;
148 * Returns whether a redirect to a different key should be inserted. This
149 * will only matter if {@link #isInsert()} returns {@code false}. The key
150 * that should be redirected to still needs to be specified via
151 * {@link #setCustomKey(String)}.
153 * @return {@code true} if a redirect should be inserted, {@code false}
156 public boolean isInsertRedirect() {
157 return insertRedirect;
161 * Sets whether a redirect should be inserted. This will only matter if
162 * {@link #isInsert()} returns {@code false}, i.e. it has been
163 * {@link #setInsert(boolean)} to {@code false}. The key that should be
164 * redirected to still needs to be specified via
165 * {@link #setCustomKey(String)}.
167 * @param insertRedirect
168 * {@code true} if a redirect should be inserted, {@code false}
171 public void setInsertRedirect(boolean insertRedirect) {
172 this.insertRedirect = insertRedirect;
176 * Returns whether this file has a changed name. Use
177 * {@link #getChangedName()} is this method returns {@code true}.
179 * @return {@code true} if this file has a changed name, {@code false}
182 public boolean hasChangedName() {
183 return (changedName != null) && (changedName.length() > 0);
187 * Returns the changed name for this file. This method will return {@code
188 * null} or an empty {@link String} if this file should not be renamed.
190 * @return The changed name, or {@code null} if this file should not be
193 public String getChangedName() {
198 * Sets the changed name for this file. Setting the changed file to {@code
199 * null} or an empty {@link String} will disable renaming.
202 * The new changed name for this file
204 public void setChangedName(String changedName) {
205 this.changedName = changedName;
209 * Sets the MIME type of the file. Setting the MIME type to
210 * <code>null</code> will set the MIME type to the default MIME type.
213 * The MIME type of the file
215 public void setMimeType(String mimeType) {
216 if (mimeType == null) {
217 this.mimeType = defaultMimeType;
219 this.mimeType = mimeType;
224 * Returns the MIME type of the file. If no custom MIME type has been set,
225 * the default MIME type is returned.
227 * @return The MIME type of the file
229 public String getMimeType() {
234 * Returns the name of the container this file should be put in.
236 * @return The name of the container
238 public String getContainer() {
243 * Sets the name of the container this file should be put in.
246 * The name of the container
248 public void setContainer(String container) {
249 if (container == null) {
250 this.container = DEFAULT_CONTAINER;
252 this.container = container;
257 * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
259 * @param replaceEdition
260 * <code>true</code> to replace tags, <code>false</code> not to
263 public void setReplaceEdition(boolean replaceEdition) {
264 this.replaceEdition = replaceEdition;
268 * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
270 * @return <code>true</code> if tags should be replaced, <code>false</code>
273 public boolean getReplaceEdition() {
274 return replaceEdition;
278 * Sets the range of editions that should be replaced.
280 * @param editionRange
281 * The range editions to replace
283 public void setEditionRange(int editionRange) {
284 this.editionRange = editionRange;
288 * Returns the range of editions that should be replaced.
290 * @return The range of editions to replace
292 public int getEditionRange() {
297 * Returns whether the options for this file have been modified, i.e. are
298 * not at their default values.
300 * @return <code>true</code> if the options have been modified,
301 * <code>false</code> if they are at default values
303 public boolean isCustom() {
304 if (insert != DEFAULT_INSERT) {
307 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
310 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
313 if (!defaultMimeType.equals(mimeType)) {
316 if (!DEFAULT_CONTAINER.equals(container)) {
319 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
322 if (editionRange != DEFAULT_EDITION_RANGE) {
325 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {