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) {
134 * Returns the name of the project.
136 * @return The name of the project
138 public String getName() {
143 * Sets the name of the project.
146 * The name of the project
148 public void setName(String name) {
149 String oldName = this.name;
151 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
155 * Returns the description of the project.
157 * @return The description of the project
159 public String getDescription() {
164 * Sets the description of the project
167 * The description of the project
169 public void setDescription(String description) {
170 String oldDescription = this.description;
171 this.description = description;
172 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
176 * Returns the public key of the project.
178 * @return The public key of the project
180 public String getPublicKey() {
185 * Sets the public key of the project.
188 * The public key of the project
190 void setPublicKey(String publicKey) {
191 String oldPublicKey = this.publicKey;
192 this.publicKey = publicKey;
193 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
197 * Returns the private key of the project.
199 * @return The private key of the project
201 public String getPrivateKey() {
206 * Sets the private key of the project.
209 * The private key of the project
211 void setPrivateKey(String privateKey) {
212 String oldPrivateKey = this.privateKey;
213 this.privateKey = privateKey;
214 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
218 * Returns the base path of the project.
220 * @return The base path of the project
222 public String getBasePath() {
227 * Sets the base path of the project.
230 * The base path of the project
232 public void setBasePath(String basePath) {
233 String oldBasePath = this.basePath;
234 this.basePath = basePath;
235 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
239 * Returns the default file.
241 * @return The default file
243 public String getDefaultFile() {
248 * Sets the default file.
253 public void setDefaultFile(String defaultFile) {
254 String oldDefaultFile = this.defaultFile;
255 this.defaultFile = defaultFile;
256 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
260 * Adds a file override for the given file.
265 * The override for the file
267 public void addFileOverride(ProjectFile projectFile, FileOverride override) {
268 addFileOverride(projectFile.getCompletePath(), override);
272 * Adds a file override for the given file.
277 * The override for the file
279 public void addFileOverride(String filePath, FileOverride override) {
280 fileOverrides.put(filePath, override);
284 * Removes the file override for the given file.
287 * The file for which to remove the override
289 public void removeFileOverride(ProjectFile projectFile) {
290 removeFileOverride(projectFile.getCompletePath());
294 * Removes the file override for the given file.
297 * The file path for which to remove the override
299 public void removeFileOverride(String filePath) {
300 fileOverrides.remove(filePath);
304 * Returns the file override for the given file.
307 * The file for which to get the override
308 * @return The file override, or <code>null</code> if the given file does
309 * not have an override
311 public FileOverride getFileOverride(ProjectFile projectFile) {
312 return getFileOverride(projectFile.getCompletePath());
316 * Returns the file override for the given file.
319 * The file path for which to get the override
320 * @return The file override, or <code>null</code> if the given file does
321 * not have an override
323 public FileOverride getFileOverride(String filePath) {
324 return fileOverrides.get(filePath);
328 * Returns the list of {@link FileOverride}s.
330 * @return All file overrides
332 public Map<String, FileOverride> getFileOverrides() {
333 return fileOverrides;
337 * Scans the base path for files and returns the {@link ProjectFile} for the
338 * base path. From this file it is possible to reach all files in the base
339 * path. This method is disk-intensive and may take some time on larger
342 * @return The file for the base path, or <code>null</code> if the base
343 * path does not denote an existing directory
345 public ProjectFile getBaseFile() {
346 File basePathFile = new File(basePath);
347 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
350 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
351 scanDirectory(basePathFile, rootProjectFile);
352 return rootProjectFile;
356 * Returns the file that is specified by its complete path.
358 * @param completePath
359 * The complete path of the file
360 * @return The project file at the given path, or <code>null</code> if
361 * there is no such file
363 public ProjectFile getFile(String completePath) {
364 if (rootProjectFile == null) {
367 if ((rootProjectFile == null) || (completePath.length() == 0)) {
368 return rootProjectFile;
370 String[] pathParts = completePath.split("\\" + File.separator);
371 ProjectFileImpl currentProjectFile = rootProjectFile;
372 for (String pathPart : pathParts) {
373 currentProjectFile = currentProjectFile.getFile(pathPart);
374 if (currentProjectFile == null) {
378 return currentProjectFile;
382 * Returns the default node to insert this project to.
384 * @return The node to insert this project to
386 public Node getNode() {
391 * Sets the default node to insert this project to.
394 * The node to insert this project to
396 public void setNode(Node node) {
397 Node oldNode = this.node;
399 fireIfPropertyChanged(PROPERTY_NODE, oldNode, node);
403 * @see java.lang.Object#toString()
406 public String toString() {
407 return getClass().getName() + "[id=" + id + ",name=" + name + ",description=" + description + ",publicKey=" + publicKey + ",privateKey=" + privateKey + ",basePath=" + basePath + ",defaultFile=" + defaultFile + ",node=" + node + "]";
415 * Scans the given directory and recreates the file and directory structure
416 * in the given project file.
419 * The directory to scan
421 * The project file in which to recreate the directory and file
424 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
425 if (!directory.isDirectory()) {
428 for (File file : directory.listFiles()) {
429 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
430 if (file.isDirectory()) {
431 scanDirectory(file, projectFileChild);
438 * Implementation of a {@link ProjectFile}.
440 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
442 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
444 /** The parent of this project file. */
445 private final ProjectFileImpl parentProjectFile;
447 /** The name of this project file. */
448 private final String name;
450 /** The size of the file. */
451 private final long size;
453 /** Whether this project file is a directory. */
454 private final boolean directory;
456 /** Whether this file is hidden. */
457 private final boolean hidden;
459 /** This project file’s children. */
460 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
463 * Creates a new project fie.
465 * @param parentProjectFile
466 * The parent of the project file, or <code>null</code> if
467 * the new project file does not have a parent
469 * The name of the project file
471 * The size of the file
473 * <code>true</code> if this project file is a directory,
474 * <code>false</code> otherwise
476 * <code>true</code> if this project file is hidden,
477 * <code>false</code> otherwise
479 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
480 this.parentProjectFile = parentProjectFile;
483 this.directory = isDirectory;
484 this.hidden = isHidden;
488 // INTERFACE ProjectFile
492 * @see net.pterodactylus.jsite.core.ProjectFile#getName()
494 public String getName() {
499 * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
501 public ProjectFile getParent() {
502 return parentProjectFile;
508 public long getSize() {
513 * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
515 public List<ProjectFile> getParents() {
516 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
517 ProjectFileImpl currentProjectFile = this;
519 parentProjectFiles.add(0, currentProjectFile);
520 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
521 return parentProjectFiles;
527 /* TODO - caching? */
528 public String getCompletePath() {
529 StringBuilder completePath = new StringBuilder();
530 ProjectFileImpl currentProjectFile = this;
531 while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
532 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
533 currentProjectFile = currentProjectFile.parentProjectFile;
535 return (completePath.length() > 0) ? completePath.substring(1) : "";
539 * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
541 public boolean isFile() {
546 * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
548 public boolean isDirectory() {
553 * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
555 public boolean isHidden() {
560 * Returns the project file with the given name. The project file has to
561 * be a direct child of this project file.
564 * The name of the file to get
565 * @return The project file, or <code>null</code> if there is no
566 * project file by that name
568 public ProjectFileImpl getFile(String name) {
569 if (!isDirectory()) {
572 for (ProjectFileImpl projectFile : childProjectFiles) {
573 if (projectFile.getName().equals(name)) {
581 * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
583 public List<ProjectFile> getFiles() {
584 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
593 * Adds a new project file as child to this project file.
596 * The name of the file
598 * The size of the file
600 * <code>true</code> if the new file is a directory,
601 * <code>false</code> otherwise
603 * <code>true</code> if the new file is hidden,
604 * <code>false</code> otherwise
605 * @return The created project file
607 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
608 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
609 childProjectFiles.add(newProjectFile);
610 return newProjectFile;
614 * Sorts the children of this file.
617 Collections.sort(childProjectFiles);
621 // INTERFACE Comparable
627 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
628 return name.compareTo(otherProjectFileImpl.name);