add more override methods that handle project files
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index 7a6ee10..65f28a1 100644 (file)
@@ -77,7 +77,10 @@ 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.
@@ -245,34 +248,80 @@ public class Project extends AbstractBean {
        }
 
        /**
-        * 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 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 override for the given file.
+        * 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 list of {@link Override}s.
+        * Returns the file override for the given file.
         * 
-        * @return All overrides
+        * @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 Map<String, Override> getOverrides() {
-               return overrides;
+       public FileOverride getFileOverride(String filePath) {
+               return fileOverrides.get(filePath);
+       }
+
+       /**
+        * Returns the list of {@link FileOverride}s.
+        * 
+        * @return All file overrides
+        */
+       public Map<String, FileOverride> getFileOverrides() {
+               return fileOverrides;
        }
 
        /**
@@ -289,11 +338,37 @@ public class Project extends AbstractBean {
                if (!basePathFile.exists() || !basePathFile.isDirectory()) {
                        return null;
                }
-               ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", 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 <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
        //
@@ -313,7 +388,7 @@ public class Project extends AbstractBean {
                        return;
                }
                for (File file: directory.listFiles()) {
-                       ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.isDirectory(), file.isHidden());
+                       ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
                        if (file.isDirectory()) {
                                scanDirectory(file, projectFileChild);
                        }
@@ -334,6 +409,9 @@ public class Project extends AbstractBean {
                /** 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;
 
@@ -351,6 +429,8 @@ public class Project extends AbstractBean {
                 *            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
@@ -358,9 +438,10 @@ public class Project extends AbstractBean {
                 *            <code>true</code> if this project file is hidden,
                 *            <code>false</code> otherwise
                 */
-               ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) {
+               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;
                }
@@ -377,6 +458,20 @@ public class Project extends AbstractBean {
                }
 
                /**
+                * @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() {
@@ -395,10 +490,11 @@ public class Project extends AbstractBean {
                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);
+                       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) : "";
                }
 
                /**
@@ -423,6 +519,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 <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() {
@@ -439,6 +556,8 @@ public class Project extends AbstractBean {
                 * 
                 * @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
@@ -447,8 +566,8 @@ public class Project extends AbstractBean {
                 *            <code>false</code> otherwise
                 * @return The created project file
                 */
-               public ProjectFileImpl addFile(String name, boolean isDirectory, boolean isHidden) {
-                       ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, isDirectory, isHidden);
+               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;
                }