Add accessors for the configuration file location.
[jSite.git] / src / de / todesbaum / jsite / main / Configuration.java
index 49f107b..ca5a1af 100644 (file)
@@ -44,11 +44,32 @@ import de.todesbaum.util.xml.XML;
 
 /**
  * The configuration.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class Configuration {
 
+       /**
+        * The location of the configuration directory.
+        *
+        * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+        */
+       public enum ConfigurationDirectory {
+
+               /** The configuration is in the same directory as the JAR file. */
+               NEXT_TO_JAR_FILE,
+
+               /**
+                * The configuration is in the user’s home directory. This is the
+                * pre-0.9.3 default.
+                */
+               HOME_DIRECTORY;
+
+       }
+
+       /** The configuration directory. */
+       private ConfigurationDirectory configurationDirectory = ConfigurationDirectory.HOME_DIRECTORY;
+
        /** The name of the file the configuration is stored to. */
        private String filename;
 
@@ -68,7 +89,7 @@ public class Configuration {
 
        /**
         * Creates a new configuration that is read from the given file.
-        * 
+        *
         * @param filename
         *            The name of the configuration file
         */
@@ -79,7 +100,7 @@ public class Configuration {
        /**
         * Creates a new configuration that is read from the given file and uses the
         * given lock file.
-        * 
+        *
         * @param filename
         *            The name of the configuration file
         * @param lockFilename
@@ -92,8 +113,27 @@ public class Configuration {
        }
 
        /**
+        * Returns the configuration directory.
+        *
+        * @return The configuration directory
+        */
+       public ConfigurationDirectory getConfigurationDirectory() {
+               return configurationDirectory;
+       }
+
+       /**
+        * Sets the configuration directory.
+        *
+        * @param configurationDirectory
+        *            The configuration directory
+        */
+       public void setConfigurationDirectory(ConfigurationDirectory configurationDirectory) {
+               this.configurationDirectory = configurationDirectory;
+       }
+
+       /**
         * Creates the directory of the configuration file.
-        * 
+        *
         * @return <code>true</code> if the directory exists, or if it could be
         *         created, <code>false</code> otherwise
         */
@@ -104,7 +144,7 @@ public class Configuration {
 
        /**
         * Creates the lock file.
-        * 
+        *
         * @return <code>true</code> if the lock file did not already exist and
         *         could be created, <code>false</code> otherwise
         */
@@ -113,9 +153,12 @@ public class Configuration {
                        return false;
                }
                File lockFile = new File(lockFilename);
-               lockFile.deleteOnExit();
                try {
-                       return lockFile.createNewFile();
+                       boolean fileLocked = lockFile.createNewFile();
+                       if (fileLocked) {
+                               lockFile.deleteOnExit();
+                       }
+                       return fileLocked;
                } catch (IOException e) {
                        /* ignore. */
                }
@@ -123,6 +166,13 @@ public class Configuration {
        }
 
        /**
+        * Tells the VM to remove the lock file on program exit.
+        */
+       public void removeLockfileOnExit() {
+               new File(lockFilename).deleteOnExit();
+       }
+
+       /**
         * Reads the configuration from the file.
         */
        private void readConfiguration() {
@@ -152,7 +202,7 @@ public class Configuration {
 
        /**
         * Saves the configuration.
-        * 
+        *
         * @return <code>true</code> if the configuration could be saved,
         *         <code>false</code> otherwise
         */
@@ -183,7 +233,7 @@ public class Configuration {
 
        /**
         * Returns the value of a node.
-        * 
+        *
         * @param nodeNames
         *            The name of all nodes in the chain
         * @param defaultValue
@@ -205,7 +255,7 @@ public class Configuration {
 
        /**
         * Returns the integer value of a node.
-        * 
+        *
         * @param nodeNames
         *            The names of all nodes in the chain
         * @param defaultValue
@@ -224,7 +274,7 @@ public class Configuration {
 
        /**
         * Returns the boolean value of a node.
-        * 
+        *
         * @param nodeNames
         *            The names of all nodes in the chain
         * @param defaultValue
@@ -242,7 +292,7 @@ public class Configuration {
 
        /**
         * Returns the hostname of the node.
-        * 
+        *
         * @return The hostname of the node
         * @deprecated Use {@link #getSelectedNode()} instead
         */
@@ -253,7 +303,7 @@ public class Configuration {
 
        /**
         * Sets the hostname of the node.
-        * 
+        *
         * @param nodeAddress
         *            The hostname of the node
         * @deprecated Use {@link #setSelectedNode(Node)} instead
@@ -265,7 +315,7 @@ public class Configuration {
 
        /**
         * The port number of the node
-        * 
+        *
         * @return The port number of the node
         * @deprecated Use {@link #getSelectedNode()} instead.
         */
@@ -276,7 +326,7 @@ public class Configuration {
 
        /**
         * Sets the port number of the node.
-        * 
+        *
         * @param nodePort
         *            The port number of the node
         * @deprecated Use {@link #setSelectedNode(Node)} instead
@@ -288,9 +338,9 @@ public class Configuration {
 
        /**
         * Returns whether the node configuration page should be skipped on startup.
-        * 
-        * @return <code>true</code> to skip the node configuration page on
-        *         startup, <code>false</code> to show it
+        *
+        * @return <code>true</code> to skip the node configuration page on startup,
+        *         <code>false</code> to show it
         */
        public boolean isSkipNodePage() {
                return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
@@ -298,7 +348,7 @@ public class Configuration {
 
        /**
         * Sets whether the node configuration page should be skipped on startup.
-        * 
+        *
         * @param skipNodePage
         *            <code>true</code> to skip the node configuration page on
         *            startup, <code>false</code> to show it
@@ -309,7 +359,7 @@ public class Configuration {
 
        /**
         * Returns all configured projects.
-        * 
+        *
         * @return A list of all projects
         */
        public Project[] getProjects() {
@@ -321,18 +371,27 @@ public class Configuration {
                                try {
                                        Project project = new Project();
                                        projects.add(project);
-                                       project.setDescription(projectNode.getNode("description").getValue());
-                                       project.setIndexFile(projectNode.getNode("index-file").getValue());
-                                       project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
-                                       project.setLocalPath(projectNode.getNode("local-path").getValue());
-                                       project.setName(projectNode.getNode("name").getValue());
-                                       project.setPath(projectNode.getNode("path").getValue());
+                                       project.setDescription(projectNode.getNode("description").getValue(""));
+                                       String indexFile = projectNode.getNode("index-file").getValue("");
+                                       if (indexFile.indexOf('/') > -1) {
+                                               indexFile = "";
+                                       }
+                                       project.setIndexFile(indexFile);
+                                       project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
+                                       project.setLocalPath(projectNode.getNode("local-path").getValue(""));
+                                       project.setName(projectNode.getNode("name").getValue(""));
+                                       project.setPath(projectNode.getNode("path").getValue(""));
                                        if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
                                                project.setPath(project.getPath().replaceAll("/", ""));
                                        }
-                                       project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
-                                       project.setInsertURI(projectNode.getNode("insert-uri").getValue());
-                                       project.setRequestURI(projectNode.getNode("request-uri").getValue());
+                                       project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
+                                       project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
+                                       project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
+                                       if (projectNode.getNode("ignore-hidden-files") != null) {
+                                               project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
+                                       } else {
+                                               project.setIgnoreHiddenFiles(true);
+                                       }
                                        SimpleXML fileOptionsNode = projectNode.getNode("file-options");
                                        Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
                                        if (fileOptionsNode != null) {
@@ -341,8 +400,14 @@ public class Configuration {
                                                        String filename = fileOptionNode.getNode("filename").getValue();
                                                        FileOption fileOption = project.getFileOption(filename);
                                                        fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
-                                                       fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
-                                                       fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
+                                                       if (fileOptionNode.getNode("insert-redirect") != null) {
+                                                               fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
+                                                       }
+                                                       fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
+                                                       if (fileOptionNode.getNode("changed-name") != null) {
+                                                               fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
+                                                       }
+                                                       fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
                                                        fileOption.setContainer(fileOptionNode.getNode("container").getValue());
                                                        if (fileOptionNode.getNode("replace-edition") != null) {
                                                                fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
@@ -362,7 +427,7 @@ public class Configuration {
 
        /**
         * Sets the list of all projects.
-        * 
+        *
         * @param projects
         *            The list of all projects
         */
@@ -379,6 +444,7 @@ public class Configuration {
                        projectNode.append("path", project.getPath());
                        projectNode.append("insert-uri", project.getInsertURI());
                        projectNode.append("request-uri", project.getRequestURI());
+                       projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
                        SimpleXML fileOptionsNode = projectNode.append("file-options");
                        Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
                        while (entries.hasNext()) {
@@ -388,7 +454,9 @@ public class Configuration {
                                        SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
                                        fileOptionNode.append("filename", entry.getKey());
                                        fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
+                                       fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
                                        fileOptionNode.append("custom-key", fileOption.getCustomKey());
+                                       fileOptionNode.append("changed-name", fileOption.getChangedName());
                                        fileOptionNode.append("mime-type", fileOption.getMimeType());
                                        fileOptionNode.append("container", fileOption.getContainer());
                                        fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
@@ -401,7 +469,7 @@ public class Configuration {
 
        /**
         * Returns the stored locale.
-        * 
+        *
         * @return The stored locale
         */
        public Locale getLocale() {
@@ -415,7 +483,7 @@ public class Configuration {
 
        /**
         * Sets the locale to store.
-        * 
+        *
         * @param locale
         *            The locale to store
         */
@@ -431,7 +499,7 @@ public class Configuration {
 
        /**
         * Returns a list of configured nodes.
-        * 
+        *
         * @return The list of the configured nodes
         */
        public Node[] getNodes() {
@@ -460,7 +528,7 @@ public class Configuration {
 
        /**
         * Sets the list of configured nodes.
-        * 
+        *
         * @param nodes
         *            The list of configured nodes
         */
@@ -479,7 +547,7 @@ public class Configuration {
 
        /**
         * Sets the selected node.
-        * 
+        *
         * @param selectedNode
         *            The selected node
         */
@@ -493,7 +561,7 @@ public class Configuration {
 
        /**
         * Returns the selected node.
-        * 
+        *
         * @return The selected node
         */
        public Node getSelectedNode() {
@@ -513,4 +581,31 @@ public class Configuration {
                return new Node(hostname, port, name);
        }
 
+       /**
+        * Returns the temp directory to use.
+        *
+        * @return The temp directoy, or {@code null} to use the default temp
+        *         directory
+        */
+       public String getTempDirectory() {
+               return getNodeValue(new String[] { "temp-directory" }, null);
+       }
+
+       /**
+        * Sets the temp directory to use.
+        *
+        * @param tempDirectory
+        *            The temp directory to use, or {@code null} to use the default
+        *            temp directory
+        */
+       public void setTempDirectory(String tempDirectory) {
+               if (tempDirectory != null) {
+                       SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
+                       tempDirectoryNode.setValue(tempDirectory);
+                       rootNode.replace(tempDirectoryNode);
+               } else {
+                       rootNode.remove("temp-directory");
+               }
+       }
+
 }