remove subversion $Id$ tags
[jSite.git] / src / de / todesbaum / jsite / main / Configuration.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.main;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 import de.todesbaum.jsite.application.FileOption;
38 import de.todesbaum.jsite.application.Node;
39 import de.todesbaum.jsite.application.Project;
40 import de.todesbaum.util.io.StreamCopier;
41 import de.todesbaum.util.xml.SimpleXML;
42 import de.todesbaum.util.xml.XML;
43
44 /**
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  */
47 public class Configuration {
48
49         private String filename;
50         private String lockFilename;
51         private SimpleXML rootNode;
52
53         public Configuration() {
54                 this(System.getProperty("user.home") + "/.jSite/config7");
55         }
56         
57         public Configuration(String filename) {
58                 this(filename, filename + ".lock");
59         }
60         
61         public Configuration(String filename, String lockFilename) {
62                 this.filename = filename;
63                 this.lockFilename = lockFilename;
64                 readConfiguration();
65         }
66         
67         private boolean createConfigDirectory() {
68                 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
69                 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
70         }
71
72         public boolean createLockFile() {
73                 if (!createConfigDirectory()) {
74                         return false;
75                 }
76                 File lockFile = new File(lockFilename);
77                 lockFile.deleteOnExit();
78                 try {
79                         return lockFile.createNewFile();
80                 } catch (IOException e) {
81                 }
82                 return false;
83         }
84
85         private void readConfiguration() {
86                 File configurationFile = new File(filename);
87                 if (configurationFile.exists()) {
88                         ByteArrayOutputStream fileByteOutputStream = null;
89                         FileInputStream fileInputStream = null;
90                         try {
91                                 fileByteOutputStream = new ByteArrayOutputStream();
92                                 fileInputStream = new FileInputStream(configurationFile);
93                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
94                                 fileByteOutputStream.close();
95                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
96                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
97                                 return;
98                         } catch (FileNotFoundException e) {
99                         } catch (IOException e) {
100                         } finally {
101                                 if (fileInputStream != null) {
102                                         try {
103                                                 fileInputStream.close();
104                                         } catch (IOException ioe1) {
105                                         }
106                                 }
107                                 if (fileByteOutputStream != null) {
108                                         try {
109                                                 fileByteOutputStream.close();
110                                         } catch (IOException ioe1) {
111                                         }
112                                 }
113                         }
114                 }
115                 rootNode = new SimpleXML("configuration");
116         }
117
118         public boolean save() {
119                 File configurationFile = new File(filename);
120                 if (!configurationFile.exists()) {
121                         File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
122                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
123                                 return false;
124                         }
125                 }
126                 FileOutputStream fileOutputStream = null;
127                 ByteArrayInputStream configurationInputStream = null;
128                 try {
129                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
130                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
131                         fileOutputStream = new FileOutputStream(configurationFile);
132                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
133                         return true;
134                 } catch (IOException ioe1) {
135                 } finally {
136                         if (configurationInputStream != null) {
137                                 try {
138                                         configurationInputStream.close();
139                                 } catch (IOException ioe1) {
140                                 }
141                         }
142                         if (fileOutputStream != null) {
143                                 try {
144                                         fileOutputStream.close();
145                                 } catch (IOException ioe1) {
146                                 }
147                         }
148                 }
149                 return false;
150         }
151
152         private String getNodeValue(String[] nodeNames, String defaultValue) {
153                 SimpleXML node = rootNode;
154                 int nodeIndex = 0;
155                 while ((node != null) && (nodeIndex < nodeNames.length)) {
156                         node = node.getNode(nodeNames[nodeIndex++]);
157                 }
158                 if (node == null) {
159                         return defaultValue;
160                 }
161                 return node.getValue();
162         }
163
164         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
165                 try {
166                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
167                 } catch (NumberFormatException nfe1) {
168                 }
169                 return defaultValue;
170         }
171
172         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
173                 String nodeValue = getNodeValue(nodeNames, null);
174                 if (nodeValue == null) {
175                         return defaultValue;
176                 }
177                 return Boolean.parseBoolean(nodeValue);
178         }
179
180         /**
181          * Returns the hostname of the node.
182          * @return The hostname of the node
183          * @deprecated Use {@link #getSelectedNode()} instead
184          */
185         @Deprecated
186         public String getNodeAddress() {
187                 return getNodeValue(new String[] { "node-address" }, "localhost");
188         }
189
190         /**
191          * Sets the hostname of the node.
192          * @param nodeAddress The hostname of the node
193          * @deprecated Use {@link #setSelectedNode(Node)} instead
194          */
195         @Deprecated
196         public void setNodeAddress(String nodeAddress) {
197                 rootNode.replace("node-address", nodeAddress);
198         }
199
200         /**
201          * The port number of the node
202          * @return The port number of the node
203          * @deprecated Use {@link #getSelectedNode()} instead. 
204          */
205         @Deprecated
206         public int getNodePort() {
207                 return getNodeIntValue(new String[] { "node-port" }, 9481);
208         }
209
210         /**
211          * Sets the port number of the node.
212          * @param nodePort The port number of the node
213          * @deprecated Use {@link #setSelectedNode(Node)} instead
214          */
215         @Deprecated
216         public void setNodePort(int nodePort) {
217                 rootNode.replace("node-port", String.valueOf(nodePort));
218         }
219
220         public boolean isSkipNodePage() {
221                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
222         }
223
224         public void setSkipNodePage(boolean skipNodePage) {
225                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
226         }
227
228         public Project[] getProjects() {
229                 List<Project> projects = new ArrayList<Project>();
230                 SimpleXML projectsNode = rootNode.getNode("project-list");
231                 if (projectsNode != null) {
232                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
233                         for (SimpleXML projectNode: projectNodes) {
234                                 try {
235                                         Project project = new Project();
236                                         projects.add(project);
237                                         project.setDescription(projectNode.getNode("description").getValue());
238                                         project.setIndexFile(projectNode.getNode("index-file").getValue());
239                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
240                                         project.setLocalPath(projectNode.getNode("local-path").getValue());
241                                         project.setName(projectNode.getNode("name").getValue());
242                                         project.setPath(projectNode.getNode("path").getValue());
243                                         if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
244                                                 project.setPath(project.getPath().replaceAll("/", ""));
245                                         }
246                                         project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
247                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue());
248                                         project.setRequestURI(projectNode.getNode("request-uri").getValue());
249                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
250                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
251                                         if (fileOptionsNode != null) {
252                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
253                                                 for (SimpleXML fileOptionNode: fileOptionNodes) {
254                                                         String filename = fileOptionNode.getNode("filename").getValue();
255                                                         FileOption fileOption = project.getFileOption(filename);
256                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
257                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
258                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
259                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
260                                                         if (fileOptionNode.getNode("replace-edition") != null) {
261                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
262                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
263                                                         }
264                                                         fileOptions.put(filename, fileOption);
265                                                 }
266                                         }
267                                         project.setFileOptions(fileOptions);
268                                 } catch (NumberFormatException nfe1) {
269                                         nfe1.printStackTrace();
270                                 }
271                         }
272                 }
273                 return projects.toArray(new Project[projects.size()]);
274         }
275
276         public void setProjects(Project[] projects) {
277                 SimpleXML projectsNode = new SimpleXML("project-list");
278                 for (Project project: projects) {
279                         SimpleXML projectNode = projectsNode.append("project");
280                         projectNode.append("edition", String.valueOf(project.getEdition()));
281                         projectNode.append("description", project.getDescription());
282                         projectNode.append("index-file", project.getIndexFile());
283                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
284                         projectNode.append("local-path", project.getLocalPath());
285                         projectNode.append("name", project.getName());
286                         projectNode.append("path", project.getPath());
287                         projectNode.append("insert-uri", project.getInsertURI());
288                         projectNode.append("request-uri", project.getRequestURI());
289                         SimpleXML fileOptionsNode = projectNode.append("file-options");
290                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
291                         while (entries.hasNext()) {
292                                 Entry<String, FileOption> entry = entries.next();
293                                 FileOption fileOption = entry.getValue();
294                                 if (fileOption.isCustom()) {
295                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
296                                         fileOptionNode.append("filename", entry.getKey());
297                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
298                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
299                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
300                                         fileOptionNode.append("container", fileOption.getContainer());
301                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
302                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
303                                 }
304                         }
305                 }
306                 rootNode.replace(projectsNode);
307         }
308
309         public Locale getLocale() {
310                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
311                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
312                 if (country != null) {
313                         return new Locale(language, country);
314                 }
315                 return new Locale(language);
316         }
317
318         public void setLocale(Locale locale) {
319                 SimpleXML i18nNode = new SimpleXML("i18n");
320                 if (locale.getCountry().length() != 0) {
321                         i18nNode.append("country", locale.getCountry());
322                 }
323                 i18nNode.append("language", locale.getLanguage());
324                 rootNode.replace(i18nNode);
325                 return;
326         }
327         
328         public Node[] getNodes() {
329                 SimpleXML nodesNode = rootNode.getNode("nodes");
330                 if (nodesNode == null) {
331                         String hostname = getNodeAddress();
332                         int port = getNodePort();
333                         if (hostname == null) {
334                                 hostname = "127.0.0.1";
335                                 port = 9481;
336                         }
337                         return new Node[] { new Node(hostname, port, "Node") };
338                 }
339                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
340                 Node[] returnNodes = new Node[nodeNodes.length];
341                 int nodeIndex = 0;
342                 for (SimpleXML nodeNode: nodeNodes) {
343                         String name = nodeNode.getNode("name").getValue();
344                         String hostname = nodeNode.getNode("hostname").getValue();
345                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
346                         Node node = new Node(hostname, port, name);
347                         returnNodes[nodeIndex++] = node;
348                 }
349                 return returnNodes;
350         }
351         
352         public void setNodes(Node[] nodes) {
353                 SimpleXML nodesNode = new SimpleXML("nodes");
354                 for (Node node: nodes) {
355                         SimpleXML nodeNode = nodesNode.append("node");
356                         nodeNode.append("name", node.getName());
357                         nodeNode.append("hostname", node.getHostname());
358                         nodeNode.append("port", String.valueOf(node.getPort()));
359                 }
360                 rootNode.replace(nodesNode);
361                 rootNode.remove("node-address");
362                 rootNode.remove("node-port");
363         }
364
365         public void setSelectedNode(Node selectedNode) {
366                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
367                 selectedNodeNode.append("name", selectedNode.getName());
368                 selectedNodeNode.append("hostname", selectedNode.getHostname());
369                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
370                 rootNode.replace(selectedNodeNode);
371         }
372         
373         public Node getSelectedNode() {
374                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
375                 if (selectedNodeNode == null) {
376                         String hostname = getNodeAddress();
377                         int port = getNodePort();
378                         if (hostname == null) {
379                                 hostname = "127.0.0.1";
380                                 port = 9481;
381                         }
382                         return new Node(hostname, port, "Node");
383                 }
384                 String name = selectedNodeNode.getNode("name").getValue();
385                 String hostname = selectedNodeNode.getNode("hostname").getValue();
386                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
387                 return new Node(hostname, port, name);
388         }
389         
390 }