Unify copyright file headers, update years to include 2011.
[jSite.git] / src / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - FileOption.java - Copyright © 2006–2011 David Roden
3  *
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
7  * version.
8  *
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
12  * details.
13  *
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.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 /**
22  * Container for various file options.
23  *
24  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
25  */
26 public class FileOption {
27
28         /** The default for the insert state. */
29         private static final boolean DEFAULT_INSERT = true;
30
31         /** The default for the insert redirect state. */
32         private static final boolean DEFAULT_INSERT_REDIRECT = true;
33
34         /** The default for the custom key. */
35         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
36
37         /** The default changed name. */
38         private static final String DEFAULT_CHANGED_NAME = null;
39
40         /** The default container. */
41         private static final String DEFAULT_CONTAINER = "";
42
43         /** The default edition range. */
44         private static final int DEFAULT_EDITION_RANGE = 3;
45
46         /** The default for the replace edition state. */
47         private static final boolean DEFAULT_REPLACE_EDITION = false;
48
49         /** The insert state. */
50         private boolean insert;
51
52         /** Whether to insert a redirect. */
53         private boolean insertRedirect;
54
55         /** The custom key. */
56         private String customKey;
57
58         /** The changed name. */
59         private String changedName;
60
61         /** The default MIME type. */
62         private final String defaultMimeType;
63
64         /** The current MIME type. */
65         private String mimeType;
66
67         /** The container. */
68         private String container;
69
70         /** The edition range. */
71         private int editionRange;
72
73         /** The replace edition state. */
74         private boolean replaceEdition;
75
76         /**
77          * Creates new file options.
78          *
79          * @param defaultMimeType
80          *            The default MIME type of the file
81          */
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;
92         }
93
94         /**
95          * Returns the custom key. The custom key is only used when
96          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
97          * true}.
98          *
99          * @return The custom key
100          */
101         public String getCustomKey() {
102                 return customKey;
103         }
104
105         /**
106          * Sets the custom key. The custom key is only used when {@link #isInsert()}
107          * and {@link #isInsertRedirect()} both return {@code true}.
108          *
109          * @param customKey
110          *            The custom key
111          */
112         public void setCustomKey(String customKey) {
113                 if (customKey == null) {
114                         this.customKey = "";
115                 } else {
116                         this.customKey = customKey;
117                 }
118         }
119
120         /**
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.
124          *
125          * @see #setCustomKey(String)
126          * @return <code>true</code> if the file should be inserted,
127          *         <code>false</code> otherwise
128          */
129         public boolean isInsert() {
130                 return insert;
131         }
132
133         /**
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
136          * inserted at all.
137          *
138          * @param insert
139          *            <code>true</code> if the file should be inserted,
140          *            <code>false</code> otherwise
141          */
142         public void setInsert(boolean insert) {
143                 this.insert = insert;
144         }
145
146         /**
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)}.
151          *
152          * @return {@code true} if a redirect should be inserted, {@code false}
153          *         otherwise
154          */
155         public boolean isInsertRedirect() {
156                 return insertRedirect;
157         }
158
159         /**
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)}.
165          *
166          * @param insertRedirect
167          *            {@code true} if a redirect should be inserted, {@code false}
168          *            otherwise
169          */
170         public void setInsertRedirect(boolean insertRedirect) {
171                 this.insertRedirect = insertRedirect;
172         }
173
174         /**
175          * Returns whether this file has a changed name. Use
176          * {@link #getChangedName()} is this method returns {@code true}.
177          *
178          * @return {@code true} if this file has a changed name, {@code false}
179          *         otherwise
180          */
181         public boolean hasChangedName() {
182                 return (changedName != null) && (changedName.length() > 0);
183         }
184
185         /**
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.
188          *
189          * @return The changed name, or {@code null} if this file should not be
190          *         renamed
191          */
192         public String getChangedName() {
193                 return changedName;
194         }
195
196         /**
197          * Sets the changed name for this file. Setting the changed file to {@code
198          * null} or an empty {@link String} will disable renaming.
199          *
200          * @param changedName
201          *            The new changed name for this file
202          */
203         public void setChangedName(String changedName) {
204                 this.changedName = changedName;
205         }
206
207         /**
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.
210          *
211          * @param mimeType
212          *            The MIME type of the file
213          */
214         public void setMimeType(String mimeType) {
215                 if (mimeType == null) {
216                         this.mimeType = defaultMimeType;
217                 } else {
218                         this.mimeType = mimeType;
219                 }
220         }
221
222         /**
223          * Returns the MIME type of the file. If no custom MIME type has been set,
224          * the default MIME type is returned.
225          *
226          * @return The MIME type of the file
227          */
228         public String getMimeType() {
229                 return mimeType;
230         }
231
232         /**
233          * Returns the name of the container this file should be put in.
234          *
235          * @return The name of the container
236          */
237         public String getContainer() {
238                 return container;
239         }
240
241         /**
242          * Sets the name of the container this file should be put in.
243          *
244          * @param container
245          *            The name of the container
246          */
247         public void setContainer(String container) {
248                 if (container == null) {
249                         this.container = DEFAULT_CONTAINER;
250                 } else {
251                         this.container = container;
252                 }
253         }
254
255         /**
256          * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
257          *
258          * @param replaceEdition
259          *            <code>true</code> to replace tags, <code>false</code> not to
260          *            replace
261          */
262         public void setReplaceEdition(boolean replaceEdition) {
263                 this.replaceEdition = replaceEdition;
264         }
265
266         /**
267          * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
268          *
269          * @return <code>true</code> if tags should be replaced, <code>false</code>
270          *         otherwise
271          */
272         public boolean getReplaceEdition() {
273                 return replaceEdition;
274         }
275
276         /**
277          * Sets the range of editions that should be replaced.
278          *
279          * @param editionRange
280          *            The range editions to replace
281          */
282         public void setEditionRange(int editionRange) {
283                 this.editionRange = editionRange;
284         }
285
286         /**
287          * Returns the range of editions that should be replaced.
288          *
289          * @return The range of editions to replace
290          */
291         public int getEditionRange() {
292                 return editionRange;
293         }
294
295         /**
296          * Returns whether the options for this file have been modified, i.e. are
297          * not at their default values.
298          *
299          * @return <code>true</code> if the options have been modified,
300          *         <code>false</code> if they are at default values
301          */
302         public boolean isCustom() {
303                 if (insert != DEFAULT_INSERT) {
304                         return true;
305                 }
306                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
307                         return true;
308                 }
309                 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
310                         return true;
311                 }
312                 if (!defaultMimeType.equals(mimeType)) {
313                         return true;
314                 }
315                 if (!DEFAULT_CONTAINER.equals(container)) {
316                         return true;
317                 }
318                 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
319                         return true;
320                 }
321                 if (editionRange != DEFAULT_EDITION_RANGE) {
322                         return true;
323                 }
324                 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {
325                         return true;
326                 }
327                 return false;
328         }
329
330 }