2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.main;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
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;
35 import java.util.Map.Entry;
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;
46 * @author David Roden <droden@gmail.com>
47 * @version $Id: Configuration.java 418 2006-03-29 17:49:16Z bombe $
49 public class Configuration {
51 private String filename;
52 private String lockFilename;
53 private SimpleXML rootNode;
55 public Configuration() {
56 filename = System.getProperty("user.home") + "/.jSite/config7";
57 lockFilename = System.getProperty("user.home") + "/.jSite/lock7";
61 private boolean createConfigDirectory() {
62 File configDirectory = new File(System.getProperty("user.home"), ".jSite");
63 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
66 public boolean createLockFile() {
67 if (!createConfigDirectory()) {
70 File lockFile = new File(lockFilename);
71 lockFile.deleteOnExit();
73 return lockFile.createNewFile();
74 } catch (IOException e) {
79 private void readConfiguration() {
80 File configurationFile = new File(filename);
81 if (configurationFile.exists()) {
82 ByteArrayOutputStream fileByteOutputStream = null;
83 FileInputStream fileInputStream = null;
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));
92 } catch (FileNotFoundException e) {
93 } catch (IOException e) {
95 if (fileInputStream != null) {
97 fileInputStream.close();
98 } catch (IOException ioe1) {
101 if (fileByteOutputStream != null) {
103 fileByteOutputStream.close();
104 } catch (IOException ioe1) {
109 rootNode = new SimpleXML("configuration");
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()) {
120 FileOutputStream fileOutputStream = null;
121 ByteArrayInputStream configurationInputStream = null;
123 byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
124 configurationInputStream = new ByteArrayInputStream(configurationBytes);
125 fileOutputStream = new FileOutputStream(configurationFile);
126 StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
128 } catch (IOException ioe1) {
130 if (configurationInputStream != null) {
132 configurationInputStream.close();
133 } catch (IOException ioe1) {
136 if (fileOutputStream != null) {
138 fileOutputStream.close();
139 } catch (IOException ioe1) {
146 private String getNodeValue(String[] nodeNames, String defaultValue) {
147 SimpleXML node = rootNode;
149 while ((node != null) && (nodeIndex < nodeNames.length)) {
150 node = node.getNode(nodeNames[nodeIndex++]);
155 return node.getValue();
158 private int getNodeIntValue(String[] nodeNames, int defaultValue) {
160 return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
161 } catch (NumberFormatException nfe1) {
166 private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
167 String nodeValue = getNodeValue(nodeNames, null);
168 if (nodeValue == null) {
171 return Boolean.parseBoolean(nodeValue);
174 public String getNodeAddress() {
175 return getNodeValue(new String[] { "node-address" }, "localhost");
178 public void setNodeAddress(String nodeAddress) {
179 rootNode.replace("node-address", nodeAddress);
182 public int getNodePort() {
183 return getNodeIntValue(new String[] { "node-port" }, 9481);
186 public void setNodePort(int nodePort) {
187 rootNode.replace("node-port", String.valueOf(nodePort));
190 public boolean isSkipNodePage() {
191 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
194 public void setSkipNodePage(boolean skipNodePage) {
195 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
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) {
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()));
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()));
236 fileOptions.put(filename, fileOption);
239 project.setFileOptions(fileOptions);
240 } catch (NumberFormatException nfe1) {
241 nfe1.printStackTrace();
245 return projects.toArray(new Project[projects.size()]);
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()));
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()));
281 rootNode.replace(projectsNode);
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);
290 return new Locale(language);
293 public void setLocale(Locale locale) {
294 SimpleXML i18nNode = new SimpleXML("i18n");
295 if (locale.getCountry().length() != 0) {
296 i18nNode.append("country", locale.getCountry());
298 i18nNode.append("language", locale.getLanguage());
299 rootNode.replace(i18nNode);
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") };
310 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
311 Node[] returnNodes = new Node[nodeNodes.length];
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;
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()));
331 rootNode.replace(nodesNode);
332 rootNode.remove("node-address");
333 rootNode.remove("node-port");
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);
344 public Node getSelectedNode() {
345 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
346 if (selectedNodeNode == null) {
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);