Source code: com/barteo/emulator/app/Config.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2002 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.emulator.app;
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Enumeration;
30 import java.util.Vector;
31
32 import com.barteo.emulator.app.util.DeviceEntry;
33
34 import nanoxml.XMLElement;
35 import nanoxml.XMLParseException;
36
37
38 public class Config
39 {
40 private static File configPath = new File(System.getProperty("user.home") + "/.microemulator/");
41 private static Vector devices = new Vector();
42
43
44 public static void loadConfig(String configFileName)
45 {
46 File configFile = new File(configPath, configFileName);
47
48 DeviceEntry defaultDevice;
49 if (configFileName.equals("config-swt.xml")) {
50 defaultDevice =
51 new DeviceEntry("Default device", null, "com.barteo.emulator.device.swt.SwtDevice", true, false);
52 } else if (configFileName.equals("config-awt.xml")) {
53 defaultDevice =
54 new DeviceEntry("Default device", null, "com.barteo.emulator.device.applet.AppletDevice", true, false);
55 } else {
56 defaultDevice =
57 new DeviceEntry("Default device", null, "com.barteo.emulator.device.j2se.J2SEDevice", true, false);
58 }
59 devices.add(defaultDevice);
60
61 String xml = "";
62 try {
63 InputStream dis = new BufferedInputStream(new FileInputStream(configFile));
64 while (dis.available() > 0) {
65 byte[] b = new byte[dis.available()];
66 dis.read(b);
67 xml += new String(b);
68 }
69 } catch (FileNotFoundException ex) {
70 loadDefaultConfig();
71 return;
72 } catch (IOException ex) {
73 System.out.println(ex);
74 loadDefaultConfig();
75 return;
76 }
77
78 XMLElement configRoot = new XMLElement();
79 try {
80 configRoot.parseString(xml);
81 } catch (XMLParseException ex) {
82 System.err.println(ex);
83 loadDefaultConfig();
84 return;
85 }
86
87 for (Enumeration e = configRoot.enumerateChildren(); e.hasMoreElements(); ) {
88 XMLElement tmp = (XMLElement) e.nextElement();
89 if (tmp.getName().equals("devices")) {
90 for (Enumeration e_device = tmp.enumerateChildren(); e_device.hasMoreElements(); ) {
91 XMLElement tmp_device = (XMLElement) e_device.nextElement();
92 if (tmp_device.getName().equals("device")) {
93 boolean devDefault = false;
94 if (tmp_device.getStringAttribute("default") != null && tmp_device.getStringAttribute("default").equals("true")) {
95 devDefault = true;
96 defaultDevice.setDefaultDevice(false);
97 }
98 String devName = null;
99 String devFile = null;
100 String devClass = null;
101 for (Enumeration e_cont = tmp_device.enumerateChildren(); e_cont.hasMoreElements(); ) {
102 XMLElement tmp_cont = (XMLElement) e_cont.nextElement();
103 if (tmp_cont.getName().equals("name")) {
104 devName = tmp_cont.getContent();
105 } else if (tmp_cont.getName().equals("filename")) {
106 devFile = tmp_cont.getContent();
107 } else if (tmp_cont.getName().equals("class")) {
108 devClass = tmp_cont.getContent();
109 }
110 }
111 devices.add(new DeviceEntry(devName, devFile, devClass, devDefault));
112 }
113 }
114 }
115 }
116 }
117
118
119 private static void loadDefaultConfig()
120 {
121 }
122
123
124 public static void saveConfig(String configFileName)
125 {
126 File configFile = new File(configPath, configFileName);
127
128 XMLElement xmlTmp;
129 XMLElement xmlRoot = new XMLElement();
130 xmlRoot.setName("config");
131 XMLElement xmlDevices = new XMLElement();
132 xmlDevices.setName("devices");
133 xmlRoot.addChild(xmlDevices);
134
135 for (Enumeration e = devices.elements(); e.hasMoreElements(); ) {
136 DeviceEntry entry = (DeviceEntry) e.nextElement();
137 if (!entry.canRemove()) {
138 continue;
139 }
140
141 XMLElement xmlDevice = new XMLElement(false, false);
142 xmlDevice.setName("device");
143 xmlDevices.addChild(xmlDevice);
144 if (entry.isDefaultDevice()) {
145 xmlDevice.setAttribute("default", "true");
146 }
147 xmlTmp = new XMLElement();
148 xmlTmp.setName("name");
149 xmlTmp.setContent(entry.getName());
150 xmlDevice.addChild(xmlTmp);
151 xmlTmp = new XMLElement();
152 xmlTmp.setName("filename");
153 xmlTmp.setContent(entry.getFileName());
154 xmlDevice.addChild(xmlTmp);
155 xmlTmp = new XMLElement();
156 xmlTmp.setName("class");
157 xmlTmp.setContent(entry.getClassName());
158 xmlDevice.addChild(xmlTmp);
159 }
160
161 configPath.mkdirs();
162 try {
163 FileWriter fw = new FileWriter(configFile);
164 xmlRoot.write(fw);
165 fw.close();
166 } catch (IOException ex) {
167 System.out.println(ex);
168 }
169 }
170
171
172 public static File getConfigPath()
173 {
174 return configPath;
175 }
176
177
178 public static Vector getDevices()
179 {
180 return devices;
181 }
182
183 }