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