118eee2208b5e4eee87158ae0c0ba765a4189fa6
[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 insert state. */
41         private boolean insert;
42
43         /** Whether to insert a redirect. */
44         private boolean insertRedirect;
45
46         /** The custom key. */
47         private String customKey;
48
49         /** The changed name. */
50         private String changedName;
51
52         /** The default MIME type. */
53         private final String defaultMimeType;
54
55         /** The current MIME type. */
56         private String mimeType;
57
58         /**
59          * Creates new file options.
60          *
61          * @param defaultMimeType
62          *            The default MIME type of the file
63          */
64         public FileOption(String defaultMimeType) {
65                 insert = DEFAULT_INSERT;
66                 insertRedirect = DEFAULT_INSERT_REDIRECT;
67                 customKey = DEFAULT_CUSTOM_KEY;
68                 changedName = DEFAULT_CHANGED_NAME;
69                 this.defaultMimeType = defaultMimeType;
70                 mimeType = defaultMimeType;
71         }
72
73         /**
74          * Returns the custom key. The custom key is only used when
75          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
76          * true}.
77          *
78          * @return The custom key
79          */
80         public String getCustomKey() {
81                 return customKey;
82         }
83
84         /**
85          * Sets the custom key. The custom key is only used when {@link #isInsert()}
86          * and {@link #isInsertRedirect()} both return {@code true}.
87          *
88          * @param customKey
89          *            The custom key
90          */
91         public void setCustomKey(String customKey) {
92                 if (customKey == null) {
93                         this.customKey = "";
94                 } else {
95                         this.customKey = customKey;
96                 }
97         }
98
99         /**
100          * Returns whether the file should be inserted. If a file is not inserted
101          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
102          * be inserted at all.
103          *
104          * @see #setCustomKey(String)
105          * @return <code>true</code> if the file should be inserted,
106          *         <code>false</code> otherwise
107          */
108         public boolean isInsert() {
109                 return insert;
110         }
111
112         /**
113          * Sets whether the file should be inserted. If a file is not inserted and
114          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
115          * inserted at all.
116          *
117          * @param insert
118          *            <code>true</code> if the file should be inserted,
119          *            <code>false</code> otherwise
120          */
121         public void setInsert(boolean insert) {
122                 this.insert = insert;
123         }
124
125         /**
126          * Returns whether a redirect to a different key should be inserted. This
127          * will only matter if {@link #isInsert()} returns {@code false}. The key
128          * that should be redirected to still needs to be specified via
129          * {@link #setCustomKey(String)}.
130          *
131          * @return {@code true} if a redirect should be inserted, {@code false}
132          *         otherwise
133          */
134         public boolean isInsertRedirect() {
135                 return insertRedirect;
136         }
137
138         /**
139          * Sets whether a redirect should be inserted. This will only matter if
140          * {@link #isInsert()} returns {@code false}, i.e. it has been
141          * {@link #setInsert(boolean)} to {@code false}. The key that should be
142          * redirected to still needs to be specified via
143          * {@link #setCustomKey(String)}.
144          *
145          * @param insertRedirect
146          *            {@code true} if a redirect should be inserted, {@code false}
147          *            otherwise
148          */
149         public void setInsertRedirect(boolean insertRedirect) {
150                 this.insertRedirect = insertRedirect;
151         }
152
153         /**
154          * Returns whether this file has a changed name. Use
155          * {@link #getChangedName()} is this method returns {@code true}.
156          *
157          * @return {@code true} if this file has a changed name, {@code false}
158          *         otherwise
159          */
160         public boolean hasChangedName() {
161                 return (changedName != null) && (changedName.length() > 0);
162         }
163
164         /**
165          * Returns the changed name for this file. This method will return {@code
166          * null} or an empty {@link String} if this file should not be renamed.
167          *
168          * @return The changed name, or {@code null} if this file should not be
169          *         renamed
170          */
171         public String getChangedName() {
172                 return changedName;
173         }
174
175         /**
176          * Sets the changed name for this file. Setting the changed file to {@code
177          * null} or an empty {@link String} will disable renaming.
178          *
179          * @param changedName
180          *            The new changed name for this file
181          */
182         public void setChangedName(String changedName) {
183                 this.changedName = changedName;
184         }
185
186         /**
187          * Sets the MIME type of the file. Setting the MIME type to
188          * <code>null</code> will set the MIME type to the default MIME type.
189          *
190          * @param mimeType
191          *            The MIME type of the file
192          */
193         public void setMimeType(String mimeType) {
194                 if (mimeType == null) {
195                         this.mimeType = defaultMimeType;
196                 } else {
197                         this.mimeType = mimeType;
198                 }
199         }
200
201         /**
202          * Returns the MIME type of the file. If no custom MIME type has been set,
203          * the default MIME type is returned.
204          *
205          * @return The MIME type of the file
206          */
207         public String getMimeType() {
208                 return mimeType;
209         }
210
211         /**
212          * Returns whether the options for this file have been modified, i.e. are
213          * not at their default values.
214          *
215          * @return <code>true</code> if the options have been modified,
216          *         <code>false</code> if they are at default values
217          */
218         public boolean isCustom() {
219                 if (insert != DEFAULT_INSERT) {
220                         return true;
221                 }
222                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
223                         return true;
224                 }
225                 if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) {
226                         return true;
227                 }
228                 if (!defaultMimeType.equals(mimeType)) {
229                         return true;
230                 }
231                 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {
232                         return true;
233                 }
234                 return false;
235         }
236
237 }