1 25 26 package com.rift.coad.lib.configuration.xml; 28 29 import java.util.Date ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.io.BufferedReader ; 35 import java.io.File ; 36 import java.io.FileReader ; 37 import javax.xml.parsers.SAXParserFactory ; 38 import javax.xml.parsers.SAXParser ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.Attributes ; 43 44 45 50 public class XMLConfigurationParser { 51 52 private static final String GENERAL_SECTION = "GENERAL"; 54 private static final String XML_CONFIG_PATH = "xml.config.path"; 55 56 59 public class XMLConfigInfo { 60 61 private String className = null; 63 private Map configInfo = null; 64 65 66 69 public XMLConfigInfo(String className) { 70 this.className = className; 71 configInfo = new HashMap (); 72 } 73 74 75 80 public String getClassName() { 81 return className; 82 } 83 84 85 90 public void addEntry(XMLConfigurationEntry entry) { 91 configInfo.put(entry.getKey(),entry); 92 } 93 94 95 100 public Map getConfigInfo() { 101 return configInfo; 102 } 103 } 104 105 109 public class XMLConfigurationHandler extends DefaultHandler { 110 111 private static final String CONFIGURATION = "configuration"; 113 private static final String OBJECT_SECTION = "object"; 114 private static final String OBJECT_SECTION_NAME = "name"; 115 private static final String OBJECT_ENTRY = "entry"; 116 private static final String OBJECT_ENTRY_KEY = "key"; 117 private static final String OBJECT_ENTRY_TYPE = "type"; 118 119 private boolean inConfiguration = false; 121 private boolean inObjectSection = false; 122 private boolean inObjectEntry = false ; 123 124 private String inData = null; 126 private XMLConfigInfo sectionConfig = null; 127 private XMLConfigurationEntry entry = null; 128 129 private Map configSections = null; 131 132 137 public XMLConfigurationHandler(Map configSections) { 138 this.configSections = configSections; 139 } 140 141 142 145 public void startElement(String uri, String localName, String qName, 146 Attributes attributes) throws SAXException { 147 try { 148 if (qName.compareToIgnoreCase(CONFIGURATION) == 0) { 150 inConfiguration = true; 151 } else if (inConfiguration && 152 qName.compareToIgnoreCase(OBJECT_SECTION) == 0) { 153 String name = (String )attributes.getValue(OBJECT_SECTION_NAME); 154 if (name == null) { 155 name = XMLConfigurationParser.GENERAL_SECTION; 156 } 157 sectionConfig = new XMLConfigInfo(name); 158 inObjectSection = true; 159 } else if (inConfiguration && inObjectSection && 160 qName.compareToIgnoreCase(OBJECT_ENTRY) == 0) { 161 String name = (String )attributes.getValue(OBJECT_ENTRY_KEY); 162 String type = (String )attributes.getValue(OBJECT_ENTRY_TYPE); 163 entry = new XMLConfigurationEntry(); 164 entry.setKey(name); 165 entry.setType(new XMLConfigurationType(type)); 166 inData = ""; 167 inObjectEntry = true; 168 } 169 } catch (Exception ex) { 170 throw new SAXException ("Failed to handle the start element : " + 171 ex.getMessage()); 172 } 173 } 174 175 178 public void characters(char[] ch, int start, int length) { 179 if (inObjectEntry) { 180 inData += new String (ch,start,length); 181 } 182 } 183 184 187 public void endElement(String uri, String localName, String qName) 188 throws SAXException { 189 try { 190 if (qName.compareToIgnoreCase(CONFIGURATION) == 0) { 192 inConfiguration = false; 193 } else if (inConfiguration && 194 qName.compareToIgnoreCase(OBJECT_SECTION) == 0) { 195 configSections.put(sectionConfig.getClassName(),sectionConfig); 196 inObjectSection = false; 197 } else if (inConfiguration && inObjectSection && 198 qName.compareToIgnoreCase(OBJECT_ENTRY) == 0) { 199 entry.setValueFromString(inData.trim()); 200 if (entry.isIntiailized() == false) { 201 throw new SAXException ( 202 "An entry has not been initialized"); 203 } 204 sectionConfig.addEntry(entry); 205 entry = null; 206 inData = ""; 207 inObjectEntry = false; 208 } 209 } catch (Exception ex) { 210 throw new SAXException ("Failed to handle the end element : " + 211 ex.getMessage(),ex); 212 } 213 } 214 215 } 216 217 private Map configSections = null; 219 private XMLConfigurationHandler handler = null; 220 private long lastModifiedAtRead = 0; 221 private File configFile = null; 222 223 226 public XMLConfigurationParser() throws XMLConfigurationException { 227 try { 228 configSections = new HashMap (); 229 handler = new XMLConfigurationHandler(configSections); 230 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 231 configFile = new File ( 232 System.getProperty(XML_CONFIG_PATH)); 233 InputSource source = new InputSource ( 234 new FileReader (configFile)); 235 parser.parse(source,handler); 236 lastModifiedAtRead = configFile.lastModified(); 237 } catch (Exception ex) { 238 throw new XMLConfigurationException( 239 "Failed load the configuration : " + ex.getMessage(), 240 ex); 241 } 242 } 243 244 245 251 public boolean modified() { 252 if (lastModifiedAtRead != configFile.lastModified()) { 253 return true; 254 } 255 return false; 256 } 257 258 259 265 public XMLConfiguration getConfig(Class classRef) { 266 XMLConfiguration configuration = new XMLConfiguration( 267 classRef.getName()); 268 269 copyConfig((XMLConfigInfo)configSections.get(GENERAL_SECTION), 270 configuration); 271 copyConfig((XMLConfigInfo)configSections.get( 272 classRef.getName()),configuration); 273 274 return configuration; 275 } 276 277 278 284 private void copyConfig(XMLConfigInfo info,XMLConfiguration config) { 285 if (info == null) { 286 return; 287 } 288 Map entries = info.getConfigInfo(); 289 for (Iterator iter = entries.keySet().iterator(); iter.hasNext();) { 290 String key = (String )iter.next(); 291 config.addConfigurationEntry( 292 (XMLConfigurationEntry)entries.get(key)); 293 } 294 } 295 } 296 | Popular Tags |