1 17 package com.finalist.jaggenerator; 18 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.NodeList ; 22 import org.xml.sax.SAXException ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import java.io.File ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.util.*; 31 32 38 public class ConfigManager { 39 40 private static final String JAG_CONFIG = "jag-config"; 41 private static final File JAG_CONFIG_FILE = new File ("jag-config.xml"); 42 private static final String [] STRING_ARRAY = new String [0]; 43 private static final File DATABASE_DRIVERS_CLASSPATH_FILE = 44 new File ("set_database_drivers_classpath.bat"); 45 private static final String SET_COMMAND = "set DATABASE_DRIVERS_CLASSPATH="; 46 private static final char SEMICOLON = ';'; 47 48 51 protected static final String XMLTAG_GUI = "gui"; 52 53 private static ConfigManager ourInstance; 54 private Document doc; 55 56 57 private ConfigManager() { 58 load(); 59 } 60 61 62 67 public synchronized static ConfigManager getInstance() { 68 if (ourInstance == null) { 69 ourInstance = new ConfigManager(); 70 } 71 return ourInstance; 72 } 73 74 81 public Document getDocument() { 82 return doc; 83 } 84 85 99 public Map retrievePropertiesFromXML(String rootElementName) { 100 HashMap props = new HashMap(); 101 Element propsRoot = (Element ) doc.getElementsByTagName(rootElementName).item(0); 102 if (propsRoot != null) { 103 NodeList children = propsRoot.getChildNodes(); 104 105 for (int j = 0; j < children.getLength(); j++) { 106 if (children.item(j) instanceof Element ) { 107 Element child = (Element ) children.item(j); 108 String key = child.getNodeName(); 109 String [] existingValues = (String []) props.get(key); 110 if (existingValues == null) { 111 props.put(key, new String []{child.getFirstChild().getNodeValue()}); 112 } 113 else { 114 ArrayList newValues = new ArrayList(Arrays.asList(existingValues)); 115 newValues.add(child.getFirstChild().getNodeValue()); 116 props.put(key, newValues.toArray(STRING_ARRAY)); 117 } 118 } 119 } 120 } 121 122 return props; 123 } 124 125 128 public void save() { 129 try { 130 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 131 DocumentBuilder builder = dbf.newDocumentBuilder(); 132 Document newDoc = builder.newDocument(); 133 Element root = newDoc.createElement(JAG_CONFIG); 134 135 Element dbRoot = DatabaseManager.getInstance().appendXML(root); 137 root.appendChild(dbRoot); 138 139 newDoc.appendChild(root); 140 141 HashMap mappingsMap = new HashMap(); 143 mappingsMap.put(DatabaseManager.NAME, DatabaseManager.getInstance().getTypeMappings()); 144 Element mappingsRoot = appendPropertiesAsXML(root, mappingsMap, DatabaseManager.APPSERVER_TYPEMAPPINGS); 145 root.appendChild(mappingsRoot); 146 147 String XMLDoc = com.finalist.jaggenerator.JagGenerator.outXML(newDoc); 148 FileWriter fw = new FileWriter (JAG_CONFIG_FILE); 149 fw.write(XMLDoc); 150 fw.close(); 151 152 } 153 catch (ParserConfigurationException e) { 154 e.printStackTrace(); 155 } 156 catch (IOException e) { 157 e.printStackTrace(); 158 } 159 160 saveDatabaseDriversClasspath(); 161 } 162 163 164 167 private void load() { 168 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 169 DocumentBuilder builder = null; 170 try { 171 builder = dbf.newDocumentBuilder(); 172 doc = builder.parse(JAG_CONFIG_FILE); 173 174 } 175 catch (ParserConfigurationException e) { 176 e.printStackTrace(); 177 JagGenerator.kickTheBucket("The XML parser can't even start up!"); 178 } 179 catch (SAXException e) { 180 e.printStackTrace(); 181 JagGenerator.kickTheBucket("JAG's config JAG_CONFIG_FILE (" + JAG_CONFIG_FILE + ") is invalid!"); 182 } 183 catch (IOException e) { 184 e.printStackTrace(); 185 JagGenerator.kickTheBucket("JAG's can't access the config JAG_CONFIG_FILE (" + JAG_CONFIG_FILE + ")!"); 186 } 187 } 188 189 195 private void saveDatabaseDriversClasspath() { 196 try { 197 DATABASE_DRIVERS_CLASSPATH_FILE.delete(); 198 StringBuffer temp = new StringBuffer (SET_COMMAND); 199 Database[] databases = DatabaseManager.getInstance().getSupportedDatabases(); 200 for (int i = 0; i < databases.length; i++) { 201 temp.append(databases[i].getFilename()); 202 temp.append(SEMICOLON); 203 } 204 205 FileWriter fw = new FileWriter (DATABASE_DRIVERS_CLASSPATH_FILE); 206 fw.write(temp.toString()); 207 fw.flush(); 208 fw.close(); 209 } 210 catch (IOException e) { 211 e.printStackTrace(); } 213 } 214 215 223 private Element appendPropertiesAsXML(Element root, Map props, String rootElementName) { 224 Document doc = root.getOwnerDocument(); 225 Element propsRoot = doc.createElement(rootElementName); 226 227 Iterator i = props.entrySet().iterator(); 228 while (i.hasNext()) { 229 Map.Entry entry = (Map.Entry) i.next(); 230 String propertyName = (String ) entry.getKey(); 231 String [] value = (String []) entry.getValue(); 232 for (int j = 0; j < value.length; j++) { 233 Element nelly = doc.createElement(propertyName); 234 if (value[j] != null) { 235 nelly.appendChild(doc.createTextNode(value[j])); 236 } 237 propsRoot.appendChild(nelly); 238 } 239 } 240 return propsRoot; 241 } 242 } 243 | Popular Tags |