2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
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 if (uri.startsWith("freenet:")) {
269 uri = uri.substring("freenet:".length());
271 if (uri.startsWith("SSK@")) {
272 uri = uri.substring("SSK@".length());
274 if (uri.startsWith("USK@")) {
275 uri = uri.substring("USK@".length());
277 if (uri.endsWith("/")) {
278 uri = uri.substring(0, uri.length() - 1);
284 * Shortens the name of the given file by removing the local path of the
285 * project and leading file separators.
288 * The file whose name should be shortened
289 * @return The shortened name of the file
291 public String shortenFilename(File file) {
292 String filename = file.getPath();
293 if (filename.startsWith(localPath)) {
294 filename = filename.substring(localPath.length());
295 if (filename.startsWith(File.separator)) {
296 filename = filename.substring(1);
303 * Returns the options for the file with the given name. If the file does
304 * not yet have any options, a new set of default options is created and
308 * The name of the file, relative to the project root
309 * @return The options for the file
311 public FileOption getFileOption(String filename) {
312 FileOption fileOption = fileOptions.get(filename);
313 if (fileOption == null) {
314 fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
315 fileOptions.put(filename, fileOption);
321 * Sets options for a file.
324 * The filename to set the options for, relative to the project
327 * The options to set for the file, or <code>null</code> to
328 * remove the options for the file
330 public void setFileOption(String filename, FileOption fileOption) {
331 if (fileOption != null) {
332 fileOptions.put(filename, fileOption);
334 fileOptions.remove(filename);
339 * Returns all file options.
341 * @return All file options
343 public Map<String, FileOption> getFileOptions() {
344 return Collections.unmodifiableMap(fileOptions);
348 * Sets all file options.
353 public void setFileOptions(Map<String, FileOption> fileOptions) {
354 this.fileOptions.clear();
355 this.fileOptions.putAll(fileOptions);
361 * Projects are compared by their name only.
363 public int compareTo(Project project) {
364 return name.compareToIgnoreCase(project.name);
368 * Returns the edition of the project.
370 * @return The edition of the project
372 public int getEdition() {
377 * Sets the edition of the project.
382 public void setEdition(int edition) {
383 this.edition = edition;
387 * Constructs the final request URI including the edition number.
390 * The offset for the edition number
391 * @return The final request URI
393 public String getFinalRequestURI(int offset) {
394 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";