001: package net.sourceforge.tracelog.config;
002:
003: import java.io.File;
004: import java.io.FileReader;
005: import java.io.FileWriter;
006: import java.io.PrintWriter;
007: import java.util.LinkedList;
008: import java.util.List;
009:
010: import org.apache.log4j.Logger;
011: import org.eclipse.swt.SWT;
012: import org.exolab.castor.mapping.Mapping;
013: import org.exolab.castor.xml.Marshaller;
014: import org.exolab.castor.xml.Unmarshaller;
015: import org.xml.sax.InputSource;
016:
017: public class XMLConfigFile extends ConfigFile {
018: private static Logger log = Logger.getLogger(XMLConfigFile.class);
019:
020: private static final String SERVER_CONFIG_DELIM = "|";
021:
022: private boolean isNewConfigFile;
023:
024: private Mapping mapping;
025:
026: XMLConfigFile() {
027: super ();
028: super .configFile = new File(System.getProperty("user.home")
029: + projectProperties.getXMLConfigFilePath());
030:
031: this .mapping = new Mapping();
032: this .isNewConfigFile = false;
033:
034: loadXMLMapping();
035: setupDefaultXMLFile();
036: }
037:
038: public List<LogBean> getConfig() {
039: try {
040: throw new Exception("Deprecated");
041: } catch (Exception e) {
042: log.error(e);
043: e.printStackTrace();
044: }
045: return null;
046: }
047:
048: public UserConfig getUserConfig() {
049: UserConfig userConfig = null;
050:
051: try {
052: Unmarshaller unmarshaller = new Unmarshaller(mapping);
053: userConfig = (UserConfig) unmarshaller
054: .unmarshal(new FileReader(configFile));
055: } catch (Exception e) {
056: log.error("Cannot retrieve user config from file: " + e);
057: e.printStackTrace();
058: }
059:
060: return userConfig;
061: }
062:
063: public void saveConfig(List<LogBean> logBeanList) {
064: try {
065: throw new Exception("Deprecated");
066: } catch (Exception e) {
067: log.error(e);
068: e.printStackTrace();
069: }
070: }
071:
072: public void saveUserConfig(UserConfig userConfig) {
073: try {
074: Marshaller marshaller = new Marshaller(new FileWriter(
075: configFile));
076: marshaller.setMapping(mapping);
077: marshaller.marshal(userConfig);
078: } catch (Exception e) {
079: log.error("Cannot save user config into file: " + e);
080: e.printStackTrace();
081: }
082:
083: }
084:
085: private void convertFromOldConfigFile() throws Exception {
086:
087: NewConfigFile newConfigFile = new NewConfigFile();
088:
089: // new config file and old config file exists, then
090: // perform conversion
091: if (isNewConfigFile && newConfigFile.isConfigFileExists()) {
092: List<LogBean> oldLogBeanList = newConfigFile.getConfig();
093:
094: List<LogFile> logFiles = new LinkedList<LogFile>();
095:
096: for (LogBean logBean : oldLogBeanList) {
097: LogFile logFile = new LogFile(logBean.getLogOrder(),
098: logBean.getLogName(), logBean.getLogFilePath(),
099: logBean.getForegroundColor(), logBean
100: .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<LogGroup> logGroups = new LinkedList<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: private void createDefaultConfigFile() throws Exception {
122: String[] sampleServerConfigs = projectProperties
123: .getSampleServerConfigs();
124:
125: List<LogFile> logFiles = new LinkedList<LogFile>();
126:
127: for (int i = 0; i < sampleServerConfigs.length; ++i) {
128: String logName = sampleServerConfigs[i]
129: .substring(0, sampleServerConfigs[i]
130: .indexOf(SERVER_CONFIG_DELIM));
131: String logPath = sampleServerConfigs[i]
132: .substring(sampleServerConfigs[i]
133: .indexOf(SERVER_CONFIG_DELIM) + 1);
134: logFiles.add(new LogFile((i + 1), logName, logPath,
135: SWT.COLOR_BLACK, SWT.COLOR_WHITE));
136: }
137:
138: LogGroup logGroup = new LogGroup();
139: logGroup.setGroupName("default");
140: logGroup.setGroupOrder(1);
141: logGroup.setLogFiles(logFiles);
142:
143: List<LogGroup> logGroups = new LinkedList<LogGroup>();
144: logGroups.add(logGroup);
145:
146: UserConfig userConfig = new UserConfig();
147: userConfig.setTextEditorPath("notepad.exe");
148: userConfig.setLogGroups(logGroups);
149:
150: saveUserConfig(userConfig);
151:
152: }
153:
154: private void loadXMLMapping() {
155: try {
156: mapping.loadMapping(new InputSource(getClass()
157: .getResourceAsStream("mapping.xml")));
158: } catch (Exception e) {
159: log.error("Cannot load mapping file: " + e);
160: e.printStackTrace();
161: }
162: }
163:
164: private void setupDefaultXMLFile() {
165: try {
166: // create directory structure and config file only if not exists
167: if (!configFile.exists()) {
168: isNewConfigFile = true;
169:
170: configFile.getParentFile().mkdirs();
171:
172: PrintWriter pw = new PrintWriter(configFile);
173: pw
174: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
175: pw.println("<user-config>");
176: pw.println("</user-config>");
177: pw.close();
178:
179: createDefaultConfigFile();
180:
181: }
182: } catch (Exception e) {
183: log.error("Cannot create a new config file: " + e);
184: e.printStackTrace();
185: }
186:
187: try {
188: convertFromOldConfigFile();
189: } catch (Exception e) {
190: log
191: .error("Cannot convert from old config file to new config file: "
192: + e);
193: e.printStackTrace();
194: }
195: }
196: }
|