From: David ‘Bombe’ Roden Date: Fri, 30 May 2008 17:24:55 +0000 (+0200) Subject: add getFile() to retrieve a project file by its path X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5c8a0dffaf3a206110486d9821d18f10305b307f;p=jSite2.git add getFile() to retrieve a project file by its path --- diff --git a/src/net/pterodactylus/jsite/project/Project.java b/src/net/pterodactylus/jsite/project/Project.java index 0e96b9c..9545b4d 100644 --- a/src/net/pterodactylus/jsite/project/Project.java +++ b/src/net/pterodactylus/jsite/project/Project.java @@ -32,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 { @@ -79,6 +79,9 @@ public class Project extends AbstractBean { /** The overrides. */ private final Map fileOverrides = new HashMap(); + /** The current root project file. */ + private ProjectFileImpl rootProjectFile; + /** * Creates a new project. */ @@ -88,7 +91,7 @@ public class Project extends AbstractBean { /** * Clones the given project. - * + * * @param project */ Project(Project project) { @@ -101,7 +104,7 @@ public class Project extends AbstractBean { /** * Returns the internal ID. - * + * * @return The internal ID */ String getId() { @@ -110,7 +113,7 @@ public class Project extends AbstractBean { /** * Sets the internal ID. - * + * * @param id * The internal ID */ @@ -120,7 +123,7 @@ public class Project extends AbstractBean { /** * Returns the name of the project. - * + * * @return The name of the project */ public String getName() { @@ -129,7 +132,7 @@ public class Project extends AbstractBean { /** * Sets the name of the project. - * + * * @param name * The name of the project */ @@ -141,7 +144,7 @@ public class Project extends AbstractBean { /** * Returns the description of the project. - * + * * @return The description of the project */ public String getDescription() { @@ -150,7 +153,7 @@ public class Project extends AbstractBean { /** * Sets the description of the project - * + * * @param description * The description of the project */ @@ -162,7 +165,7 @@ public class Project extends AbstractBean { /** * Returns the public key of the project. - * + * * @return The public key of the project */ public String getPublicKey() { @@ -171,7 +174,7 @@ public class Project extends AbstractBean { /** * Sets the public key of the project. - * + * * @param publicKey * The public key of the project */ @@ -183,7 +186,7 @@ public class Project extends AbstractBean { /** * Returns the private key of the project. - * + * * @return The private key of the project */ public String getPrivateKey() { @@ -192,7 +195,7 @@ public class Project extends AbstractBean { /** * Sets the private key of the project. - * + * * @param privateKey * The private key of the project */ @@ -204,7 +207,7 @@ public class Project extends AbstractBean { /** * Returns the base path of the project. - * + * * @return The base path of the project */ public String getBasePath() { @@ -213,7 +216,7 @@ public class Project extends AbstractBean { /** * Sets the base path of the project. - * + * * @param basePath * The base path of the project */ @@ -225,7 +228,7 @@ public class Project extends AbstractBean { /** * Returns the default file. - * + * * @return The default file */ public String getDefaultFile() { @@ -234,7 +237,7 @@ public class Project extends AbstractBean { /** * Sets the default file. - * + * * @param defaultFile * The default file */ @@ -246,7 +249,7 @@ public class Project extends AbstractBean { /** * Adds a file override for the given file. - * + * * @param filePath * The file path * @param override @@ -258,7 +261,7 @@ public class Project extends AbstractBean { /** * Removes the file override for the given file. - * + * * @param filePath * The file path for which to remove the override */ @@ -268,7 +271,7 @@ public class Project extends AbstractBean { /** * Returns the list of {@link FileOverride}s. - * + * * @return All file overrides */ public Map getFileOverrides() { @@ -280,7 +283,7 @@ public class Project extends AbstractBean { * 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 * directories! - * + * * @return The file for the base path, or null if the base * path does not denote an existing directory */ @@ -289,11 +292,37 @@ public class Project extends AbstractBean { if (!basePathFile.exists() || !basePathFile.isDirectory()) { return null; } - ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", 0, true, false); + 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 null 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 // @@ -301,7 +330,7 @@ public class Project extends AbstractBean { /** * Scans the given directory and recreates the file and directory structure * in the given project file. - * + * * @param directory * The directory to scan * @param projectFile @@ -312,7 +341,7 @@ public class Project extends AbstractBean { if (!directory.isDirectory()) { return; } - for (File file : directory.listFiles()) { + for (File file: directory.listFiles()) { ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden()); if (file.isDirectory()) { scanDirectory(file, projectFileChild); @@ -323,7 +352,7 @@ public class Project extends AbstractBean { /** * Implementation of a {@link ProjectFile}. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ private static class ProjectFileImpl implements ProjectFile, Comparable { @@ -348,7 +377,7 @@ public class Project extends AbstractBean { /** * 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 @@ -444,6 +473,27 @@ public class Project extends AbstractBean { } /** + * 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 null 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 getFiles() { @@ -457,7 +507,7 @@ public class Project extends AbstractBean { /** * Adds a new project file as child to this project file. - * + * * @param name * The name of the file * @param size