fix annotation name
[jSite2.git] / src / net / pterodactylus / jsite / core / 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.core;
21
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import net.pterodactylus.util.logging.Logging;
26
27 /**
28  * An override is used to enter other information about a file than the defaults
29  * would have yielded. It is also used to add redirects to a project.
30  * 
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  * @version $Id$
33  */
34 public class FileOverride {
35
36         /** The logger. */
37         private static final Logger logger = Logging.getLogger(FileOverride.class.getName());
38
39         /** The insert override. */
40         private Boolean insert;
41
42         /** The override content type. */
43         private String contentType;
44
45         /** The redirect target. */
46         private String redirectTarget;
47
48         /**
49          * Checks whether this override has any content.
50          * 
51          * @return <code>true</code> if this override does not have any effects,
52          *         <code>false</code> otherwise
53          */
54         public boolean isEmpty() {
55                 return (insert == null) && (contentType == null) && (redirectTarget == null);
56         }
57
58         /**
59          * Returns the insert override.
60          * 
61          * @return <code>true</code> if the entry should be inserted,
62          *         <code>false</code> if it should not be inserted,
63          *         <code>null</code> if the default should not be overridden
64          */
65         public Boolean isInsert() {
66                 return insert;
67         }
68
69         /**
70          * Sets the insert override.
71          * 
72          * @param insert
73          *            <code>true</code> if the entry should be inserted,
74          *            <code>false</code> if it should not be inserted,
75          *            <code>null</code> if the default should not be overridden
76          */
77         public void setInsert(Boolean insert) {
78                 this.insert = insert;
79         }
80
81         /**
82          * Returns the override content type.
83          * 
84          * @return The override content type, or <code>null</code> to not override
85          *         the default
86          */
87         public String getContentType() {
88                 return contentType;
89         }
90
91         /**
92          * Sets the override content type.
93          * 
94          * @param contentType
95          *            The override content type, or <code>null</code> to not
96          *            override the default
97          */
98         public void setContentType(String contentType) {
99                 this.contentType = contentType;
100         }
101
102         /**
103          * Returns the target of a redirect.
104          * 
105          * @return The target URI of the redirect, or <code>null</code> if no
106          *         redirect should be created
107          */
108         public String getRedirectTarget() {
109                 return redirectTarget;
110         }
111
112         /**
113          * Sets the target of a redirect.
114          * 
115          * @param redirectTarget
116          *            The target URI of the redirect, or <code>null</code> if no
117          *            redirect should be created
118          */
119         public void setRedirectTarget(String redirectTarget) {
120                 this.redirectTarget = redirectTarget;
121         }
122
123         /**
124          * @see java.lang.Object#toString()
125          */
126         @Override
127         public String toString() {
128                 return ((insert != null) ? String.valueOf(insert) : "") + "|" + ((contentType != null) ? contentType : "") + "|" + ((redirectTarget != null) ? redirectTarget : "");
129         }
130
131         /**
132          * Converts an override string created by {@link #toString()} back to an
133          * {@link FileOverride} object.
134          * 
135          * @param overrideString
136          *            The textual representation of the override
137          * @return The parsed override, or <code>null</code> if the string could
138          *         not be parsed
139          */
140         public static FileOverride valueOf(String overrideString) {
141                 FileOverride override = new FileOverride();
142                 String[] parts = overrideString.split("\\|");
143                 logger.log(Level.FINEST, "parts.length: " + parts.length);
144                 if ((parts.length > 0) && (parts[0].length() > 0)) {
145                         override.insert = Boolean.valueOf(parts[0]);
146                 }
147                 if ((parts.length > 1) && (parts[1].length() > 0)) {
148                         override.contentType = parts[1];
149                 }
150                 if ((parts.length > 2) && (parts[2].length() > 0)) {
151                         override.redirectTarget = parts[2];
152                 }
153                 return override;
154         }
155
156 }