46396f6730ad9538a279c5fe02e11224ac815dea
[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 custom key. */
33         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
34
35         /** The default container. */
36         private static final String DEFAULT_CONTAINER = "";
37
38         /** The default edition range. */
39         private static final int DEFAULT_EDITION_RANGE = 3;
40
41         /** The default for the replace edition state. */
42         private static final boolean DEFAULT_REPLACE_EDITION = false;
43
44         /** The insert state. */
45         private boolean insert;
46
47         /** Whether to insert a redirect. */
48         private boolean insertRedirect;
49
50         /** The custom key. */
51         private String customKey;
52
53         /** The default MIME type. */
54         private final String defaultMimeType;
55
56         /** The current MIME type. */
57         private String mimeType;
58
59         /** The container. */
60         private String container;
61
62         /** The edition range. */
63         private int editionRange;
64
65         /** The replace edition state. */
66         private boolean replaceEdition;
67
68         /**
69          * Creates new file options.
70          *
71          * @param defaultMimeType
72          *            The default MIME type of the file
73          */
74         public FileOption(String defaultMimeType) {
75                 insert = DEFAULT_INSERT;
76                 customKey = DEFAULT_CUSTOM_KEY;
77                 this.defaultMimeType = defaultMimeType;
78                 mimeType = defaultMimeType;
79                 container = DEFAULT_CONTAINER;
80                 editionRange = DEFAULT_EDITION_RANGE;
81                 replaceEdition = DEFAULT_REPLACE_EDITION;
82         }
83
84         /**
85          * Returns the custom key. The custom key is only used when
86          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
87          * true}.
88          *
89          * @return The custom key
90          */
91         public String getCustomKey() {
92                 return customKey;
93         }
94
95         /**
96          * Sets the custom key. The custom key is only used when {@link #isInsert()}
97          * and {@link #isInsertRedirect()} both return {@code true}.
98          *
99          * @param customKey
100          *            The custom key
101          */
102         public void setCustomKey(String customKey) {
103                 if (customKey == null) {
104                         this.customKey = "";
105                 } else {
106                         this.customKey = customKey;
107                 }
108         }
109
110         /**
111          * Returns whether the file should be inserted. If a file is not inserted
112          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
113          * be inserted at all.
114          *
115          * @see #setCustomKey(String)
116          * @return <code>true</code> if the file should be inserted,
117          *         <code>false</code> otherwise
118          */
119         public boolean isInsert() {
120                 return insert;
121         }
122
123         /**
124          * Sets whether the file should be inserted. If a file is not inserted and
125          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
126          * inserted at all.
127          *
128          * @param insert
129          *            <code>true</code> if the file should be inserted,
130          *            <code>false</code> otherwise
131          */
132         public void setInsert(boolean insert) {
133                 this.insert = insert;
134         }
135
136         /**
137          * Returns whether a redirect to a different key should be inserted. This
138          * will only matter if {@link #isInsert()} returns {@code false}. The key
139          * that should be redirected to still needs to be specified via
140          * {@link #setCustomKey(String)}.
141          *
142          * @return {@code true} if a redirect should be inserted, {@code false}
143          *         otherwise
144          */
145         public boolean isInsertRedirect() {
146                 return insertRedirect;
147         }
148
149         /**
150          * Sets whether a redirect should be inserted. This will only matter if
151          * {@link #isInsert()} returns {@code false}, i.e. it has been
152          * {@link #setInsert(boolean)} to {@code false}. The key that should be
153          * redirected to still needs to be specified via
154          * {@link #setCustomKey(String)}.
155          *
156          * @param insertRedirect
157          *            {@code true} if a redirect should be inserted, {@code false}
158          *            otherwise
159          */
160         public void setInsertRedirect(boolean insertRedirect) {
161                 this.insertRedirect = insertRedirect;
162         }
163
164         /**
165          * Sets the MIME type of the file. Setting the MIME type to
166          * <code>null</code> will set the MIME type to the default MIME type.
167          *
168          * @param mimeType
169          *            The MIME type of the file
170          */
171         public void setMimeType(String mimeType) {
172                 if (mimeType == null) {
173                         this.mimeType = defaultMimeType;
174                 } else {
175                         this.mimeType = mimeType;
176                 }
177         }
178
179         /**
180          * Returns the MIME type of the file. If no custom MIME type has been set,
181          * the default MIME type is returned.
182          *
183          * @return The MIME type of the file
184          */
185         public String getMimeType() {
186                 return mimeType;
187         }
188
189         /**
190          * Returns the name of the container this file should be put in.
191          *
192          * @return The name of the container
193          */
194         public String getContainer() {
195                 return container;
196         }
197
198         /**
199          * Sets the name of the container this file should be put in.
200          *
201          * @param container
202          *            The name of the container
203          */
204         public void setContainer(String container) {
205                 if (container == null) {
206                         this.container = DEFAULT_CONTAINER;
207                 } else {
208                         this.container = container;
209                 }
210         }
211
212         /**
213          * Sets whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
214          *
215          * @param replaceEdition
216          *            <code>true</code> to replace tags, <code>false</code> not to
217          *            replace
218          */
219         public void setReplaceEdition(boolean replaceEdition) {
220                 this.replaceEdition = replaceEdition;
221         }
222
223         /**
224          * Returns whether the file should have “$[EDITION+<i>n</i>]” tags replaced.
225          *
226          * @return <code>true</code> if tags should be replaced, <code>false</code>
227          *         otherwise
228          */
229         public boolean getReplaceEdition() {
230                 return replaceEdition;
231         }
232
233         /**
234          * Sets the range of editions that should be replaced.
235          *
236          * @param editionRange
237          *            The range editions to replace
238          */
239         public void setEditionRange(int editionRange) {
240                 this.editionRange = editionRange;
241         }
242
243         /**
244          * Returns the range of editions that should be replaced.
245          *
246          * @return The range of editions to replace
247          */
248         public int getEditionRange() {
249                 return editionRange;
250         }
251
252         /**
253          * Returns whether the options for this file have been modified, i.e. are
254          * not at their default values.
255          *
256          * @return <code>true</code> if the options have been modified,
257          *         <code>false</code> if they are at default values
258          */
259         public boolean isCustom() {
260                 if (insert != DEFAULT_INSERT) {
261                         return true;
262                 }
263                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
264                         return true;
265                 }
266                 if (!defaultMimeType.equals(mimeType)) {
267                         return true;
268                 }
269                 if (!DEFAULT_CONTAINER.equals(container)) {
270                         return true;
271                 }
272                 if (replaceEdition != DEFAULT_REPLACE_EDITION) {
273                         return true;
274                 }
275                 if (editionRange != DEFAULT_EDITION_RANGE) {
276                         return true;
277                 }
278                 return false;
279         }
280
281 }