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