From a78255dcb78274055e6c92c52a74aed0cbc23736 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 30 May 2008 00:35:16 +0200 Subject: [PATCH] move file scanning to project --- src/net/pterodactylus/jsite/project/Project.java | 158 ++++++++++++++++----- .../pterodactylus/jsite/project/ProjectFile.java | 15 +- 2 files changed, 127 insertions(+), 46 deletions(-) diff --git a/src/net/pterodactylus/jsite/project/Project.java b/src/net/pterodactylus/jsite/project/Project.java index dda7a5c..f6314cb 100644 --- a/src/net/pterodactylus/jsite/project/Project.java +++ b/src/net/pterodactylus/jsite/project/Project.java @@ -20,7 +20,9 @@ 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; @@ -30,7 +32,7 @@ import net.pterodactylus.util.beans.AbstractBean; /** * 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 { @@ -86,7 +88,7 @@ public class Project extends AbstractBean { /** * Clones the given project. - * + * * @param project */ Project(Project project) { @@ -99,7 +101,7 @@ public class Project extends AbstractBean { /** * Returns the internal ID. - * + * * @return The internal ID */ String getId() { @@ -108,7 +110,7 @@ public class Project extends AbstractBean { /** * Sets the internal ID. - * + * * @param id * The internal ID */ @@ -118,7 +120,7 @@ public class Project extends AbstractBean { /** * Returns the name of the project. - * + * * @return The name of the project */ public String getName() { @@ -127,7 +129,7 @@ public class Project extends AbstractBean { /** * Sets the name of the project. - * + * * @param name * The name of the project */ @@ -139,7 +141,7 @@ public class Project extends AbstractBean { /** * Returns the description of the project. - * + * * @return The description of the project */ public String getDescription() { @@ -148,7 +150,7 @@ public class Project extends AbstractBean { /** * Sets the description of the project - * + * * @param description * The description of the project */ @@ -160,7 +162,7 @@ public class Project extends AbstractBean { /** * Returns the public key of the project. - * + * * @return The public key of the project */ public String getPublicKey() { @@ -169,7 +171,7 @@ public class Project extends AbstractBean { /** * Sets the public key of the project. - * + * * @param publicKey * The public key of the project */ @@ -181,7 +183,7 @@ public class Project extends AbstractBean { /** * Returns the private key of the project. - * + * * @return The private key of the project */ public String getPrivateKey() { @@ -190,7 +192,7 @@ public class Project extends AbstractBean { /** * Sets the private key of the project. - * + * * @param privateKey * The private key of the project */ @@ -202,7 +204,7 @@ public class Project extends AbstractBean { /** * Returns the base path of the project. - * + * * @return The base path of the project */ public String getBasePath() { @@ -211,7 +213,7 @@ public class Project extends AbstractBean { /** * Sets the base path of the project. - * + * * @param basePath * The base path of the project */ @@ -223,7 +225,7 @@ public class Project extends AbstractBean { /** * Returns the default file. - * + * * @return The default file */ public String getDefaultFile() { @@ -232,7 +234,7 @@ public class Project extends AbstractBean { /** * Sets the default file. - * + * * @param defaultFile * The default file */ @@ -244,7 +246,7 @@ public class Project extends AbstractBean { /** * Adds an override for the given file. - * + * * @param filePath * The file path * @param override @@ -256,7 +258,7 @@ public class Project extends AbstractBean { /** * Removes the override for the given file. - * + * * @param filePath * The file path for which to remove the override */ @@ -266,7 +268,7 @@ public class Project extends AbstractBean { /** * Returns the list of {@link Override}s. - * + * * @return All overrides */ public Map getOverrides() { @@ -276,33 +278,82 @@ public class Project extends AbstractBean { /** * 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 */ public ProjectFile getBaseFile() { + ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", true, false); + scanDirectory(new File(basePath), rootProjectFile); + return rootProjectFile; + } + // + // 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.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 { + /** The parent of this project file. */ private final ProjectFileImpl parentProjectFile; + + /** The name of this project file. */ private final String name; + + /** Whether this project file is a directory. */ private final boolean directory; - private final boolean hidden; - private List childProjectFiles = new ArrayList(); + /** Whether this file is hidden. */ + private final boolean hidden; + /** This project file’s children. */ + private List childProjectFiles = new ArrayList(); - public ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) { + /** + * Creates a new project fie. + * + * @param parentProjectFile + * The parent of the project file, or null if + * the new project file does not have a parent + * @param name + * The name of the project file + * @param isDirectory + * true if this project file is a directory, + * false otherwise + * @param isHidden + * true if this project file is hidden, + * false otherwise + */ + ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) { this.parentProjectFile = parentProjectFile; this.name = name; this.directory = isDirectory; @@ -336,42 +387,71 @@ public class Project extends AbstractBean { * @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; } /** * @see net.pterodactylus.jsite.project.ProjectFile#getFiles() */ public List getFiles() { - // TODO Auto-generated method stub - return null; + List projectFiles = new ArrayList(childProjectFiles); + return projectFiles; } // - // PRIVATE METHODS + // ACTIONS // - private ProjectFileImpl getParent() { - return parentProjectFile; + /** + * Adds a new project file as child to this project file. + * + * @param name + * The name of the file + * @param isDirectory + * true if the new file is a directory, + * false otherwise + * @param isHidden + * true if the new file is hidden, + * false otherwise + * @return The created project file + */ + public ProjectFileImpl addFile(String name, boolean isDirectory, boolean isHidden) { + ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, 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); + } } diff --git a/src/net/pterodactylus/jsite/project/ProjectFile.java b/src/net/pterodactylus/jsite/project/ProjectFile.java index 3e170fd..984dcbe 100644 --- a/src/net/pterodactylus/jsite/project/ProjectFile.java +++ b/src/net/pterodactylus/jsite/project/ProjectFile.java @@ -16,6 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + package net.pterodactylus.jsite.project; import java.util.List; @@ -26,14 +27,14 @@ import net.pterodactylus.jsite.core.Core; * Abstraction for a that exists on the machine {@link Core} is being run on. * This abstraction layer exists to make it possible to run jSite as a daemon * and only connect to it via network. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public interface ProjectFile { /** * Returns the name of the file. - * + * * @return The name of the file */ public String getName(); @@ -41,14 +42,14 @@ public interface ProjectFile { /** * Returns all parent files of this file. This file is the last file in the * returned list. - * + * * @return A list of all parents of this file and this file itself */ public List getParents(); /** * Returns whether this file is a directory. - * + * * @return true if this file is a directory, * false otherwise */ @@ -56,7 +57,7 @@ public interface ProjectFile { /** * Returns whether this file is a file (as opposed to being a directory). - * + * * @return true if this file is a file, false * otherwise */ @@ -64,14 +65,14 @@ public interface ProjectFile { /** * Returns whether this file is hidden. - * + * * @return true if this file is hidden */ public boolean isHidden(); /** * If this file is a directory, returns all files in this directory. - * + * * @see #isDirectory() * @return All files in this directory if this file is a directory, or * null if this file is not a directory -- 2.7.4