2 * jSite - Project.java - Copyright © 2006–2011 David Roden
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
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
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.
19 package de.todesbaum.jsite.application;
22 import java.util.Collections;
23 import java.util.HashMap;
26 import de.todesbaum.util.mime.DefaultMIMETypes;
29 * Container for project information.
31 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33 public class Project implements Comparable<Project> {
35 /** The name of the project. */
36 protected String name;
38 /** The description of the project. */
39 protected String description;
41 /** The insert URI of the project. */
42 protected String insertURI;
44 /** The request URI of the project. */
45 protected String requestURI;
47 /** The index file of the project. */
48 protected String indexFile;
50 /** The local path of the project. */
51 protected String localPath;
53 /** The remote path of the URI. */
54 protected String path;
56 /** The time of the last insertion. */
57 protected long lastInsertionTime;
59 /** The edition to insert to. */
60 protected int edition;
62 /** Whether to ignore hidden directory. */
63 private boolean ignoreHiddenFiles;
65 /** Options for files. */
66 protected Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
76 * Creates a new project from an existing one.
79 * The project to clone
81 public Project(Project project) {
83 description = project.description;
84 insertURI = project.insertURI;
85 requestURI = project.requestURI;
87 edition = project.edition;
88 localPath = project.localPath;
89 indexFile = project.indexFile;
90 lastInsertionTime = project.lastInsertionTime;
91 ignoreHiddenFiles = project.ignoreHiddenFiles;
92 fileOptions = new HashMap<String, FileOption>(project.fileOptions);
96 * Returns the name of the project.
98 * @return The name of the project
100 public String getName() {
105 * Sets the name of the project.
108 * The name of the project
110 public void setName(String name) {
115 * Returns the description of the project.
117 * @return The description of the project
119 public String getDescription() {
124 * Sets the description of the project.
127 * The description of the project
129 public void setDescription(String description) {
130 this.description = description;
134 * Returns the local path of the project.
136 * @return The local path of the project
138 public String getLocalPath() {
143 * Sets the local path of the project.
146 * The local path of the project
148 public void setLocalPath(String localPath) {
149 this.localPath = localPath;
153 * Returns the name of the index file of the project, relative to the
154 * project’s local path.
156 * @return The name of the index file of the project
158 public String getIndexFile() {
163 * Sets the name of the index file of the project, relative to the project’s
167 * The name of the index file of the project
169 public void setIndexFile(String indexFile) {
170 this.indexFile = indexFile;
174 * Returns the time the project was last inserted, in milliseconds since the
177 * @return The time of the last insertion
179 public long getLastInsertionTime() {
180 return lastInsertionTime;
184 * Sets the time the project was last inserted, in milliseconds since the
187 * @param lastInserted
188 * The time of the last insertion
190 public void setLastInsertionTime(long lastInserted) {
191 lastInsertionTime = lastInserted;
195 * Returns the remote path of the project. The remote path is the path that
196 * directly follows the request URI of the project.
198 * @return The remote path of the project
200 public String getPath() {
205 * Sets the remote path of the project. The remote path is the path that
206 * directly follows the request URI of the project.
209 * The remote path of the project
211 public void setPath(String path) {
216 * Returns the insert URI of the project.
218 * @return The insert URI of the project
220 public String getInsertURI() {
225 * Sets the insert URI of the project.
228 * The insert URI of the project
230 public void setInsertURI(String insertURI) {
231 this.insertURI = shortenURI(insertURI);
235 * Returns the request URI of the project.
237 * @return The request URI of the project
239 public String getRequestURI() {
244 * Sets the request URI of the project.
247 * The request URI of the project
249 public void setRequestURI(String requestURI) {
250 this.requestURI = shortenURI(requestURI);
254 * Returns whether hidden files are ignored, i.e. not inserted.
256 * @return {@code true} if hidden files are not inserted, {@code false}
259 public boolean isIgnoreHiddenFiles() {
260 return ignoreHiddenFiles;
264 * Sets whether hidden files are ignored, i.e. not inserted.
266 * @param ignoreHiddenFiles
267 * {@code true} if hidden files are not inserted, {@code false}
270 public void setIgnoreHiddenFiles(boolean ignoreHiddenFiles) {
271 this.ignoreHiddenFiles = ignoreHiddenFiles;
277 * This method returns the name of the project.
280 public String toString() {
285 * Shortens the given URI by removing scheme and key-type prefixes.
289 * @return The shortened URI
291 private String shortenURI(String uri) {
292 String shortUri = uri;
293 if (shortUri.startsWith("freenet:")) {
294 shortUri = shortUri.substring("freenet:".length());
296 if (shortUri.startsWith("SSK@")) {
297 shortUri = shortUri.substring("SSK@".length());
299 if (shortUri.startsWith("USK@")) {
300 shortUri = shortUri.substring("USK@".length());
302 if (shortUri.endsWith("/")) {
303 shortUri = shortUri.substring(0, shortUri.length() - 1);
309 * Shortens the name of the given file by removing the local path of the
310 * project and leading file separators.
313 * The file whose name should be shortened
314 * @return The shortened name of the file
316 public String shortenFilename(File file) {
317 String filename = file.getPath();
318 if (filename.startsWith(localPath)) {
319 filename = filename.substring(localPath.length());
320 if (filename.startsWith(File.separator)) {
321 filename = filename.substring(1);
328 * Returns the options for the file with the given name. If the file does
329 * not yet have any options, a new set of default options is created and
333 * The name of the file, relative to the project root
334 * @return The options for the file
336 public FileOption getFileOption(String filename) {
337 FileOption fileOption = fileOptions.get(filename);
338 if (fileOption == null) {
339 fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename));
340 fileOptions.put(filename, fileOption);
346 * Sets options for a file.
349 * The filename to set the options for, relative to the project
352 * The options to set for the file, or <code>null</code> to
353 * remove the options for the file
355 public void setFileOption(String filename, FileOption fileOption) {
356 if (fileOption != null) {
357 fileOptions.put(filename, fileOption);
359 fileOptions.remove(filename);
364 * Returns all file options.
366 * @return All file options
368 public Map<String, FileOption> getFileOptions() {
369 return Collections.unmodifiableMap(fileOptions);
373 * Sets all file options.
378 public void setFileOptions(Map<String, FileOption> fileOptions) {
379 this.fileOptions.clear();
380 this.fileOptions.putAll(fileOptions);
386 * Projects are compared by their name only.
388 public int compareTo(Project project) {
389 return name.compareToIgnoreCase(project.name);
393 * Returns the edition of the project.
395 * @return The edition of the project
397 public int getEdition() {
402 * Sets the edition of the project.
407 public void setEdition(int edition) {
408 this.edition = edition;
412 * Constructs the final request URI including the edition number.
415 * The offset for the edition number
416 * @return The final request URI
418 public String getFinalRequestURI(int offset) {
419 return "USK@" + requestURI + "/" + path + "/" + (edition + offset) + "/";
423 * Copies the current hashes of all file options to the last insert hashes,
424 * updating the hashes for the next insert. This method should only be
425 * called after the insert has finished successfully.
427 public void copyHashes() {
428 for (FileOption fileOption : fileOptions.values()) {
429 fileOption.setLastInsertHash(fileOption.getCurrentHash());