reformat
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index 265eeff..22e66d8 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 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.
+        * Returns the list of {@link FileOverride}s.
         * 
-        * @return All overrides
+        * @return All file overrides
         */
-       public Map<String, Override> getOverrides() {
-               return 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, "", 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 <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
        //
@@ -312,7 +387,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);
@@ -383,6 +458,13 @@ public class Project extends AbstractBean {
                }
 
                /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#getParent()
+                */
+               public ProjectFile getParent() {
+                       return parentProjectFile;
+               }
+
+               /**
                 * {@inheritDoc}
                 */
                public long getSize() {
@@ -408,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) : "";
                }
 
                /**
@@ -436,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() {