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 default node to insert to. */
88 /** The current root project file. */
89 private ProjectFileImpl rootProjectFile;
92 * Creates a new project.
95 id = Hex.toHex(IdGenerator.generateId());
99 * Clones the given project.
103 Project(Project project) {
105 this.name = project.name;
106 this.description = project.description;
107 this.publicKey = project.publicKey;
108 this.privateKey = project.privateKey;
109 this.basePath = project.basePath;
113 * Returns the internal ID.
115 * @return The internal ID
122 * Sets the internal ID.
127 void setId(String id) {
132 * Returns the name of the project.
134 * @return The name of the project
136 public String getName() {
141 * Sets the name of the project.
144 * The name of the project
146 public void setName(String name) {
147 String oldName = this.name;
149 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
153 * Returns the description of the project.
155 * @return The description of the project
157 public String getDescription() {
162 * Sets the description of the project
165 * The description of the project
167 public void setDescription(String description) {
168 String oldDescription = this.description;
169 this.description = description;
170 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
174 * Returns the public key of the project.
176 * @return The public key of the project
178 public String getPublicKey() {
183 * Sets the public key of the project.
186 * The public key of the project
188 void setPublicKey(String publicKey) {
189 String oldPublicKey = this.publicKey;
190 this.publicKey = publicKey;
191 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
195 * Returns the private key of the project.
197 * @return The private key of the project
199 public String getPrivateKey() {
204 * Sets the private key of the project.
207 * The private key of the project
209 void setPrivateKey(String privateKey) {
210 String oldPrivateKey = this.privateKey;
211 this.privateKey = privateKey;
212 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
216 * Returns the base path of the project.
218 * @return The base path of the project
220 public String getBasePath() {
225 * Sets the base path of the project.
228 * The base path of the project
230 public void setBasePath(String basePath) {
231 String oldBasePath = this.basePath;
232 this.basePath = basePath;
233 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
237 * Returns the default file.
239 * @return The default file
241 public String getDefaultFile() {
246 * Sets the default file.
251 public void setDefaultFile(String defaultFile) {
252 String oldDefaultFile = this.defaultFile;
253 this.defaultFile = defaultFile;
254 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
258 * Adds a file override for the given file.
263 * The override for the file
265 public void addFileOverride(ProjectFile projectFile, FileOverride override) {
266 addFileOverride(projectFile.getCompletePath(), override);
270 * Adds a file override for the given file.
275 * The override for the file
277 public void addFileOverride(String filePath, FileOverride override) {
278 fileOverrides.put(filePath, override);
282 * Removes the file override for the given file.
285 * The file for which to remove the override
287 public void removeFileOverride(ProjectFile projectFile) {
288 removeFileOverride(projectFile.getCompletePath());
292 * Removes the file override for the given file.
295 * The file path for which to remove the override
297 public void removeFileOverride(String filePath) {
298 fileOverrides.remove(filePath);
302 * Returns the file override for the given file.
305 * The file for which to get the override
306 * @return The file override, or <code>null</code> if the given file does
307 * not have an override
309 public FileOverride getFileOverride(ProjectFile projectFile) {
310 return getFileOverride(projectFile.getCompletePath());
314 * Returns the file override for the given file.
317 * The file path for which to get the override
318 * @return The file override, or <code>null</code> if the given file does
319 * not have an override
321 public FileOverride getFileOverride(String filePath) {
322 return fileOverrides.get(filePath);
326 * Returns the list of {@link FileOverride}s.
328 * @return All file overrides
330 public Map<String, FileOverride> getFileOverrides() {
331 return fileOverrides;
335 * Scans the base path for files and returns the {@link ProjectFile} for the
336 * base path. From this file it is possible to reach all files in the base
337 * path. This method is disk-intensive and may take some time on larger
340 * @return The file for the base path, or <code>null</code> if the base
341 * path does not denote an existing directory
343 public ProjectFile getBaseFile() {
344 File basePathFile = new File(basePath);
345 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
348 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
349 scanDirectory(basePathFile, rootProjectFile);
350 return rootProjectFile;
354 * Returns the file that is specified by its complete path.
356 * @param completePath
357 * The complete path of the file
358 * @return The project file at the given path, or <code>null</code> if
359 * there is no such file
361 public ProjectFile getFile(String completePath) {
362 if (rootProjectFile == null) {
365 if ((rootProjectFile == null) || (completePath.length() == 0)) {
366 return rootProjectFile;
368 String[] pathParts = completePath.split("\\" + File.separator);
369 ProjectFileImpl currentProjectFile = rootProjectFile;
370 for (String pathPart : pathParts) {
371 currentProjectFile = currentProjectFile.getFile(pathPart);
372 if (currentProjectFile == null) {
376 return currentProjectFile;
380 * Returns the default node to insert this project to.
382 * @return The node to insert this project to
384 public Node getNode() {
389 * Sets the default node to insert this project to.
392 * The node to insert this project to
394 void setNode(Node node) {
403 * Scans the given directory and recreates the file and directory structure
404 * in the given project file.
407 * The directory to scan
409 * The project file in which to recreate the directory and file
412 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
413 if (!directory.isDirectory()) {
416 for (File file : directory.listFiles()) {
417 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
418 if (file.isDirectory()) {
419 scanDirectory(file, projectFileChild);
426 * Implementation of a {@link ProjectFile}.
428 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
430 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
432 /** The parent of this project file. */
433 private final ProjectFileImpl parentProjectFile;
435 /** The name of this project file. */
436 private final String name;
438 /** The size of the file. */
439 private final long size;
441 /** Whether this project file is a directory. */
442 private final boolean directory;
444 /** Whether this file is hidden. */
445 private final boolean hidden;
447 /** This project file’s children. */
448 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
451 * Creates a new project fie.
453 * @param parentProjectFile
454 * The parent of the project file, or <code>null</code> if
455 * the new project file does not have a parent
457 * The name of the project file
459 * The size of the file
461 * <code>true</code> if this project file is a directory,
462 * <code>false</code> otherwise
464 * <code>true</code> if this project file is hidden,
465 * <code>false</code> otherwise
467 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
468 this.parentProjectFile = parentProjectFile;
471 this.directory = isDirectory;
472 this.hidden = isHidden;
476 // INTERFACE ProjectFile
480 * @see net.pterodactylus.jsite.core.ProjectFile#getName()
482 public String getName() {
487 * @see net.pterodactylus.jsite.core.ProjectFile#getParent()
489 public ProjectFile getParent() {
490 return parentProjectFile;
496 public long getSize() {
501 * @see net.pterodactylus.jsite.core.ProjectFile#getParents()
503 public List<ProjectFile> getParents() {
504 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
505 ProjectFileImpl currentProjectFile = this;
507 parentProjectFiles.add(0, currentProjectFile);
508 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
509 return parentProjectFiles;
515 /* TODO - caching? */
516 public String getCompletePath() {
517 StringBuilder completePath = new StringBuilder();
518 ProjectFileImpl currentProjectFile = this;
519 while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
520 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
521 currentProjectFile = currentProjectFile.parentProjectFile;
523 return (completePath.length() > 0) ? completePath.substring(1) : "";
527 * @see net.pterodactylus.jsite.core.ProjectFile#isFile()
529 public boolean isFile() {
534 * @see net.pterodactylus.jsite.core.ProjectFile#isDirectory()
536 public boolean isDirectory() {
541 * @see net.pterodactylus.jsite.core.ProjectFile#isHidden()
543 public boolean isHidden() {
548 * Returns the project file with the given name. The project file has to
549 * be a direct child of this project file.
552 * The name of the file to get
553 * @return The project file, or <code>null</code> if there is no
554 * project file by that name
556 public ProjectFileImpl getFile(String name) {
557 if (!isDirectory()) {
560 for (ProjectFileImpl projectFile : childProjectFiles) {
561 if (projectFile.getName().equals(name)) {
569 * @see net.pterodactylus.jsite.core.ProjectFile#getFiles()
571 public List<ProjectFile> getFiles() {
572 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
581 * Adds a new project file as child to this project file.
584 * The name of the file
586 * The size of the file
588 * <code>true</code> if the new file is a directory,
589 * <code>false</code> otherwise
591 * <code>true</code> if the new file is hidden,
592 * <code>false</code> otherwise
593 * @return The created project file
595 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
596 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
597 childProjectFiles.add(newProjectFile);
598 return newProjectFile;
602 * Sorts the children of this file.
605 Collections.sort(childProjectFiles);
609 // INTERFACE Comparable
615 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
616 return name.compareTo(otherProjectFileImpl.name);