1 4 package net.sourceforge.tracelog.config; 5 6 import java.io.File ; 7 import java.io.FileReader ; 8 import java.io.FileWriter ; 9 import java.io.PrintWriter ; 10 import java.util.LinkedList ; 11 import java.util.List ; 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 ; 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_DELIM = "|"; 28 32 private boolean isNewConfigFile; 33 34 XMLConfigFile() { 35 super(); 36 super.configFile = new File (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 (getClass().getResourceAsStream("mapping.xml"))); 48 } 49 catch (Exception e) { 50 log.error("Cannot load mapping file: " + e); 51 e.printStackTrace(); 52 } 53 } 54 55 private void setupDefaultXMLFile() { 56 try { 57 if (!configFile.exists()) { 59 isNewConfigFile = true; 60 61 configFile.getParentFile().mkdirs(); 62 63 PrintWriter pw = new PrintWriter (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 e) { 74 log.error("Cannot create a new config file: " + e); 75 e.printStackTrace(); 76 } 77 78 try { 79 convertFromOldConfigFile(); 80 } 81 catch (Exception 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 { 88 89 NewConfigFile newConfigFile = new NewConfigFile(); 90 91 if (isNewConfigFile && newConfigFile.isConfigFileExists()) { 94 List <LogBean> oldLogBeanList = newConfigFile.getConfig(); 95 96 List <LogFile> logFiles = new LinkedList <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 <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 public UserConfig getUserConfig() { 122 UserConfig userConfig = null; 123 124 try { 125 Unmarshaller unmarshaller = new Unmarshaller(mapping); 126 userConfig = (UserConfig) unmarshaller.unmarshal(new FileReader (configFile)); 127 } 128 catch (Exception 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 <LogBean> logBeanList) { 137 try { 138 throw new Exception ("Deprecated"); 139 } 140 catch (Exception e) { 141 log.error(e); 142 e.printStackTrace(); 143 } 144 } 145 146 public List <LogBean> getConfig() { 147 try { 148 throw new Exception ("Deprecated"); 149 } 150 catch (Exception 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 (configFile)); 160 marshaller.setMapping(mapping); 161 marshaller.marshal(userConfig); 162 } 163 catch (Exception e) { 164 log.error("Cannot save user config into file: " + e); 165 e.printStackTrace(); 166 } 167 168 } 169 170 private void createDefaultConfigFile() throws Exception { 171 String [] sampleServerConfigs = projectProperties.getSampleServerConfigs(); 172 173 List <LogFile> logFiles = new LinkedList <LogFile>(); 174 175 for (int i = 0; i < sampleServerConfigs.length; ++i) { 176 String logName = sampleServerConfigs[i].substring(0, sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM)); 177 String 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 <LogGroup> logGroups = new LinkedList <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 |