2 * jSite2 - Project.java -
3 * Copyright © 2008 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 net.pterodactylus.jsite.core;
22 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
30 import net.pterodactylus.jsite.util.IdGenerator;
31 import net.pterodactylus.util.beans.AbstractBean;
32 import net.pterodactylus.util.number.Hex;
35 * Container for project information. A Project is capable of notifying
36 * {@link PropertyChangeListener}s if any of the contained properties change.
38 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40 public class Project extends AbstractBean {
42 /** Name of the “name” property. */
43 public static final String PROPERTY_NAME = "name";
45 /** Name of the “description” property. */
46 public static final String PROPERTY_DESCRIPTION = "description";
48 /** Name of the “public key” property. */
49 public static final String PROPERTY_PUBLIC_KEY = "publicKey";
51 /** Name of the “private key” property. */
52 public static final String PROPERTY_PRIVATE_KEY = "privateKey";
54 /** Name of the “base path” property. */
55 public static final String PROPERTY_BASE_PATH = "basePath";
57 /** Name of the “default file” property. */
58 public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
60 /** Name of the “node” property. */
61 public static final String PROPERTY_NODE = "node";
66 /** The name of the project. */
69 /** The description of the project. */
70 private String description;
72 /** The public key. */
73 private String publicKey;
75 /** The private key. */
76 private String privateKey;
78 /** The base path of the project. */
79 private String basePath;
81 /** The default file. */
82 private String defaultFile;
85 private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
87 /** The default node to insert to. */
90 /** The current root project file. */
91 private ProjectFileImpl rootProjectFile;
94 * Creates a new project.
97 id = Hex.toHex(IdGenerator.generateId());
101 * Clones the given project.
105 Project(Project project) {
107 this.name = project.name;
108 this.description = project.description;
109 this.publicKey = project.publicKey;
110 this.privateKey = project.privateKey;
111 this.basePath = project.basePath;
115 * Returns the internal ID.
117 * @return The internal ID
124 * Sets the internal ID.
129 void setId(String id) {
131 this.id = Hex.toHex(IdGenerator.generateId());
138 * Returns the name of the project.
140 * @return The name of the project
142 public String getName() {
147 * Sets the name of the project.
150 * The name of the project
152 public void setName(String name) {
153 String oldName = this.name;
155 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
159 * Returns the description of the project.
161 * @return The description of the project
163 public String getDescription() {
168 * Sets the description of the project
171 * The description of the project
173 public void setDescription(String description) {
174 String oldDescription = this.description;
175 this.description = description;
176 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
180 * Returns the public key of the project.
182 * @return The public key of the project
184 public String getPublicKey() {
189 * Sets the public key of the project.
192 * The public key of the project
194 void setPublicKey(String publicKey) {
195 String oldPublicKey = this.publicKey;
196 this.publicKey = publicKey;
197 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
201 * Returns the private key of the project.
203 * @return The private key of the project
205 public String getPrivateKey() {
210 * Sets the private key of the project.
213 * The private key of the project
215 void setPrivateKey(String privateKey) {
216 String oldPrivateKey = this.privateKey;
217 this.privateKey = privateKey;
218 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
222 * Returns the base path of the project.
224 * @return The base path of the project
226 public String getBasePath() {
231 * Sets the base path of the project.
234 * The base path of the project
236 public void setBasePath(String basePath) {
237 String oldBasePath = this.basePath;
238 this.basePath = basePath;
239 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
243 * Returns the default file.
245 * @return The default file
247 public String getDefaultFile() {
252 * Sets the default file.
257 public void setDefaultFile(String defaultFile) {
258 String oldDefaultFile = this.defaultFile;
259 this.defaultFile = defaultFile;
260 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
264 * Adds a file override for the given file.
269 * The override for the file
271 public void addFileOverride(ProjectFile projectFile, FileOverride override) {
272 addFileOverride(projectFile.getCompletePath(), override);
276 * Adds a file override for the given file.
281 * The override for the file
283 public void addFileOverride(String filePath, FileOverride override) {
284 fileOverrides.put(filePath, override);
288 * Removes the file override for the given file.
291 * The file for which to remove the override
293 public void removeFileOverride(ProjectFile projectFile) {
294 removeFileOverride(projectFile.getCompletePath());
298 * Removes the file override for the given file.
301 * The file path for which to remove the override
303 public void removeFileOverride(String filePath) {
304 fileOverrides.remove(filePath);
308 * Returns the file override for the given file.
311 * The file for which to get the override
312 * @return The file override, or <code>null</code> if the given file does
313 * not have an override
315 public FileOverride getFileOverride(ProjectFile projectFile) {
316 return getFileOverride(projectFile.getCompletePath());
320 * Returns the file override for the given file.
323 * The file path for which to get the override
324 * @return The file override, or <code>null</code> if the given file does
325 * not have an override
327 public FileOverride getFileOverride(String filePath) {
328 return fileOverrides.get(filePath);
332 * Returns the list of {@link FileOverride}s.
334 * @return All file overrides
336 public Map<String, FileOverride> getFileOverrides() {
337 return fileOverrides;
341 * Scans the base path for files and returns the {@link ProjectFile} for the
342 * base path. From this file it is possible to reach all files in the base
343 * path. This method is disk-intensive and may take some time on larger
346 * @return The file for the base path, or <code>null</code> if the base
347 * path does not denote an existing directory
349 public ProjectFile getBaseFile() {
350 File basePathFile = new File(basePath);
351 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
354 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
355 scanDirectory(basePathFile, rootProjectFile);
356 return rootProjectFile;
360 * Returns the file that is specified by its complete path.
362 * @param completePath
363 * The complete path of the file
364 * @return The project file at the given path, or <code>null</code> if
365 * there is no such file
367 public ProjectFile getFile(String completePath) {
368 if (rootProjectFile == null) {
371 if ((rootProjectFile == null) || (completePath.length() == 0)) {
372 return rootProjectFile;
374 String[] pathParts = completePath.split("\\" + File.separator);
375 ProjectFileImpl currentProjectFile = rootProjectFile;
376 for (String pathPart : pathParts) {
377 currentProjectFile = currentProjectFile.getFile(pathPart);
378 if (currentProjectFile == null) {
382 return currentProjectFile;
386 * Returns the default node to insert this project to.
388 * @return The node to insert this project to
390 public Node getNode() {
395 * Sets the default node to insert this project to.
398 * The node to insert this project to
400 public void setNode(Node node) {
401 Node oldNode = this.node;
403 fireIfPropertyChanged(PROPERTY_NODE, oldNode, node);
407 * @see java.lang.Object#toString()
410 public String toString() {
411 return getClass().getName() + "[id=" + id + ",name=" + name + ",description=" + description + ",publicKey=" + publicKey + ",privateKey=" + privateKey + ",basePath=" + basePath + ",defaultFile=" + defaultFile + ",node=" + node + "]";
419 * Scans the given directory and recreates the file and directory structure
420 * in the given project file.
423 * The directory to scan
425 * The project file in which to recreate the directory and file
428 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
429 if (!directory.isDirectory()) {
432 for (File file : directory.listFiles()) {
433 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
434 if (file.isDirectory()) {
435 scanDirectory(file, projectFileChild);
442 * Implementation of a {@link ProjectFile}.
444 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
446 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
448 /** The parent of this project file. */
449 private final ProjectFileImpl parentProjectFile;
451 /** The name of this project file. */
452 private final String name;
454 /** The size of the file. */
455 private final long size;
457 /** Whether this project file is a directory. */
458 private final boolean directory;
460 /** Whether this file is hidden. */
461 private final boolean hidden;
463 /** This project file’s children. */
464 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
467 * Creates a new project fie.
469 * @param parentProjectFile
470 * The parent of the project file, or <code>null</code> if
471 * the new project file does not have a parent
473 * The name of the project file
475 * The size of the file
477 * <code>true</code> if this project file is a directory,
478 * <code>false</code> otherwise
480 * <code>true</code> if this project file is hidden,
481 * <code>false</code> otherwise
483 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
484 this.parentProjectFile = parentProjectFile;
487 this.directory = isDirectory;
488 this.hidden = isHidden;
492 // INTERFACE ProjectFile
496 * @see net.pterodactylus.jsite.core.ProjectFile#getName()
498 public String getName() {
503 * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
505 public ProjectFile getParent() {
506 return parentProjectFile;
512 public long getSize() {
517 * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
519 public List<ProjectFile> getParents() {
520 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
521 ProjectFileImpl currentProjectFile = this;
523 parentProjectFiles.add(0, currentProjectFile);
524 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
525 return parentProjectFiles;
531 /* TODO - caching? */
532 public String getCompletePath() {
533 StringBuilder completePath = new StringBuilder();
534 ProjectFileImpl currentProjectFile = this;
535 while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
536 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
537 currentProjectFile = currentProjectFile.parentProjectFile;
539 return (completePath.length() > 0) ? completePath.substring(1) : "";
543 * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
545 public boolean isFile() {
550 * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
552 public boolean isDirectory() {
557 * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
559 public boolean isHidden() {
564 * Returns the project file with the given name. The project file has to
565 * be a direct child of this project file.
568 * The name of the file to get
569 * @return The project file, or <code>null</code> if there is no
570 * project file by that name
572 public ProjectFileImpl getFile(String name) {
573 if (!isDirectory()) {
576 for (ProjectFileImpl projectFile : childProjectFiles) {
577 if (projectFile.getName().equals(name)) {
585 * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
587 public List<ProjectFile> getFiles() {
588 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
597 * Adds a new project file as child to this project file.
600 * The name of the file
602 * The size of the file
604 * <code>true</code> if the new file is a directory,
605 * <code>false</code> otherwise
607 * <code>true</code> if the new file is hidden,
608 * <code>false</code> otherwise
609 * @return The created project file
611 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
612 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
613 childProjectFiles.add(newProjectFile);
614 return newProjectFile;
618 * Sorts the children of this file.
621 Collections.sort(childProjectFiles);
625 // INTERFACE Comparable
631 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
632 return name.compareTo(otherProjectFileImpl.name);