X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fproject%2FProject.java;h=265eeff2ae1ce3fcf20fe4dda2ba9dae4d648430;hb=d14c6006312ccb2e0b0376ee9b2ce9665ed15132;hp=c54f211de9de171636fc569c88889c1a14261561;hpb=1b87930e43eb7d14068a21ed0b488f28cc6ce49b;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/project/Project.java b/src/net/pterodactylus/jsite/project/Project.java index c54f211..265eeff 100644 --- a/src/net/pterodactylus/jsite/project/Project.java +++ b/src/net/pterodactylus/jsite/project/Project.java @@ -22,7 +22,10 @@ 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; import net.pterodactylus.util.beans.AbstractBean; @@ -49,8 +52,8 @@ public class Project extends AbstractBean { /** Name of the “base path” property. */ public static final String PROPERTY_BASE_PATH = "basePath"; - /** Name of the “base path entries” property. */ - public static final String PROPERTY_BASE_PATH_ENTRIES = "basePathEntries"; + /** Name of the “default file” property. */ + public static final String PROPERTY_DEFAULT_FILE = "defaultFile"; /** Internal ID. */ private String id; @@ -70,11 +73,11 @@ public class Project extends AbstractBean { /** The base path of the project. */ private String basePath; - /** The list of files from the base path. */ - private List basePathEntries = new ArrayList(); + /** The default file. */ + private String defaultFile; - /** The list of virtual files. */ - private List virtualEntries = new ArrayList(); + /** The overrides. */ + private final Map overrides = new HashMap(); /** * Creates a new project. @@ -94,8 +97,6 @@ public class Project extends AbstractBean { this.publicKey = project.publicKey; this.privateKey = project.privateKey; this.basePath = project.basePath; - this.basePathEntries.addAll(project.basePathEntries); - this.virtualEntries.addAll(project.virtualEntries); } /** @@ -223,51 +224,74 @@ public class Project extends AbstractBean { } /** - * Rescans the base path for new or changed files. + * Returns the default file. + * + * @return The default file */ - public void rescanBasePath() { - List entries = new ArrayList(); - scanPath("", entries); - basePathEntries.clear(); - basePathEntries.addAll(entries); - firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null); + public String getDefaultFile() { + return defaultFile; } /** - * Returns the list of files from the base path. + * Sets the default file. * - * @return The list of files from the base path + * @param defaultFile + * The default file */ - public List getBasePathEntries() { - return basePathEntries; + public void setDefaultFile(String defaultFile) { + String oldDefaultFile = this.defaultFile; + this.defaultFile = defaultFile; + fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile); } /** - * Returns the list of visual entries. + * Adds an override for the given file. * - * @return The visual entries + * @param filePath + * The file path + * @param override + * The override for the file */ - public List getVirtualEntries() { - return virtualEntries; + public void addOverride(String filePath, Override override) { + overrides.put(filePath, override); } /** - * Adds a virtual entry that redirects to the given target. + * Removes the override for the given file. * - * @param name - * The name of the entry - * @param contentType - * The content type of the entry, or null for - * auto-detection - * @param target - * The target URI of the redirect + * @param filePath + * The file path for which to remove the override + */ + public void removeOverride(String filePath) { + overrides.remove(filePath); + } + + /** + * Returns the list of {@link Override}s. + * + * @return All overrides + */ + public Map getOverrides() { + return overrides; + } + + /** + * 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 + * directories! + * + * @return The file for the base path, or null if the base + * path does not denote an existing directory */ - public void addVirtualEntry(String name, String contentType, String target) { - RedirectEntry redirectEntry = new RedirectEntry(); - redirectEntry.setName(name); - redirectEntry.setContentType(contentType); - redirectEntry.setTarget(target); - redirectEntry.setInsert(true); + public ProjectFile getBaseFile() { + File basePathFile = new File(basePath); + if (!basePathFile.exists() || !basePathFile.isDirectory()) { + return null; + } + ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", 0, true, false); + scanDirectory(basePathFile, rootProjectFile); + return rootProjectFile; } // @@ -275,31 +299,193 @@ public class Project extends AbstractBean { // /** - * Scans the given path relative to {@link #basePath} for files and adds - * them to the given list of entries. + * Scans the given directory and recreates the file and directory structure + * in the given project file. * - * @param currentPath - * The current path, relative to the base path - * @param entries - * The list of entries + * @param directory + * The directory to scan + * @param projectFile + * The project file in which to recreate the directory and file + * structure */ - private void scanPath(String currentPath, List entries) { - File currentDirectory = new File(basePath + File.separatorChar + currentPath); - if (!currentDirectory.isDirectory()) { + private void scanDirectory(File directory, ProjectFileImpl projectFile) { + if (!directory.isDirectory()) { return; } - for (File file: currentDirectory.listFiles()) { - String fileName = currentPath + file.getName(); + for (File file: directory.listFiles()) { + ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden()); if (file.isDirectory()) { - scanPath(fileName + File.separatorChar, entries); - continue; + scanDirectory(file, projectFileChild); } - PhysicalEntry entry = new PhysicalEntry(); - entry.setName(fileName); - entry.setPath(file.getPath()); - entry.setInsert(!file.isHidden()); - entries.add(entry); } + projectFile.sort(); + } + + /** + * Implementation of a {@link ProjectFile}. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + 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; + + /** 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 childProjectFiles = new ArrayList(); + + /** + * 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 size + * The size of the 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, 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() { + return name; + } + + /** + * {@inheritDoc} + */ + public long getSize() { + return size; + } + + /** + * @see net.pterodactylus.jsite.project.ProjectFile#getParents() + */ + public List getParents() { + List parentProjectFiles = new ArrayList(); + 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; + do { + completePath.insert(0, File.separatorChar).insert(0, this.getName()); + } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null); + return completePath.substring(1); + } + + /** + * @see net.pterodactylus.jsite.project.ProjectFile#isFile() + */ + public boolean isFile() { + return !directory; + } + + /** + * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory() + */ + public boolean isDirectory() { + return directory; + } + + /** + * @see net.pterodactylus.jsite.project.ProjectFile#isHidden() + */ + public boolean isHidden() { + return hidden; + } + + /** + * @see net.pterodactylus.jsite.project.ProjectFile#getFiles() + */ + public List getFiles() { + List projectFiles = new ArrayList(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 + * 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, 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); + } + } }