3931dcb75e542e0ccddddced7e22e4d91102127b
[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.EditionProject;
38 import de.todesbaum.jsite.application.FileOption;
39 import de.todesbaum.jsite.application.Node;
40 import de.todesbaum.jsite.application.Project;
41 import de.todesbaum.util.io.StreamCopier;
42 import de.todesbaum.util.xml.SimpleXML;
43 import de.todesbaum.util.xml.XML;
44
45 /**
46  * @author David Roden <droden@gmail.com>
47  * @version $Id: Configuration.java 418 2006-03-29 17:49:16Z bombe $
48  */
49 public class Configuration {
50
51         private String filename;
52         private String lockFilename;
53         private SimpleXML rootNode;
54
55         public Configuration() {
56                 filename = System.getProperty("user.home") + "/.jSite/config7";
57                 lockFilename = System.getProperty("user.home") + "/.jSite/lock7";
58                 readConfiguration();
59         }
60         
61         private boolean createConfigDirectory() {
62                 File configDirectory = new File(System.getProperty("user.home"), ".jSite");
63                 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
64         }
65
66         public boolean createLockFile() {
67                 if (!createConfigDirectory()) {
68                         return false;
69                 }
70                 File lockFile = new File(lockFilename);
71                 lockFile.deleteOnExit();
72                 try {
73                         return lockFile.createNewFile();
74                 } catch (IOException e) {
75                 }
76                 return false;
77         }
78
79         private void readConfiguration() {
80                 File configurationFile = new File(filename);
81                 if (configurationFile.exists()) {
82                         ByteArrayOutputStream fileByteOutputStream = null;
83                         FileInputStream fileInputStream = null;
84                         try {
85                                 fileByteOutputStream = new ByteArrayOutputStream();
86                                 fileInputStream = new FileInputStream(configurationFile);
87                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
88                                 fileByteOutputStream.close();
89                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
90                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
91                                 return;
92                         } catch (FileNotFoundException e) {
93                         } catch (IOException e) {
94                         } finally {
95                                 if (fileInputStream != null) {
96                                         try {
97                                                 fileInputStream.close();
98                                         } catch (IOException ioe1) {
99                                         }
100                                 }
101                                 if (fileByteOutputStream != null) {
102                                         try {
103                                                 fileByteOutputStream.close();
104                                         } catch (IOException ioe1) {
105                                         }
106                                 }
107                         }
108                 }
109                 rootNode = new SimpleXML("configuration");
110         }
111
112         public boolean save() {
113                 File configurationFile = new File(filename);
114                 if (!configurationFile.exists()) {
115                         File configurationFilePath = configurationFile.getParentFile();
116                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
117                                 return false;
118                         }
119                 }
120                 FileOutputStream fileOutputStream = null;
121                 ByteArrayInputStream configurationInputStream = null;
122                 try {
123                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
124                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
125                         fileOutputStream = new FileOutputStream(configurationFile);
126                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
127                         return true;
128                 } catch (IOException ioe1) {
129                 } finally {
130                         if (configurationInputStream != null) {
131                                 try {
132                                         configurationInputStream.close();
133                                 } catch (IOException ioe1) {
134                                 }
135                         }
136                         if (fileOutputStream != null) {
137                                 try {
138                                         fileOutputStream.close();
139                                 } catch (IOException ioe1) {
140                                 }
141                         }
142                 }
143                 return false;
144         }
145
146         private String getNodeValue(String[] nodeNames, String defaultValue) {
147                 SimpleXML node = rootNode;
148                 int nodeIndex = 0;
149                 while ((node != null) && (nodeIndex < nodeNames.length)) {
150                         node = node.getNode(nodeNames[nodeIndex++]);
151                 }
152                 if (node == null) {
153                         return defaultValue;
154                 }
155                 return node.getValue();
156         }
157
158         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
159                 try {
160                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
161                 } catch (NumberFormatException nfe1) {
162                 }
163                 return defaultValue;
164         }
165
166         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
167                 String nodeValue = getNodeValue(nodeNames, null);
168                 if (nodeValue == null) {
169                         return defaultValue;
170                 }
171                 return Boolean.parseBoolean(nodeValue);
172         }
173
174         public String getNodeAddress() {
175                 return getNodeValue(new String[] { "node-address" }, "localhost");
176         }
177
178         public void setNodeAddress(String nodeAddress) {
179                 rootNode.replace("node-address", nodeAddress);
180         }
181
182         public int getNodePort() {
183                 return getNodeIntValue(new String[] { "node-port" }, 9481);
184         }
185
186         public void setNodePort(int nodePort) {
187                 rootNode.replace("node-port", String.valueOf(nodePort));
188         }
189
190         public boolean isSkipNodePage() {
191                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
192         }
193
194         public void setSkipNodePage(boolean skipNodePage) {
195                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
196         }
197
198         public Project[] getProjects() {
199                 List<Project> projects = new ArrayList<Project>();
200                 SimpleXML projectsNode = rootNode.getNode("project-list");
201                 if (projectsNode != null) {
202                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
203                         for (SimpleXML projectNode: projectNodes) {
204                                 try {
205                                         Project project = null;
206                                         SimpleXML typeNode = projectNode.getNode("type");
207                                         if ("edition".equals(typeNode.getValue())) {
208                                                 EditionProject editionProject = new EditionProject();
209                                                 project = editionProject;
210                                                 editionProject.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue()));
211                                         }
212                                         projects.add(project);
213                                         project.setDescription(projectNode.getNode("description").getValue());
214                                         project.setIndexFile(projectNode.getNode("index-file").getValue());
215                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue()));
216                                         project.setLocalPath(projectNode.getNode("local-path").getValue());
217                                         project.setName(projectNode.getNode("name").getValue());
218                                         project.setPath(projectNode.getNode("path").getValue());
219                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue());
220                                         project.setRequestURI(projectNode.getNode("request-uri").getValue());
221                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
222                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
223                                         if (fileOptionsNode != null) {
224                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
225                                                 for (SimpleXML fileOptionNode: fileOptionNodes) {
226                                                         String filename = fileOptionNode.getNode("filename").getValue();
227                                                         FileOption fileOption = project.getFileOption(filename);
228                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
229                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue());
230                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue());
231                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
232                                                         if (fileOptionNode.getNode("replace-edition") != null) {
233                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
234                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
235                                                         }
236                                                         fileOptions.put(filename, fileOption);
237                                                 }
238                                         }
239                                         project.setFileOptions(fileOptions);
240                                 } catch (NumberFormatException nfe1) {
241                                         nfe1.printStackTrace();
242                                 }
243                         }
244                 }
245                 return projects.toArray(new Project[projects.size()]);
246         }
247
248         public void setProjects(Project[] projects) {
249                 SimpleXML projectsNode = new SimpleXML("project-list");
250                 for (Project project: projects) {
251                         SimpleXML projectNode = projectsNode.append("project");
252                         if (project instanceof EditionProject) {
253                                 projectNode.append("type", "edition");
254                                 projectNode.append("edition", String.valueOf(((EditionProject) project).getEdition()));
255                         }
256                         projectNode.append("description", project.getDescription());
257                         projectNode.append("index-file", project.getIndexFile());
258                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
259                         projectNode.append("local-path", project.getLocalPath());
260                         projectNode.append("name", project.getName());
261                         projectNode.append("path", project.getPath());
262                         projectNode.append("insert-uri", project.getInsertURI());
263                         projectNode.append("request-uri", project.getRequestURI());
264                         SimpleXML fileOptionsNode = projectNode.append("file-options");
265                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
266                         while (entries.hasNext()) {
267                                 Entry<String, FileOption> entry = entries.next();
268                                 FileOption fileOption = entry.getValue();
269                                 if (fileOption.isCustom()) {
270                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
271                                         fileOptionNode.append("filename", entry.getKey());
272                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
273                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
274                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
275                                         fileOptionNode.append("container", fileOption.getContainer());
276                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
277                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
278                                 }
279                         }
280                 }
281                 rootNode.replace(projectsNode);
282         }
283
284         public Locale getLocale() {
285                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
286                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
287                 if (country != null) {
288                         return new Locale(language, country);
289                 }
290                 return new Locale(language);
291         }
292
293         public void setLocale(Locale locale) {
294                 SimpleXML i18nNode = new SimpleXML("i18n");
295                 if (locale.getCountry().length() != 0) {
296                         i18nNode.append("country", locale.getCountry());
297                 }
298                 i18nNode.append("language", locale.getLanguage());
299                 rootNode.replace(i18nNode);
300                 return;
301         }
302         
303         public Node[] getNodes() {
304                 SimpleXML nodesNode = rootNode.getNode("nodes");
305                 if (nodesNode == null) {
306                         String hostname = getNodeAddress();
307                         int port = getNodePort();
308                         return new Node[] { new Node(hostname, port, "Node") };
309                 }
310                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
311                 Node[] returnNodes = new Node[nodeNodes.length];
312                 int nodeIndex = 0;
313                 for (SimpleXML nodeNode: nodeNodes) {
314                         String name = nodeNode.getNode("name").getValue();
315                         String hostname = nodeNode.getNode("hostname").getValue();
316                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
317                         Node node = new Node(hostname, port, name);
318                         returnNodes[nodeIndex++] = node;
319                 }
320                 return returnNodes;
321         }
322         
323         public void setNodes(Node[] nodes) {
324                 SimpleXML nodesNode = new SimpleXML("nodes");
325                 for (Node node: nodes) {
326                         SimpleXML nodeNode = nodesNode.append("node");
327                         nodeNode.append("name", node.getName());
328                         nodeNode.append("hostname", node.getHostname());
329                         nodeNode.append("port", String.valueOf(node.getPort()));
330                 }
331                 rootNode.replace(nodesNode);
332                 rootNode.remove("node-address");
333                 rootNode.remove("node-port");
334         }
335
336         public void setSelectedNode(Node selectedNode) {
337                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
338                 selectedNodeNode.append("name", selectedNode.getName());
339                 selectedNodeNode.append("hostname", selectedNode.getHostname());
340                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
341                 rootNode.replace(selectedNodeNode);
342         }
343         
344         public Node getSelectedNode() {
345                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
346                 if (selectedNodeNode == null) {
347                         return null;
348                 }
349                 String name = selectedNodeNode.getNode("name").getValue();
350                 String hostname = selectedNodeNode.getNode("hostname").getValue();
351                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
352                 return new Node(hostname, port, name);
353         }
354         
355 }