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

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

Adds proxy support for the connection.

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