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 default container. */
41 private static final String DEFAULT_CONTAINER = "";
43 /** The default edition range. */
44 private static final int DEFAULT_EDITION_RANGE = 3;
46 /** The default for the replace edition state. */
47 private static final boolean DEFAULT_REPLACE_EDITION = false;
49 /** The insert state. */
50 private boolean insert;
52 /** Whether to insert a redirect. */
53 private boolean insertRedirect;
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 private String container;
70 /** The edition range. */
71 private int editionRange;
73 /** The replace edition state. */
74 private boolean replaceEdition;
77 * Creates new file options.
79 * @param defaultMimeType
80 * The default MIME type of the file
82 public FileOption(String defaultMimeType) {
83 insert = DEFAULT_INSERT;
84 insertRedirect = DEFAULT_INSERT_REDIRECT;
85 customKey = DEFAULT_CUSTOM_KEY;
86 changedName = DEFAULT_CHANGED_NAME;
87 this.defaultMimeType = defaultMimeType;
88 mimeType = defaultMimeType;
89 container = DEFAULT_CONTAINER;
90 editionRange = DEFAULT_EDITION_RANGE;
91 replaceEdition = DEFAULT_REPLACE_EDITION;
95 * Returns the custom key. The custom key is only used when
96 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
99 * @return The custom key
101 public String getCustomKey() {
106 * Sets the custom key. The custom key is only used when {@link #isInsert()}
107 * and {@link #isInsertRedirect()} both return {@code true}.
112 public void setCustomKey(String customKey) {
113 if (customKey == null) {
116 this.customKey = customKey;
121 * Returns whether the file should be inserted. If a file is not inserted
122 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
123 * be inserted at all.
125 * @see #setCustomKey(String)
126 * @return <code>true</code> if the file should be inserted,
127 * <code>false</code> otherwise
129 public boolean isInsert() {
134 * Sets whether the file should be inserted. If a file is not inserted and
135 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
139 * <code>true</code> if the file should be inserted,
140 * <code>false</code> otherwise
142 public void setInsert(boolean insert) {
143 this.insert = insert;
147 * Returns whether a redirect to a different key should be inserted. This
148 * will only matter if {@link #isInsert()} returns {@code false}. The key
149 * that should be redirected to still needs to be specified via
150 * {@link #setCustomKey(String)}.
152 * @return {@code true} if a redirect should be inserted, {@code false}
155 public boolean isInsertRedirect() {
156 return insertRedirect;
160 * Sets whether a redirect should be inserted. This will only matter if
161 * {@link #isInsert()} returns {@code false}, i.e. it has been
162 * {@link #setInsert(boolean)} to {@code false}. The key that should be
163 * redirected to still needs to be specified via
164 * {@link #setCustomKey(String)}.
166 * @param insertRedirect
167 * {@code true} if a redirect should be inserted, {@code false}
170 public void setInsertRedirect(boolean insertRedirect) {
171 this.insertRedirect = insertRedirect;
175 * Returns whether this file has a changed name. Use
176 * {@link #getChangedName()} is this method returns {@code true}.
178 * @return {@code true} if this file has a changed name, {@code false}
181 public boolean hasChangedName() {
182 return (changedName != null) && (changedName.length() > 0);
186 * Returns the changed name for this file. This method will return {@code
187 * null} or an empty {@link String} if this file should not be renamed.
189 * @return The changed name, or {@code null} if this file should not be
192 public String getChangedName() {
197 * Sets the changed name for this file. Setting the changed file to {@code
198 * null} or an empty {@link String} will disable renaming.
201 * The new changed name for this file
203 public void setChangedName(String changedName) {
204 this.changedName = changedName;
208 * Sets the MIME type of the file. Setting the MIME type to
209 * <code>null</code> will set the MIME type to the default MIME type.
212 * The MIME type of the file
214 public void setMimeType(String mimeType) {
215 if (mimeType == null) {
216 this.mimeType = defaultMimeType;
218 this.mimeType = mimeType;
223 * Returns the MIME type of the file. If no custom MIME type has been set,
224 * the default MIME type is returned.
226 * @return The MIME type of the file
228 public String getMimeType() {
233 * Returns the name of the container this file should be put in.
235 * @return The name of the container
237 public String getContainer() {
242 * Sets the name of the container this file should be put in.
245 * The name of the container
247 public void setContainer(String container) {
248 if (container == null) {
249 this.container = DEFAULT_CONTAINER;
251 this.container = container;
256 * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
258 * @param replaceEdition
259 * <code>true</code> to replace tags, <code>false</code> not to
262 public void setReplaceEdition(boolean replaceEdition) {
263 this.replaceEdition = replaceEdition;
267 * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
269 * @return <code>true</code> if tags should be replaced, <code>false</code>
272 public boolean getReplaceEdition() {
273 return replaceEdition;
277 * Sets the range of editions that should be replaced.
279 * @param editionRange
280 * The range editions to replace
282 public void setEditionRange(int editionRange) {
283 this.editionRange = editionRange;
287 * Returns the range of editions that should be replaced.
289 * @return The range of editions to replace
291 public int getEditionRange() {
296 * Returns whether the options for this file have been modified, i.e. are
297 * not at their default values.
299 * @return <code>true</code> if the options have been modified,
300 * <code>false</code> if they are at default values
302 public boolean isCustom() {
303 if (insert != DEFAULT_INSERT) {
306 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
309 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
312 if (!defaultMimeType.equals(mimeType)) {
315 if (!DEFAULT_CONTAINER.equals(container)) {
318 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
321 if (editionRange != DEFAULT_EDITION_RANGE) {
324 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {