source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/util/preferences/PreferencesManagement.java @ 9921

Last change on this file since 9921 was 9921, checked in by mlg, 13 years ago

Adds spring for the dependency injection
Deletion of jdom (not useful just for a class that manipulates a simple XML file)

File size: 4.4 KB
Line 
1package fr.mael.jiwigo.transverse.util.preferences;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6
7import org.w3c.dom.Document;
8import org.w3c.dom.Element;
9
10import 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 */
42public 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    /**
59     * Creation of the default file configuration
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("        <image_quality>1</image_quality>\n");
72            xml.append("        <password></password>\n");
73            xml.append("        <login></login>\n");
74            xml.append("        <url></url>\n");
75            xml.append("        <use_proxy>false</use_proxy>\n");
76            xml.append("        <url_proxy></url_proxy>\n");
77            xml.append("        <port_proxy></port_proxy>\n");
78            xml.append("        <user_proxy></user_proxy>\n");
79            xml.append("        <password_proxy></password_proxy>\n");
80            xml.append("</config>");
81            FileWriter out = new FileWriter(outFile);
82            out.write(xml.toString());
83            out.close();
84        }
85    }
86
87    /**
88     * Function that returns the value of a preference
89     * @param key the key of the preference
90     * @return the value
91     */
92    public static String getValue(String key) {
93        try {
94            Document document = Tools.readFileAsDocument(getConfigFilePath());
95            return Tools.getStringValueDom(document.getDocumentElement(), key);
96        } catch (Exception e) {
97            LOG.error(Tools.getStackTrace(e));
98            return null;
99        }
100    }
101
102    /**
103     * Function that gives a value to a preference key
104     * @param key the key
105     * @param text the value
106     */
107    public static void setValue(String key, String text) {
108        try {
109            Document document = Tools.readFileAsDocument(getConfigFilePath());
110            try {
111                Tools.setStringValueDom(document.getDocumentElement(), key, text);
112            } catch (Exception e) {
113                e.printStackTrace();
114                Element element = document.createElement(key);
115                Tools.setStringValueDom(element, key, text);
116                document.getDocumentElement().appendChild(element);
117            }
118            Tools.writeXmlFile(document, getConfigFilePath());
119        } catch (Exception e) {
120            LOG.error(Tools.getStackTrace(e));
121        }
122    }
123}
Note: See TracBrowser for help on using the repository browser.