fix npe at startup
[jSite.git] / src / de / todesbaum / jsite / main / Configuration.java
index 3931dcb..f8acc53 100644 (file)
@@ -34,7 +34,6 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 
-import de.todesbaum.jsite.application.EditionProject;
 import de.todesbaum.jsite.application.FileOption;
 import de.todesbaum.jsite.application.Node;
 import de.todesbaum.jsite.application.Project;
@@ -44,7 +43,7 @@ import de.todesbaum.util.xml.XML;
 
 /**
  * @author David Roden <droden@gmail.com>
- * @version $Id: Configuration.java 418 2006-03-29 17:49:16Z bombe $
+ * @version $Id$
  */
 public class Configuration {
 
@@ -53,13 +52,21 @@ public class Configuration {
        private SimpleXML rootNode;
 
        public Configuration() {
-               filename = System.getProperty("user.home") + "/.jSite/config7";
-               lockFilename = System.getProperty("user.home") + "/.jSite/lock7";
+               this(System.getProperty("user.home") + "/.jSite/config7");
+       }
+       
+       public Configuration(String filename) {
+               this(filename, filename + ".lock");
+       }
+       
+       public Configuration(String filename, String lockFilename) {
+               this.filename = filename;
+               this.lockFilename = lockFilename;
                readConfiguration();
        }
        
        private boolean createConfigDirectory() {
-               File configDirectory = new File(System.getProperty("user.home"), ".jSite");
+               File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
                return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
        }
 
@@ -112,7 +119,7 @@ public class Configuration {
        public boolean save() {
                File configurationFile = new File(filename);
                if (!configurationFile.exists()) {
-                       File configurationFilePath = configurationFile.getParentFile();
+                       File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
                        if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
                                return false;
                        }
@@ -171,18 +178,42 @@ public class Configuration {
                return Boolean.parseBoolean(nodeValue);
        }
 
+       /**
+        * Returns the hostname of the node.
+        * @return The hostname of the node
+        * @deprecated Use {@link #getSelectedNode()} instead
+        */
+       @Deprecated
        public String getNodeAddress() {
                return getNodeValue(new String[] { "node-address" }, "localhost");
        }
 
+       /**
+        * Sets the hostname of the node.
+        * @param nodeAddress The hostname of the node
+        * @deprecated Use {@link #setSelectedNode(Node)} instead
+        */
+       @Deprecated
        public void setNodeAddress(String nodeAddress) {
                rootNode.replace("node-address", nodeAddress);
        }
 
+       /**
+        * The port number of the node
+        * @return The port number of the node
+        * @deprecated Use {@link #getSelectedNode()} instead. 
+        */
+       @Deprecated
        public int getNodePort() {
                return getNodeIntValue(new String[] { "node-port" }, 9481);
        }
 
+       /**
+        * Sets the port number of the node.
+        * @param nodePort The port number of the node
+        * @deprecated Use {@link #setSelectedNode(Node)} instead
+        */
+       @Deprecated
        public void setNodePort(int nodePort) {
                rootNode.replace("node-port", String.valueOf(nodePort));
        }
@@ -202,13 +233,7 @@ public class Configuration {
                        SimpleXML[] projectNodes = projectsNode.getNodes("project");
                        for (SimpleXML projectNode: projectNodes) {
                                try {
-                                       Project project = null;
-                                       SimpleXML typeNode = projectNode.getNode("type");
-                                       if ("edition".equals(typeNode.getValue())) {
-                                               EditionProject editionProject = new EditionProject();
-                                               project = editionProject;
-                                               editionProject.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
-                                       }
+                                       Project project = new Project();
                                        projects.add(project);
                                        project.setDescription(projectNode.getNode("description").getValue());
                                        project.setIndexFile(projectNode.getNode("index-file").getValue());
@@ -216,6 +241,10 @@ public class Configuration {
                                        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());
                                        SimpleXML fileOptionsNode = projectNode.getNode("file-options");
@@ -249,10 +278,7 @@ public class Configuration {
                SimpleXML projectsNode = new SimpleXML("project-list");
                for (Project project: projects) {
                        SimpleXML projectNode = projectsNode.append("project");
-                       if (project instanceof EditionProject) {
-                               projectNode.append("type", "edition");
-                               projectNode.append("edition", String.valueOf(((EditionProject) project).getEdition()));
-                       }
+                       projectNode.append("edition", String.valueOf(project.getEdition()));
                        projectNode.append("description", project.getDescription());
                        projectNode.append("index-file", project.getIndexFile());
                        projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
@@ -305,6 +331,10 @@ public class Configuration {
                if (nodesNode == null) {
                        String hostname = getNodeAddress();
                        int port = getNodePort();
+                       if (hostname == null) {
+                               hostname = "127.0.0.1";
+                               port = 9481;
+                       }
                        return new Node[] { new Node(hostname, port, "Node") };
                }
                SimpleXML[] nodeNodes = nodesNode.getNodes("node");
@@ -344,7 +374,13 @@ public class Configuration {
        public Node getSelectedNode() {
                SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
                if (selectedNodeNode == null) {
-                       return null;
+                       String hostname = getNodeAddress();
+                       int port = getNodePort();
+                       if (hostname == null) {
+                               hostname = "127.0.0.1";
+                               port = 9481;
+                       }
+                       return new Node(hostname, port, "Node");
                }
                String name = selectedNodeNode.getNode("name").getValue();
                String hostname = selectedNodeNode.getNode("hostname").getValue();