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.jdom.Document; |
---|
8 | import org.jdom.input.SAXBuilder; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
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 | * fonction qui retourne le chemin du fichier de configuration |
---|
52 | * @return |
---|
53 | */ |
---|
54 | private static String getConfigFilePath() { |
---|
55 | return System.getProperty("user.home") + "/.jiwigo/config.xml"; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Creation du fichier de configuration par défaut |
---|
60 | * @throws IOException |
---|
61 | */ |
---|
62 | public static void creerFichierParDefaut() throws IOException { |
---|
63 | File outFile = new File(getConfigFilePath()); |
---|
64 | if (!outFile.exists()) { |
---|
65 | StringBuffer xml = new StringBuffer(); |
---|
66 | xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
---|
67 | xml.append("<config>\n"); |
---|
68 | xml.append(" <chunk_size>0.5</chunk_size>\n"); |
---|
69 | xml.append(" <width_original>800</width_original>\n"); |
---|
70 | xml.append(" <height_original>600</height_original>\n"); |
---|
71 | xml.append(" <password></password>\n"); |
---|
72 | xml.append(" <login></login>\n"); |
---|
73 | xml.append(" <url></url>\n"); |
---|
74 | xml.append("</config>"); |
---|
75 | |
---|
76 | FileWriter out = new FileWriter(outFile); |
---|
77 | out.write(xml.toString()); |
---|
78 | out.close(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Fonction qui retourne la valeur d'une préférence |
---|
84 | * @param key la cle de la preference |
---|
85 | * @return la valeur |
---|
86 | */ |
---|
87 | public static String getValue(String key) { |
---|
88 | SAXBuilder sxb = new SAXBuilder(); |
---|
89 | try { |
---|
90 | Document document = sxb.build(new File(getConfigFilePath())); |
---|
91 | return document.getRootElement().getChildText(key); |
---|
92 | } catch (Exception e) { |
---|
93 | LOG.error(Outil.getStackTrace(e)); |
---|
94 | return null; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Fonction qui attribue une valeur à une préférence |
---|
100 | * @param key la cle |
---|
101 | * @param text la valeur |
---|
102 | */ |
---|
103 | public static void setValue(String key, String text) { |
---|
104 | SAXBuilder sxb = new SAXBuilder(); |
---|
105 | try { |
---|
106 | Document document = sxb.build(new File(getConfigFilePath())); |
---|
107 | document.getRootElement().getChild(key).setText(text); |
---|
108 | FileWriter out = new FileWriter(getConfigFilePath()); |
---|
109 | out.write(Outil.documentToString(document)); |
---|
110 | out.close(); |
---|
111 | } catch (Exception e) { |
---|
112 | LOG.error(Outil.getStackTrace(e)); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|