package net.pterodactylus.jsite.project;
import java.beans.PropertyChangeListener;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Container for project information. A Project is capable of notifying
* {@link PropertyChangeListener}s if any of the contained properties change.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class Project extends AbstractBean {
private String defaultFile;
/** The overrides. */
- private final Map<String, Override> overrides = new HashMap<String, Override>();
+ private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
+
+ /** The current root project file. */
+ private ProjectFileImpl rootProjectFile;
/**
* Creates a new project.
/**
* Clones the given project.
- *
+ *
* @param project
*/
Project(Project project) {
/**
* Returns the internal ID.
- *
+ *
* @return The internal ID
*/
String getId() {
/**
* Sets the internal ID.
- *
+ *
* @param id
* The internal ID
*/
/**
* Returns the name of the project.
- *
+ *
* @return The name of the project
*/
public String getName() {
/**
* Sets the name of the project.
- *
+ *
* @param name
* The name of the project
*/
/**
* Returns the description of the project.
- *
+ *
* @return The description of the project
*/
public String getDescription() {
/**
* Sets the description of the project
- *
+ *
* @param description
* The description of the project
*/
/**
* Returns the public key of the project.
- *
+ *
* @return The public key of the project
*/
public String getPublicKey() {
/**
* Sets the public key of the project.
- *
+ *
* @param publicKey
* The public key of the project
*/
/**
* Returns the private key of the project.
- *
+ *
* @return The private key of the project
*/
public String getPrivateKey() {
/**
* Sets the private key of the project.
- *
+ *
* @param privateKey
* The private key of the project
*/
/**
* Returns the base path of the project.
- *
+ *
* @return The base path of the project
*/
public String getBasePath() {
/**
* Sets the base path of the project.
- *
+ *
* @param basePath
* The base path of the project
*/
/**
* Returns the default file.
- *
+ *
* @return The default file
*/
public String getDefaultFile() {
/**
* Sets the default file.
- *
+ *
* @param defaultFile
* The default file
*/
}
/**
- * Adds an override for the given file.
- *
+ * Adds a file override for the given file.
+ *
+ * @param projectFile
+ * The file
+ * @param override
+ * The override for the file
+ */
+ public void addFileOverride(ProjectFile projectFile, FileOverride override) {
+ addFileOverride(projectFile.getCompletePath(), override);
+ }
+
+ /**
+ * Adds a file override for the given file.
+ *
* @param filePath
* The file path
* @param override
* The override for the file
*/
- public void addOverride(String filePath, Override override) {
- overrides.put(filePath, override);
+ public void addFileOverride(String filePath, FileOverride override) {
+ fileOverrides.put(filePath, override);
}
/**
- * Removes the override for the given file.
- *
+ * Removes the file override for the given file.
+ *
+ * @param projectFile
+ * The file for which to remove the override
+ */
+ public void removeFileOverride(ProjectFile projectFile) {
+ removeFileOverride(projectFile.getCompletePath());
+ }
+
+ /**
+ * Removes the file override for the given file.
+ *
* @param filePath
* The file path for which to remove the override
*/
- public void removeOverride(String filePath) {
- overrides.remove(filePath);
+ public void removeFileOverride(String filePath) {
+ fileOverrides.remove(filePath);
+ }
+
+ /**
+ * Returns the file override for the given file.
+ *
+ * @param projectFile
+ * The file for which to get the override
+ * @return The file override, or <code>null</code> if the given file does
+ * not have an override
+ */
+ public FileOverride getFileOverride(ProjectFile projectFile) {
+ return getFileOverride(projectFile.getCompletePath());
+ }
+
+ /**
+ * Returns the file override for the given file.
+ *
+ * @param filePath
+ * The file path for which to get the override
+ * @return The file override, or <code>null</code> if the given file does
+ * not have an override
+ */
+ public FileOverride getFileOverride(String filePath) {
+ return fileOverrides.get(filePath);
}
/**
- * Returns the list of {@link Override}s.
- *
- * @return All overrides
+ * Returns the list of {@link FileOverride}s.
+ *
+ * @return All file overrides
*/
- public Map<String, Override> getOverrides() {
- return overrides;
+ public Map<String, FileOverride> getFileOverrides() {
+ return fileOverrides;
}
/**
* Scans the base path for files and returns the {@link ProjectFile} for the
* base path. From this file it is possible to reach all files in the base
- * path.
- *
- * This method is disk-intensive and may take some time on larger
+ * path. This method is disk-intensive and may take some time on larger
* directories!
- *
- * @return The file for the base path
+ *
+ * @return The file for the base path, or <code>null</code> if the base
+ * path does not denote an existing directory
*/
public ProjectFile getBaseFile() {
+ File basePathFile = new File(basePath);
+ if (!basePathFile.exists() || !basePathFile.isDirectory()) {
+ return null;
+ }
+ rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
+ scanDirectory(basePathFile, rootProjectFile);
+ return rootProjectFile;
+ }
+
+ /**
+ * Returns the file that is specified by its complete path.
+ *
+ * @param completePath
+ * The complete path of the file
+ * @return The project file at the given path, or <code>null</code> if
+ * there is no such file
+ */
+ public ProjectFile getFile(String completePath) {
+ if (rootProjectFile == null) {
+ getBaseFile();
+ }
+ if ((rootProjectFile == null) || (completePath.length() == 0)) {
+ return rootProjectFile;
+ }
+ String[] pathParts = completePath.split("\\" + File.separator);
+ ProjectFileImpl currentProjectFile = rootProjectFile;
+ for (String pathPart : pathParts) {
+ currentProjectFile = currentProjectFile.getFile(pathPart);
+ if (currentProjectFile == null) {
+ return null;
+ }
+ }
+ return currentProjectFile;
+ }
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Scans the given directory and recreates the file and directory structure
+ * in the given project file.
+ *
+ * @param directory
+ * The directory to scan
+ * @param projectFile
+ * The project file in which to recreate the directory and file
+ * structure
+ */
+ private void scanDirectory(File directory, ProjectFileImpl projectFile) {
+ if (!directory.isDirectory()) {
+ return;
+ }
+ for (File file : directory.listFiles()) {
+ ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
+ if (file.isDirectory()) {
+ scanDirectory(file, projectFileChild);
+ }
+ }
+ projectFile.sort();
}
/**
* Implementation of a {@link ProjectFile}.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
- private static class ProjectFileImpl implements ProjectFile {
+ private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
+
+ /** The parent of this project file. */
+ private final ProjectFileImpl parentProjectFile;
+
+ /** The name of this project file. */
+ private final String name;
+
+ /** The size of the file. */
+ private final long size;
+
+ /** Whether this project file is a directory. */
+ private final boolean directory;
+
+ /** Whether this file is hidden. */
+ private final boolean hidden;
+
+ /** This project file’s children. */
+ private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
+
+ /**
+ * Creates a new project fie.
+ *
+ * @param parentProjectFile
+ * The parent of the project file, or <code>null</code> if
+ * the new project file does not have a parent
+ * @param name
+ * The name of the project file
+ * @param size
+ * The size of the file
+ * @param isDirectory
+ * <code>true</code> if this project file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if this project file is hidden,
+ * <code>false</code> otherwise
+ */
+ ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
+ this.parentProjectFile = parentProjectFile;
+ this.name = name;
+ this.size = size;
+ this.directory = isDirectory;
+ this.hidden = isHidden;
+ }
+
+ //
+ // INTERFACE ProjectFile
+ //
/**
* @see net.pterodactylus.jsite.project.ProjectFile#getName()
*/
public String getName() {
- // TODO Auto-generated method stub
- return null;
+ return name;
+ }
+
+ /**
+ * @see net.pterodactylus.jsite.project.ProjectFile#getParent()
+ */
+ public ProjectFile getParent() {
+ return parentProjectFile;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getSize() {
+ return size;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#getParents()
*/
public List<ProjectFile> getParents() {
- // TODO Auto-generated method stub
- return null;
+ List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
+ ProjectFileImpl currentProjectFile = this;
+ do {
+ parentProjectFiles.add(0, currentProjectFile);
+ } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
+ return parentProjectFiles;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ /* TODO - caching? */
+ public String getCompletePath() {
+ StringBuilder completePath = new StringBuilder();
+ ProjectFileImpl currentProjectFile = this;
+ while ((currentProjectFile != null) && (currentProjectFile.parentProjectFile != null)) {
+ completePath.insert(0, currentProjectFile.getName()).insert(0, File.separatorChar);
+ currentProjectFile = currentProjectFile.parentProjectFile;
+ }
+ return (completePath.length() > 0) ? completePath.substring(1) : "";
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#isFile()
*/
public boolean isFile() {
- // TODO Auto-generated method stub
- return false;
+ return !directory;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
*/
public boolean isDirectory() {
- // TODO Auto-generated method stub
- return false;
+ return directory;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
*/
public boolean isHidden() {
- // TODO Auto-generated method stub
- return false;
+ return hidden;
+ }
+
+ /**
+ * Returns the project file with the given name. The project file has to
+ * be a direct child of this project file.
+ *
+ * @param name
+ * The name of the file to get
+ * @return The project file, or <code>null</code> if there is no
+ * project file by that name
+ */
+ public ProjectFileImpl getFile(String name) {
+ if (!isDirectory()) {
+ return null;
+ }
+ for (ProjectFileImpl projectFile : childProjectFiles) {
+ if (projectFile.getName().equals(name)) {
+ return projectFile;
+ }
+ }
+ return null;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
*/
public List<ProjectFile> getFiles() {
- // TODO Auto-generated method stub
- return null;
+ List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
+ return projectFiles;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Adds a new project file as child to this project file.
+ *
+ * @param name
+ * The name of the file
+ * @param size
+ * The size of the file
+ * @param isDirectory
+ * <code>true</code> if the new file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if the new file is hidden,
+ * <code>false</code> otherwise
+ * @return The created project file
+ */
+ public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
+ ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
+ childProjectFiles.add(newProjectFile);
+ return newProjectFile;
}
+
+ /**
+ * Sorts the children of this file.
+ */
+ public void sort() {
+ Collections.sort(childProjectFiles);
+ }
+
+ //
+ // INTERFACE Comparable
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public int compareTo(ProjectFileImpl otherProjectFileImpl) {
+ return name.compareTo(otherProjectFileImpl.name);
+ }
+
}
}