2 * jSite - a tool for uploading websites into Freenet Copyright (C) 2006 David
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
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
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.
20 package de.todesbaum.jsite.application;
23 import java.util.Collections;
24 import java.util.HashMap;
27 import de.todesbaum.util.mime.DefaultMIMETypes;
30 * Container for project information.
32 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class Project implements Comparable<Project> {
36 /** The name of the project. */
37 protected String name;
39 /** The description of the project. */
40 protected String description;
42 /** The insert URI of the project. */
43 protected String insertURI;
45 /** The request URI of the project. */
46 protected String requestURI;
48 /** The index file of the project. */
49 protected String indexFile;
51 /** The local path of the project. */
52 protected String localPath;
54 /** The remote path of the URI. */
55 protected String path;
57 /** The time of the last insertion. */
58 protected long lastInsertionTime;
60 /** The edition to insert to. */
61 protected int edition;
63 /** Options for files. */
64 protected Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
74 * Creates a new project from an existing one.
77 * The project to clone
79 public Project(Project project) {
81 description = project.description;
82 insertURI = project.insertURI;
83 requestURI = project.requestURI;
85 edition = project.edition;
86 localPath = project.localPath;
87 indexFile = project.indexFile;
88 lastInsertionTime = project.lastInsertionTime;
89 fileOptions = new HashMap<String, FileOption>(project.fileOptions);
93 * Returns the name of the project.
95 * @return The name of the project
97 public String getName() {
102 * Sets the name of the project.
105 * The name of the project
107 public void setName(String name) {
112 * Returns the description of the project.
114 * @return The description of the project
116 public String getDescription() {
121 * Sets the description of the project.
124 * The description of the project
126 public void setDescription(String description) {
127 this.description = description;
131 * Returns the local path of the project.
133 * @return The local path of the project
135 public String getLocalPath() {
140 * Sets the local path of the project.
143 * The local path of the project
145 public void setLocalPath(String localPath) {
146 this.localPath = localPath;
150 * Returns the name of the index file of the project, relative to the
151 * project’s local path.
153 * @return The name of the index file of the project
155 public String getIndexFile() {
160 * Sets the name of the index file of the project, relative to the project’s
164 * The name of the index file of the project
166 public void setIndexFile(String indexFile) {
167 this.indexFile = indexFile;
171 * Returns the time the project was last inserted, in milliseconds since the
174 * @return The time of the last insertion
176 public long getLastInsertionTime() {
177 return lastInsertionTime;
181 * Sets the time the project was last inserted, in milliseconds since the
184 * @param lastInserted
185 * The time of the last insertion
187 public void setLastInsertionTime(long lastInserted) {
188 lastInsertionTime = lastInserted;
192 * Returns the remote path of the project. The remote path is the path that
193 * directly follows the request URI of the project.
195 * @return The remote path of the project
197 public String getPath() {
202 * Sets the remote path of the project. The remote path is the path that
203 * directly follows the request URI of the project.
206 * The remote path of the project
208 public void setPath(String path) {
213 * Returns the insert URI of the project.
215 * @return The insert URI of the project
217 public String getInsertURI() {
222 * Sets the insert URI of the project.
225 * The insert URI of the project
227 public void setInsertURI(String insertURI) {
228 this.insertURI = shortenURI(insertURI);
232 * Returns the request URI of the project.
234 * @return The request URI of the project
236 public String getRequestURI() {
241 * Sets the request URI of the project.
244 * The request URI of the project
246 public void setRequestURI(String requestURI) {
247 this.requestURI = shortenURI(requestURI);
253 * This method returns the name of the project.
256 public String toString() {
261 * Shortens the given URI by removing scheme and key-type prefixes.
265 * @return The shortened URI
267 private String shortenURI(String uri) {
268 String shortUri = uri;
269 if (shortUri.startsWith("freenet:")) {
270 shortUri = shortUri.substring("freenet:".length());
272 if (shortUri.startsWith("SSK@")) {
273 shortUri = shortUri.substring("SSK@".length());
275 if (shortUri.startsWith("USK@")) {
276 shortUri = shortUri.substring("USK@".length());
278 if (shortUri.endsWith("/")) {
279 shortUri = shortUri.substring(0, shortUri.length() - 1);
285 * Shortens the name of the given file by removing the local path of the
286 * project and leading file separators.
289 * The file whose name should be shortened
290 * @return The shortened name of the file
292 public String shortenFilename(File file) {
293 String filename = file.getPath();
294 if (filename.startsWith(localPath)) {
295 filename = filename.substring(localPath.length());
296 if (filename.startsWith(File.separator)) {
297 filename = filename.substring(1);
304 * Returns the options for the file with the given name. If the file does
305 * not yet have any options, a new set of default options is created and
309 * The name of the file, relative to the project root
310 * @return The options for the file
312 public FileOption getFileOption(String filename) {
313 FileOption fileOption = fileOptions.get(filename);
314 if (fileOption == null) {
315 fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
316 fileOptions.put(filename, fileOption);
322 * Sets options for a file.
325 * The filename to set the options for, relative to the project
328 * The options to set for the file, or <code>null</code> to
329 * remove the options for the file
331 public void setFileOption(String filename, FileOption fileOption) {
332 if (fileOption != null) {
333 fileOptions.put(filename, fileOption);
335 fileOptions.remove(filename);
340 * Returns all file options.
342 * @return All file options
344 public Map<String, FileOption> getFileOptions() {
345 return Collections.unmodifiableMap(fileOptions);
349 * Sets all file options.
354 public void setFileOptions(Map<String, FileOption> fileOptions) {
355 this.fileOptions.clear();
356 this.fileOptions.putAll(fileOptions);
362 * Projects are compared by their name only.
364 public int compareTo(Project project) {
365 return name.compareToIgnoreCase(project.name);
369 * Returns the edition of the project.
371 * @return The edition of the project
373 public int getEdition() {
378 * Sets the edition of the project.
383 public void setEdition(int edition) {
384 this.edition = edition;
388 * Constructs the final request URI including the edition number.
391 * The offset for the edition number
392 * @return The final request URI
394 public String getFinalRequestURI(int offset) {
395 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";