1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.BufferedOutputStream ; 58 import java.io.File ; 59 import java.io.FileOutputStream ; 60 import java.io.IOException ; 61 import java.io.OutputStream ; 62 import java.net.URL ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 66 import org.dom4j.Attribute; 67 import org.dom4j.Document; 68 import org.dom4j.Element; 69 import org.dom4j.io.OutputFormat; 70 import org.dom4j.io.SAXReader; 71 import org.dom4j.io.XMLWriter; 72 73 import org.apache.commons.lang.StringUtils; 74 import org.apache.commons.lang.exception.NestableRuntimeException; 75 76 90 public class DOM4JConfiguration extends XMLConfiguration 91 { 92 private static final char ATTRIB_MARKER = '@'; 94 private static final String ATTRIB_START_MARKER = "[" + ATTRIB_MARKER; 95 96 100 private static final String NODE_DELIMITER = "."; 101 102 105 private String fileName; 106 107 110 private Document document; 111 112 115 private boolean autoSave = false; 116 117 122 public DOM4JConfiguration() 123 { 124 } 125 126 135 public DOM4JConfiguration(String resource) throws Exception 136 { 137 setFile(resourceURLToFile(resource)); 138 load(); 139 } 140 141 147 public DOM4JConfiguration(File file) throws Exception 148 { 149 setFile(file); 150 load(); 151 } 152 153 public void load() throws Exception 154 { 155 156 document = new SAXReader().read( 157 ConfigurationUtils.getURL(getBasePath(), getFileName())); 158 initProperties(document.getRootElement(), new StringBuffer ()); 159 160 } 161 162 private static File resourceURLToFile(String resource) 163 { 164 URL confURL = DOM4JConfiguration.class.getClassLoader().getResource(resource); 165 if (confURL == null) 166 { 167 confURL = ClassLoader.getSystemResource(resource); 168 } 169 return new File (confURL.getFile()); 170 } 171 172 179 private void initProperties(Element element, StringBuffer hierarchy) 180 { 181 for (Iterator it = element.elementIterator(); it.hasNext();) 182 { 183 StringBuffer subhierarchy = new StringBuffer (hierarchy.toString()); 184 Element child = (Element) it.next(); 185 String nodeName = child.getName(); 186 String nodeValue = child.getTextTrim(); 187 subhierarchy.append(nodeName); 188 if (nodeValue.length() > 0) 189 { 190 super.addProperty(subhierarchy.toString(), nodeValue); 191 } 192 193 List attributes = child.attributes(); 195 for (int j = 0, k = attributes.size(); j < k; j++) 196 { 197 Attribute a = (Attribute) attributes.get(j); 198 String attName = subhierarchy.toString() + '[' + ATTRIB_MARKER + a.getName() + ']'; 199 String attValue = a.getValue(); 200 super.addProperty(attName, attValue); 201 } 202 StringBuffer buf = new StringBuffer (subhierarchy.toString()); 203 initProperties(child, buf.append('.')); 204 } 205 } 206 207 214 public void addProperty(String name, Object value) 215 { 216 super.addProperty(name, value); 217 setXmlProperty(name, value); 218 possiblySave(); 219 } 220 221 228 public void setProperty(String name, Object value) 229 { 230 super.setProperty(name, value); 231 setXmlProperty(name, value); 232 possiblySave(); 233 } 234 235 242 private void setXmlProperty(String name, Object value) 243 { 244 String [] nodes = StringUtils.split(name, NODE_DELIMITER); 245 String attName = null; 246 Element element = document.getRootElement(); 247 for (int i = 0; i < nodes.length; i++) 248 { 249 String eName = nodes[i]; 250 int index = eName.indexOf(ATTRIB_START_MARKER); 251 if (index > -1) 252 { 253 attName = eName.substring(index + ATTRIB_START_MARKER.length(), eName.length() - 1); 254 eName = eName.substring(0, index); 255 } 256 if (element.element(eName) == null && attName == null) 259 { 260 element.addElement(eName); 261 } 262 element = element.element(eName); 263 } 264 265 if (attName == null) 266 { 267 element.setText((String ) value); 268 } 269 else 270 { 271 element.addAttribute(attName, (String ) value); 272 } 273 } 274 275 281 public void clearProperty(String name) 282 { 283 super.clearProperty(name); 284 clearXmlProperty(name); 285 possiblySave(); 286 } 287 288 private void clearXmlProperty(String name) 289 { 290 String [] nodes = StringUtils.split(name, NODE_DELIMITER); 291 String attName = null; 292 Element element = document.getRootElement(); 293 for (int i = 0; i < nodes.length; i++) 294 { 295 String eName = nodes[i]; 296 int index = eName.indexOf(ATTRIB_START_MARKER); 297 if (index > -1) 298 { 299 attName = eName.substring(index + ATTRIB_START_MARKER.length(), eName.length() - 1); 300 eName = eName.substring(0, index); 301 } 302 element = element.element(eName); 303 if (element == null) 304 { 305 return; 306 } 307 } 308 309 if (attName == null) 310 { 311 element.remove(element.element(nodes[nodes.length - 1])); 312 } 313 else 314 { 315 element.remove(element.attribute(attName)); 316 } 317 } 318 319 321 private void possiblySave() 322 { 323 if (autoSave) 324 { 325 try 326 { 327 save(); 328 } 329 catch (IOException e) 330 { 331 throw new NestableRuntimeException("Failed to auto-save", e); 332 } 333 } 334 } 335 336 340 public void setAutoSave(boolean autoSave) 341 { 342 this.autoSave = autoSave; 343 } 344 345 public synchronized void save() throws IOException 346 { 347 XMLWriter writer = null; 348 OutputStream out = null; 349 try 350 { 351 OutputFormat outputter = OutputFormat.createPrettyPrint(); 352 out = new BufferedOutputStream (new FileOutputStream (getFile())); 353 writer = new XMLWriter(out, outputter); 354 writer.write(document); 355 } 356 finally 357 { 358 if (out != null) 359 { 360 out.close(); 361 } 362 363 if (writer != null) 364 { 365 writer.close(); 366 } 367 } 368 } 369 373 public File getFile() 374 { 375 return ConfigurationUtils.constructFile(getBasePath(), getFileName()); 376 } 377 378 382 public void setFile(File file) 383 { 384 this.fileName = file.getAbsolutePath(); 385 } 386 387 public void setFileName(String fileName) 388 { 389 390 this.fileName = fileName; 391 392 } 393 394 398 public String getFileName() 399 { 400 return fileName; 401 } 402 } 403 | Popular Tags |