1 | package fr.mael.jiwigo.transverse.util.preferences; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileWriter; |
---|
5 | import java.io.IOException; |
---|
6 | |
---|
7 | import org.w3c.dom.Document; |
---|
8 | import org.w3c.dom.Element; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
11 | |
---|
12 | /** |
---|
13 | Copyright (c) 2010, Mael |
---|
14 | All rights reserved. |
---|
15 | |
---|
16 | Redistribution and use in source and binary forms, with or without |
---|
17 | modification, are permitted provided that the following conditions are met: |
---|
18 | * Redistributions of source code must retain the above copyright |
---|
19 | notice, this list of conditions and the following disclaimer. |
---|
20 | * Redistributions in binary form must reproduce the above copyright |
---|
21 | notice, this list of conditions and the following disclaimer in the |
---|
22 | documentation and/or other materials provided with the distribution. |
---|
23 | * Neither the name of jiwigo nor the |
---|
24 | names of its contributors may be used to endorse or promote products |
---|
25 | derived from this software without specific prior written permission. |
---|
26 | |
---|
27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
28 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
29 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
30 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
31 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
32 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
33 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
34 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
35 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
36 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
37 | |
---|
38 | * Classe de gestion des preferences |
---|
39 | * @author mael |
---|
40 | * |
---|
41 | */ |
---|
42 | public class PreferencesManagement { |
---|
43 | |
---|
44 | /** |
---|
45 | * Logger |
---|
46 | */ |
---|
47 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
48 | .getLog(PreferencesManagement.class); |
---|
49 | |
---|
50 | /** |
---|
51 | * function that returns the path of the configuration file |
---|
52 | * @return |
---|
53 | */ |
---|
54 | private static String getConfigFilePath() { |
---|
55 | return System.getProperty("user.home") + "/.jiwigo/config.xml"; |
---|
56 | } |
---|
57 | |
---|
58 | public static void checkValues() { |
---|
59 | Document document = Tools.readFileAsDocument(getConfigFilePath()); |
---|
60 | Element el = (Element) document.getDocumentElement().getElementsByTagName("sending_method").item(0); |
---|
61 | if (el == null) { |
---|
62 | setValue("sending_method", "0"); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Creation of the default file configuration |
---|
68 | * @throws IOException |
---|
69 | */ |
---|
70 | public static void creerFichierParDefaut() throws IOException { |
---|
71 | |
---|
72 | File outFile = new File(getConfigFilePath()); |
---|
73 | if (!outFile.exists()) { |
---|
74 | StringBuffer xml = new StringBuffer(); |
---|
75 | xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
---|
76 | xml.append("<config>\n"); |
---|
77 | xml.append(" <chunk_size>0.5</chunk_size>\n"); |
---|
78 | xml.append(" <width_original>800</width_original>\n"); |
---|
79 | xml.append(" <height_original>600</height_original>\n"); |
---|
80 | xml.append(" <image_quality>1</image_quality>\n"); |
---|
81 | xml.append(" <password></password>\n"); |
---|
82 | xml.append(" <login></login>\n"); |
---|
83 | xml.append(" <url></url>\n"); |
---|
84 | xml.append(" <use_proxy>false</use_proxy>\n"); |
---|
85 | xml.append(" <url_proxy></url_proxy>\n"); |
---|
86 | xml.append(" <port_proxy></port_proxy>\n"); |
---|
87 | xml.append(" <user_proxy></user_proxy>\n"); |
---|
88 | xml.append(" <password_proxy></password_proxy>\n"); |
---|
89 | xml.append(" <sending_method>0</sending_method>\n"); |
---|
90 | xml.append("</config>"); |
---|
91 | FileWriter out = new FileWriter(outFile); |
---|
92 | out.write(xml.toString()); |
---|
93 | out.close(); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Function that returns the value of a preference |
---|
99 | * @param key the key of the preference |
---|
100 | * @return the value |
---|
101 | */ |
---|
102 | public static String getValue(String key) { |
---|
103 | try { |
---|
104 | Document document = Tools.readFileAsDocument(getConfigFilePath()); |
---|
105 | return Tools.getStringValueDom(document.getDocumentElement(), key); |
---|
106 | } catch (Exception e) { |
---|
107 | LOG.error(Tools.getStackTrace(e)); |
---|
108 | return null; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Function that gives a value to a preference key |
---|
114 | * @param key the key |
---|
115 | * @param text the value |
---|
116 | */ |
---|
117 | public static void setValue(String key, String text) { |
---|
118 | try { |
---|
119 | Document document = Tools.readFileAsDocument(getConfigFilePath()); |
---|
120 | try { |
---|
121 | Tools.setStringValueDom(document.getDocumentElement(), key, text); |
---|
122 | } catch (Exception e) { |
---|
123 | e.printStackTrace(); |
---|
124 | Element element = document.createElement(key); |
---|
125 | element.setTextContent(text); |
---|
126 | document.getDocumentElement().appendChild(element); |
---|
127 | |
---|
128 | } |
---|
129 | Tools.writeXmlFile(document, getConfigFilePath()); |
---|
130 | } catch (Exception e) { |
---|
131 | LOG.error(Tools.getStackTrace(e)); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|