KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > config > XMLConfigFile


1 /*
2  * NewConfigFile.java May 8, 2007
3  */

4 package net.sourceforge.tracelog.config;
5
6 import java.io.File JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.swt.SWT;
15 import org.exolab.castor.mapping.Mapping;
16 import org.exolab.castor.xml.Marshaller;
17 import org.exolab.castor.xml.Unmarshaller;
18 import org.xml.sax.InputSource JavaDoc;
19
20 public class XMLConfigFile extends ConfigFile {
21     private static Logger log = Logger.getLogger(XMLConfigFile.class);
22
23     private Mapping mapping;
24
25     // private static final String SERVER_CONFIG_PROP_KEY_PREFIX =
26
// "server.config.";
27
private static final String JavaDoc SERVER_CONFIG_DELIM = "|";
28     //
29
// private HashMap<String, String> configFileProperties = new
30
// HashMap<String, String>();
31

32     private boolean isNewConfigFile;
33
34     XMLConfigFile() {
35         super();
36         super.configFile = new File JavaDoc(System.getProperty("user.home") + projectProperties.getXMLConfigFilePath());
37
38         this.mapping = new Mapping();
39         this.isNewConfigFile = false;
40
41         loadXMLMapping();
42         setupDefaultXMLFile();
43     }
44
45     private void loadXMLMapping() {
46         try {
47             mapping.loadMapping(new InputSource JavaDoc(getClass().getResourceAsStream("mapping.xml")));
48         }
49         catch (Exception JavaDoc e) {
50             log.error("Cannot load mapping file: " + e);
51             e.printStackTrace();
52         }
53     }
54
55     private void setupDefaultXMLFile() {
56         try {
57             // create directory structure and config file only if not exists
58
if (!configFile.exists()) {
59                 isNewConfigFile = true;
60
61                 configFile.getParentFile().mkdirs();
62
63                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(configFile);
64                 pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
65                 pw.println("<user-config>");
66                 pw.println("</user-config>");
67                 pw.close();
68
69                 createDefaultConfigFile();
70
71             }
72         }
73         catch (Exception JavaDoc e) {
74             log.error("Cannot create a new config file: " + e);
75             e.printStackTrace();
76         }
77
78         try {
79             convertFromOldConfigFile();
80         }
81         catch (Exception JavaDoc e) {
82             log.error("Cannot convert from old config file to new config file: " + e);
83             e.printStackTrace();
84         }
85     }
86
87     private void convertFromOldConfigFile() throws Exception JavaDoc {
88
89         NewConfigFile newConfigFile = new NewConfigFile();
90
91         // new config file and old config file exists, then
92
// perform conversion
93
if (isNewConfigFile && newConfigFile.isConfigFileExists()) {
94             List JavaDoc<LogBean> oldLogBeanList = newConfigFile.getConfig();
95
96             List JavaDoc<LogFile> logFiles = new LinkedList JavaDoc<LogFile>();
97
98             for (LogBean logBean : oldLogBeanList) {
99                 LogFile logFile = new LogFile(logBean.getLogOrder(), logBean.getLogName(), logBean.getLogFilePath(), logBean.getForegroundColor(),
100                         logBean.getBackgroundColor());
101
102                 logFiles.add(logFile);
103             }
104
105             LogGroup logGroup = new LogGroup();
106             logGroup.setGroupName("default");
107             logGroup.setGroupOrder(1);
108             logGroup.setLogFiles(logFiles);
109
110             List JavaDoc<LogGroup> logGroups = new LinkedList JavaDoc<LogGroup>();
111             logGroups.add(logGroup);
112
113             UserConfig userConfig = new UserConfig();
114             userConfig.setTextEditorPath("notepad.exe");
115             userConfig.setLogGroups(logGroups);
116
117             saveUserConfig(userConfig);
118         }
119     }
120
121     public UserConfig getUserConfig() {
122         UserConfig userConfig = null;
123
124         try {
125             Unmarshaller unmarshaller = new Unmarshaller(mapping);
126             userConfig = (UserConfig) unmarshaller.unmarshal(new FileReader JavaDoc(configFile));
127         }
128         catch (Exception JavaDoc e) {
129             log.error("Cannot retrieve user config from file: " + e);
130             e.printStackTrace();
131         }
132
133         return userConfig;
134     }
135
136     public void saveConfig(List JavaDoc<LogBean> logBeanList) {
137         try {
138             throw new Exception JavaDoc("Deprecated");
139         }
140         catch (Exception JavaDoc e) {
141             log.error(e);
142             e.printStackTrace();
143         }
144     }
145
146     public List JavaDoc<LogBean> getConfig() {
147         try {
148             throw new Exception JavaDoc("Deprecated");
149         }
150         catch (Exception JavaDoc e) {
151             log.error(e);
152             e.printStackTrace();
153         }
154         return null;
155     }
156
157     public void saveUserConfig(UserConfig userConfig) {
158         try {
159             Marshaller marshaller = new Marshaller(new FileWriter JavaDoc(configFile));
160             marshaller.setMapping(mapping);
161             marshaller.marshal(userConfig);
162         }
163         catch (Exception JavaDoc e) {
164             log.error("Cannot save user config into file: " + e);
165             e.printStackTrace();
166         }
167
168     }
169
170     private void createDefaultConfigFile() throws Exception JavaDoc {
171         String JavaDoc[] sampleServerConfigs = projectProperties.getSampleServerConfigs();
172
173         List JavaDoc<LogFile> logFiles = new LinkedList JavaDoc<LogFile>();
174
175         for (int i = 0; i < sampleServerConfigs.length; ++i) {
176             String JavaDoc logName = sampleServerConfigs[i].substring(0, sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM));
177             String JavaDoc logPath = sampleServerConfigs[i].substring(sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM) + 1);
178             logFiles.add(new LogFile((i + 1), logName, logPath, SWT.COLOR_BLACK, SWT.COLOR_WHITE));
179         }
180
181         LogGroup logGroup = new LogGroup();
182         logGroup.setGroupName("default");
183         logGroup.setGroupOrder(1);
184         logGroup.setLogFiles(logFiles);
185
186         List JavaDoc<LogGroup> logGroups = new LinkedList JavaDoc<LogGroup>();
187         logGroups.add(logGroup);
188
189         UserConfig userConfig = new UserConfig();
190         userConfig.setTextEditorPath("notepad.exe");
191         userConfig.setLogGroups(logGroups);
192
193         saveUserConfig(userConfig);
194
195     }
196 }
197
Popular Tags