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.core.Node;
31 import net.pterodactylus.jsite.util.IdGenerator;
32 import net.pterodactylus.util.beans.AbstractBean;
33 import net.pterodactylus.util.number.Hex;
36 * Container for project information. A Project is capable of notifying
37 * {@link PropertyChangeListener}s if any of the contained properties change.
39 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41 public class Project extends AbstractBean {
43 /** Name of the “name” property. */
44 public static final String PROPERTY_NAME = "name";
46 /** Name of the “description” property. */
47 public static final String PROPERTY_DESCRIPTION = "description";
49 /** Name of the “public key” property. */
50 public static final String PROPERTY_PUBLIC_KEY = "publicKey";
52 /** Name of the “private key” property. */
53 public static final String PROPERTY_PRIVATE_KEY = "privateKey";
55 /** Name of the “base path” property. */
56 public static final String PROPERTY_BASE_PATH = "basePath";
58 /** Name of the “default file” property. */
59 public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
64 /** The name of the project. */
67 /** The description of the project. */
68 private String description;
70 /** The public key. */
71 private String publicKey;
73 /** The private key. */
74 private String privateKey;
76 /** The base path of the project. */
77 private String basePath;
79 /** The default file. */
80 private String defaultFile;
83 private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
85 /** The current root project file. */
86 private ProjectFileImpl rootProjectFile;
89 * Creates a new project.
92 id = Hex.toHex(IdGenerator.generateId());
96 * Clones the given project.
100 Project(Project project) {
102 this.name = project.name;
103 this.description = project.description;
104 this.publicKey = project.publicKey;
105 this.privateKey = project.privateKey;
106 this.basePath = project.basePath;
110 * Returns the internal ID.
112 * @return The internal ID
119 * Sets the internal ID.
124 void setId(String id) {
129 * Returns the name of the project.
131 * @return The name of the project
133 public String getName() {
138 * Sets the name of the project.
141 * The name of the project
143 public void setName(String name) {
144 String oldName = this.name;
146 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
150 * Returns the description of the project.
152 * @return The description of the project
154 public String getDescription() {
159 * Sets the description of the project
162 * The description of the project
164 public void setDescription(String description) {
165 String oldDescription = this.description;
166 this.description = description;
167 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
171 * Returns the public key of the project.
173 * @return The public key of the project
175 public String getPublicKey() {
180 * Sets the public key of the project.
183 * The public key of the project
185 void setPublicKey(String publicKey) {
186 String oldPublicKey = this.publicKey;
187 this.publicKey = publicKey;
188 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
192 * Returns the private key of the project.
194 * @return The private key of the project
196 public String getPrivateKey() {
201 * Sets the private key of the project.
204 * The private key of the project
206 void setPrivateKey(String privateKey) {
207 String oldPrivateKey = this.privateKey;
208 this.privateKey = privateKey;
209 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
213 * Returns the base path of the project.
215 * @return The base path of the project
217 public String getBasePath() {
222 * Sets the base path of the project.
225 * The base path of the project
227 public void setBasePath(String basePath) {
228 String oldBasePath = this.basePath;
229 this.basePath = basePath;
230 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
234 * Returns the default file.
236 * @return The default file
238 public String getDefaultFile() {
243 * Sets the default file.
248 public void setDefaultFile(String defaultFile) {
249 String oldDefaultFile = this.defaultFile;
250 this.defaultFile = defaultFile;
251 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
255 * Adds a file override for the given file.
260 * The override for the file
262 public void addFileOverride(ProjectFile projectFile, FileOverride override) {
263 addFileOverride(projectFile.getCompletePath(), override);
267 * Adds a file override for the given file.
272 * The override for the file
274 public void addFileOverride(String filePath, FileOverride override) {
275 fileOverrides.put(filePath, override);
279 * Removes the file override for the given file.
282 * The file for which to remove the override
284 public void removeFileOverride(ProjectFile projectFile) {
285 removeFileOverride(projectFile.getCompletePath());
289 * Removes the file override for the given file.
292 * The file path for which to remove the override
294 public void removeFileOverride(String filePath) {
295 fileOverrides.remove(filePath);
299 * Returns the file override for the given file.
302 * The file for which to get the override
303 * @return The file override, or <code>null</code> if the given file does
304 * not have an override
306 public FileOverride getFileOverride(ProjectFile projectFile) {
307 return getFileOverride(projectFile.getCompletePath());
311 * Returns the file override for the given file.
314 * The file path for which to get the override
315 * @return The file override, or <code>null</code> if the given file does
316 * not have an override
318 public FileOverride getFileOverride(String filePath) {
319 return fileOverrides.get(filePath);
323 * Returns the list of {@link FileOverride}s.
325 * @return All file overrides
327 public Map<String, FileOverride> getFileOverrides() {
328 return fileOverrides;
332 * Scans the base path for files and returns the {@link ProjectFile} for the
333 * base path. From this file it is possible to reach all files in the base
334 * path. This method is disk-intensive and may take some time on larger
337 * @return The file for the base path, or <code>null</code> if the base
338 * path does not denote an existing directory
340 public ProjectFile getBaseFile() {
341 File basePathFile = new File(basePath);
342 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
345 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
346 scanDirectory(basePathFile, rootProjectFile);
347 return rootProjectFile;
351 * Returns the file that is specified by its complete path.
353 * @param completePath
354 * The complete path of the file
355 * @return The project file at the given path, or <code>null</code> if
356 * there is no such file
358 public ProjectFile getFile(String completePath) {
359 if (rootProjectFile == null) {
362 if ((rootProjectFile == null) || (completePath.length() == 0)) {
363 return rootProjectFile;
365 String[] pathParts = completePath.split("\\" + File.separator);
366 ProjectFileImpl currentProjectFile = rootProjectFile;
367 for (String pathPart : pathParts) {
368 currentProjectFile = currentProjectFile.getFile(pathPart);
369 if (currentProjectFile == null) {
373 return currentProjectFile;
381 * Scans the given directory and recreates the file and directory structure
382 * in the given project file.
385 * The directory to scan
387 * The project file in which to recreate the directory and file
390 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
391 if (!directory.isDirectory()) {
394 for (File file : directory.listFiles()) {
395 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
396 if (file.isDirectory()) {
397 scanDirectory(file, projectFileChild);
404 * Implementation of a {@link ProjectFile}.
406 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
408 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
410 /** The parent of this project file. */
411 private final ProjectFileImpl parentProjectFile;
413 /** The name of this project file. */
414 private final String name;
416 /** The size of the file. */
417 private final long size;
419 /** Whether this project file is a directory. */
420 private final boolean directory;
422 /** Whether this file is hidden. */
423 private final boolean hidden;
425 /** This project file’s children. */
426 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
429 * Creates a new project fie.
431 * @param parentProjectFile
432 * The parent of the project file, or <code>null</code> if
433 * the new project file does not have a parent
435 * The name of the project file
437 * The size of the file
439 * <code>true</code> if this project file is a directory,
440 * <code>false</code> otherwise
442 * <code>true</code> if this project file is hidden,
443 * <code>false</code> otherwise
445 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
446 this.parentProjectFile = parentProjectFile;
449 this.directory = isDirectory;
450 this.hidden = isHidden;
454 // INTERFACE ProjectFile
458 * @see net.pterodactylus.jsite.core.ProjectFile#getName()
460 public String getName() {
465 * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
467 public ProjectFile getParent() {
468 return parentProjectFile;
474 public long getSize() {
479 * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
481 public List<ProjectFile> getParents() {
482 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
483 ProjectFileImpl currentProjectFile = this;
485 parentProjectFiles.add(0, currentProjectFile);
486 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
487 return parentProjectFiles;
493 /* TODO - caching? */
494 public String getCompletePath() {
495 StringBuilder completePath = new StringBuilder();
496 ProjectFileImpl currentProjectFile = this;
497 while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
498 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
499 currentProjectFile = currentProjectFile.parentProjectFile;
501 return (completePath.length() > 0) ? completePath.substring(1) : "";
505 * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
507 public boolean isFile() {
512 * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
514 public boolean isDirectory() {
519 * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
521 public boolean isHidden() {
526 * Returns the project file with the given name. The project file has to
527 * be a direct child of this project file.
530 * The name of the file to get
531 * @return The project file, or <code>null</code> if there is no
532 * project file by that name
534 public ProjectFileImpl getFile(String name) {
535 if (!isDirectory()) {
538 for (ProjectFileImpl projectFile : childProjectFiles) {
539 if (projectFile.getName().equals(name)) {
547 * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
549 public List<ProjectFile> getFiles() {
550 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
559 * Adds a new project file as child to this project file.
562 * The name of the file
564 * The size of the file
566 * <code>true</code> if the new file is a directory,
567 * <code>false</code> otherwise
569 * <code>true</code> if the new file is hidden,
570 * <code>false</code> otherwise
571 * @return The created project file
573 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
574 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
575 childProjectFiles.add(newProjectFile);
576 return newProjectFile;
580 * Sorts the children of this file.
583 Collections.sort(childProjectFiles);
587 // INTERFACE Comparable
593 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
594 return name.compareTo(otherProjectFileImpl.name);