Source code: jgift/ConfigWriter.java
1 /*
2 * This file is part of jgiFT.
3 * Copyright (C) 2003, Jason Shobe
4 *
5 * jgiFT 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.
9 *
10 * jgiFT 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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with jgiFT; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package jgift;
20
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Properties;
28
29 /**
30 * ConfigWriter handles the saving of giFT properties.
31 *
32 * @author Jason Shobe
33 * @version $Revision: 1.2 $
34 */
35 public class ConfigWriter {
36 /**
37 * Creates a new instance of ConfigWriter.
38 *
39 * @param properties the property set to save.
40 */
41 public ConfigWriter(Properties properties) {
42 this.properties = properties;
43 }
44
45 /**
46 * Writes the properties to the configuration file.
47 *
48 * @throws IOException if an I/O error occurs while writing the file.
49 */
50 public void write() throws IOException {
51 HashMap groups = new HashMap();
52 Iterator keys = properties.keySet().iterator();
53
54 while(keys.hasNext()) {
55 String key = (String) keys.next();
56 String group = key.substring(0, key.indexOf('.'));
57 String propKey = key.substring(key.indexOf('.') + 1);
58
59 Properties props = (Properties) groups.get(group);
60
61 if(props == null) {
62 props = new Properties();
63 groups.put(group, props);
64 }
65
66 props.setProperty(propKey, properties.getProperty(key));
67 }
68
69 keys = groups.keySet().iterator();
70 PrintWriter writer = new PrintWriter(new FileWriter(
71 System.getProperty("user.home") + File.separator + ".giFT" +
72 File.separator + "gift.conf"));
73
74 while(keys.hasNext()) {
75 String group = (String) keys.next();
76 writer.println("[" + group + "]");
77 Properties props = (Properties) groups.get(group);
78 Iterator pkeys = props.keySet().iterator();
79
80 while(pkeys.hasNext()) {
81 String key = (String) pkeys.next();
82 String val = props.getProperty(key);
83 writer.println(key + " = " + val);
84 }
85
86 writer.println();
87 }
88
89 writer.flush();
90 writer.close();
91 }
92
93 private Properties properties = null;
94 }
95