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.project;
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.util.beans.AbstractBean;
33 * Container for project information. A Project is capable of notifying
34 * {@link PropertyChangeListener}s if any of the contained properties change.
36 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38 public class Project extends AbstractBean {
40 /** Name of the “name” property. */
41 public static final String PROPERTY_NAME = "name";
43 /** Name of the “description” property. */
44 public static final String PROPERTY_DESCRIPTION = "description";
46 /** Name of the “public key” property. */
47 public static final String PROPERTY_PUBLIC_KEY = "publicKey";
49 /** Name of the “private key” property. */
50 public static final String PROPERTY_PRIVATE_KEY = "privateKey";
52 /** Name of the “base path” property. */
53 public static final String PROPERTY_BASE_PATH = "basePath";
55 /** Name of the “default file” property. */
56 public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
61 /** The name of the project. */
64 /** The description of the project. */
65 private String description;
67 /** The public key. */
68 private String publicKey;
70 /** The private key. */
71 private String privateKey;
73 /** The base path of the project. */
74 private String basePath;
76 /** The default file. */
77 private String defaultFile;
80 private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
82 /** The current root project file. */
83 private ProjectFileImpl rootProjectFile;
86 * Creates a new project.
93 * Clones the given project.
97 Project(Project project) {
98 this.name = project.name;
99 this.description = project.description;
100 this.publicKey = project.publicKey;
101 this.privateKey = project.privateKey;
102 this.basePath = project.basePath;
106 * Returns the internal ID.
108 * @return The internal ID
115 * Sets the internal ID.
120 void setId(String id) {
125 * Returns the name of the project.
127 * @return The name of the project
129 public String getName() {
134 * Sets the name of the project.
137 * The name of the project
139 public void setName(String name) {
140 String oldName = this.name;
142 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
146 * Returns the description of the project.
148 * @return The description of the project
150 public String getDescription() {
155 * Sets the description of the project
158 * The description of the project
160 public void setDescription(String description) {
161 String oldDescription = this.description;
162 this.description = description;
163 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
167 * Returns the public key of the project.
169 * @return The public key of the project
171 public String getPublicKey() {
176 * Sets the public key of the project.
179 * The public key of the project
181 void setPublicKey(String publicKey) {
182 String oldPublicKey = this.publicKey;
183 this.publicKey = publicKey;
184 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
188 * Returns the private key of the project.
190 * @return The private key of the project
192 public String getPrivateKey() {
197 * Sets the private key of the project.
200 * The private key of the project
202 void setPrivateKey(String privateKey) {
203 String oldPrivateKey = this.privateKey;
204 this.privateKey = privateKey;
205 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
209 * Returns the base path of the project.
211 * @return The base path of the project
213 public String getBasePath() {
218 * Sets the base path of the project.
221 * The base path of the project
223 public void setBasePath(String basePath) {
224 String oldBasePath = this.basePath;
225 this.basePath = basePath;
226 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
230 * Returns the default file.
232 * @return The default file
234 public String getDefaultFile() {
239 * Sets the default file.
244 public void setDefaultFile(String defaultFile) {
245 String oldDefaultFile = this.defaultFile;
246 this.defaultFile = defaultFile;
247 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
251 * Adds a file override for the given file.
256 * The override for the file
258 public void addFileOverride(String filePath, FileOverride override) {
259 fileOverrides.put(filePath, override);
263 * Removes the file override for the given file.
266 * The file path for which to remove the override
268 public void removeFileOverride(String filePath) {
269 fileOverrides.remove(filePath);
273 * Returns the file override for the given file.
276 * The file for which to get the override
277 * @return The file override, or <code>null</code> if the given file does
278 * not have an override
280 public FileOverride getFileOverride(ProjectFile projectFile) {
281 return fileOverrides.get(projectFile.getCompletePath());
285 * Returns the file override for the given file.
288 * The file path for which to get the override
289 * @return The file override, or <code>null</code> if the given file does
290 * not have an override
292 public FileOverride getFileOverride(String filePath) {
293 return fileOverrides.get(filePath);
297 * Returns the list of {@link FileOverride}s.
299 * @return All file overrides
301 public Map<String, FileOverride> getFileOverrides() {
302 return fileOverrides;
306 * Scans the base path for files and returns the {@link ProjectFile} for the
307 * base path. From this file it is possible to reach all files in the base
308 * path. This method is disk-intensive and may take some time on larger
311 * @return The file for the base path, or <code>null</code> if the base
312 * path does not denote an existing directory
314 public ProjectFile getBaseFile() {
315 File basePathFile = new File(basePath);
316 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
319 rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
320 scanDirectory(basePathFile, rootProjectFile);
321 return rootProjectFile;
325 * Returns the file that is specified by its complete path.
327 * @param completePath
328 * The complete path of the file
329 * @return The project file at the given path, or <code>null</code> if
330 * there is no such file
332 public ProjectFile getFile(String completePath) {
333 if (rootProjectFile == null) {
336 if ((rootProjectFile == null) || (completePath.length() == 0)) {
337 return rootProjectFile;
339 String[] pathParts = completePath.split("\\" + File.separator);
340 ProjectFileImpl currentProjectFile = rootProjectFile;
341 for (String pathPart: pathParts) {
342 currentProjectFile = currentProjectFile.getFile(pathPart);
343 if (currentProjectFile == null) {
347 return currentProjectFile;
355 * Scans the given directory and recreates the file and directory structure
356 * in the given project file.
359 * The directory to scan
361 * The project file in which to recreate the directory and file
364 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
365 if (!directory.isDirectory()) {
368 for (File file: directory.listFiles()) {
369 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
370 if (file.isDirectory()) {
371 scanDirectory(file, projectFileChild);
378 * Implementation of a {@link ProjectFile}.
380 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
382 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
384 /** The parent of this project file. */
385 private final ProjectFileImpl parentProjectFile;
387 /** The name of this project file. */
388 private final String name;
390 /** The size of the file. */
391 private final long size;
393 /** Whether this project file is a directory. */
394 private final boolean directory;
396 /** Whether this file is hidden. */
397 private final boolean hidden;
399 /** This project file’s children. */
400 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
403 * Creates a new project fie.
405 * @param parentProjectFile
406 * The parent of the project file, or <code>null</code> if
407 * the new project file does not have a parent
409 * The name of the project file
411 * The size of the file
413 * <code>true</code> if this project file is a directory,
414 * <code>false</code> otherwise
416 * <code>true</code> if this project file is hidden,
417 * <code>false</code> otherwise
419 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
420 this.parentProjectFile = parentProjectFile;
423 this.directory = isDirectory;
424 this.hidden = isHidden;
428 // INTERFACE ProjectFile
432 * @see net.pterodactylus.jsite.project.ProjectFile#getName()
434 public String getName() {
439 * @see net.pterodactylus.jsite.project.ProjectFile#getParent()
441 public ProjectFile getParent() {
442 return parentProjectFile;
448 public long getSize() {
453 * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
455 public List<ProjectFile> getParents() {
456 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
457 ProjectFileImpl currentProjectFile = this;
459 parentProjectFiles.add(0, currentProjectFile);
460 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
461 return parentProjectFiles;
467 /* TODO - caching? */
468 public String getCompletePath() {
469 StringBuilder completePath = new StringBuilder();
470 ProjectFileImpl currentProjectFile = this;
471 while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
472 completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
473 currentProjectFile = currentProjectFile.parentProjectFile;
475 return (completePath.length() > 0) ? completePath.substring(1) : "";
479 * @see net.pterodactylus.jsite.project.ProjectFile#isFile()
481 public boolean isFile() {
486 * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
488 public boolean isDirectory() {
493 * @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
495 public boolean isHidden() {
500 * Returns the project file with the given name. The project file has to
501 * be a direct child of this project file.
504 * The name of the file to get
505 * @return The project file, or <code>null</code> if there is no
506 * project file by that name
508 public ProjectFileImpl getFile(String name) {
509 if (!isDirectory()) {
512 for (ProjectFileImpl projectFile: childProjectFiles) {
513 if (projectFile.getName().equals(name)) {
521 * @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
523 public List<ProjectFile> getFiles() {
524 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
533 * Adds a new project file as child to this project file.
536 * The name of the file
538 * The size of the file
540 * <code>true</code> if the new file is a directory,
541 * <code>false</code> otherwise
543 * <code>true</code> if the new file is hidden,
544 * <code>false</code> otherwise
545 * @return The created project file
547 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
548 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
549 childProjectFiles.add(newProjectFile);
550 return newProjectFile;
554 * Sorts the children of this file.
557 Collections.sort(childProjectFiles);
561 // INTERFACE Comparable
567 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
568 return name.compareTo(otherProjectFileImpl.name);