add isEmpty()
[jSite2.git] / src / net / pterodactylus / jsite / project / FileOverride.java
1 /*
2  * jSite2 - Override.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.project;
21
22 /**
23  * An override is used to enter other information about a file than the defaults
24  * would have yielded. It is also used to add redirects to a project.
25  * 
26  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27  * @version $Id$
28  */
29 public class FileOverride {
30
31         /** The insert override. */
32         private Boolean insert;
33
34         /** The override content type. */
35         private String contentType;
36
37         /** The redirect target. */
38         private String redirectTarget;
39
40         /**
41          * Checks whether this override has any content.
42          * 
43          * @return <code>true</code> if this override does not have any effects,
44          *         <code>false</code> otherwise
45          */
46         public boolean isEmpty() {
47                 return (insert == null) && (contentType == null) && (redirectTarget == null);
48         }
49
50         /**
51          * Returns the insert override.
52          * 
53          * @return <code>true</code> if the entry should be inserted,
54          *         <code>false</code> if it should not be inserted,
55          *         <code>null</code> if the default should not be overridden
56          */
57         public Boolean isInsert() {
58                 return insert;
59         }
60
61         /**
62          * Sets the insert override.
63          * 
64          * @param insert
65          *            <code>true</code> if the entry should be inserted,
66          *            <code>false</code> if it should not be inserted,
67          *            <code>null</code> if the default should not be overridden
68          */
69         public void setInsert(Boolean insert) {
70                 this.insert = insert;
71         }
72
73         /**
74          * Returns the override content type.
75          * 
76          * @return The override content type, or <code>null</code> to not override
77          *         the default
78          */
79         public String getContentType() {
80                 return contentType;
81         }
82
83         /**
84          * Sets the override content type.
85          * 
86          * @param contentType
87          *            The override content type, or <code>null</code> to not
88          *            override the default
89          */
90         public void setContentType(String contentType) {
91                 this.contentType = contentType;
92         }
93
94         /**
95          * Returns the target of a redirect.
96          * 
97          * @return The target URI of the redirect, or <code>null</code> if no
98          *         redirect should be created
99          */
100         public String getRedirectTarget() {
101                 return redirectTarget;
102         }
103
104         /**
105          * Sets the target of a redirect.
106          * 
107          * @param redirectTarget
108          *            The target URI of the redirect, or <code>null</code> if no
109          *            redirect should be created
110          */
111         public void setRedirectTarget(String redirectTarget) {
112                 this.redirectTarget = redirectTarget;
113         }
114
115         /**
116          * @see java.lang.Object#toString()
117          */
118         @java.lang.Override
119         public String toString() {
120                 return ((insert != null) ? String.valueOf(insert) : "") + "|" + ((contentType != null) ? contentType : "") + "|" + ((redirectTarget != null) ? redirectTarget : "");
121         }
122
123         /**
124          * Converts an override string created by {@link #toString()} back to an
125          * {@link FileOverride} object.
126          * 
127          * @param overrideString
128          *            The textual representation of the override
129          * @return The parsed override, or <code>null</code> if the string could
130          *         not be parsed
131          */
132         public static FileOverride valueOf(String overrideString) {
133                 FileOverride override = new FileOverride();
134                 String[] parts = overrideString.split("\\|");
135                 if (parts.length < 3) {
136                         return null;
137                 }
138                 if (parts[0].length() > 0) {
139                         override.insert = Boolean.valueOf(parts[0]);
140                 }
141                 if (parts[1].length() > 0) {
142                         override.contentType = parts[1];
143                 }
144                 if (parts[2].length() > 0) {
145                         override.redirectTarget = parts[2];
146                 }
147                 return override;
148         }
149
150 }