1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.IOException ; 58 import java.io.Writer ; 59 import java.util.NoSuchElementException ; 60 61 import org.apache.commons.digester.Digester; 62 import org.dom4j.Document; 63 import org.dom4j.DocumentException; 64 import org.dom4j.io.DOMWriter; 65 import org.dom4j.io.OutputFormat; 66 import org.dom4j.io.SAXReader; 67 import org.dom4j.io.XMLWriter; 68 import org.xml.sax.SAXException ; 69 70 82 public class ConfigurationXMLDocument 83 { 84 85 protected static final String ELEM_CLASS = "config/class"; 86 87 88 protected static final String ELEM_PROPERTY = "config/class/property"; 89 90 91 protected static final String ATTR_NAME = "name"; 92 93 94 protected static final String ATTR_VALUE = "value"; 95 96 97 private Configuration configuration; 98 99 104 public ConfigurationXMLDocument(Configuration config) 105 { 106 setConfiguration(config); 107 } 108 109 113 public Configuration getConfiguration() 114 { 115 return configuration; 116 } 117 118 122 public void setConfiguration(Configuration configuration) 123 { 124 this.configuration = configuration; 125 } 126 127 134 public static ConfigurationXMLReader createXMLReader(Configuration config) 135 { 136 if (config instanceof HierarchicalConfiguration) 137 { 138 return new HierarchicalConfigurationXMLReader( 139 (HierarchicalConfiguration) config); 140 } 141 else 142 { 143 return new BaseConfigurationXMLReader(config); 144 } 145 } 146 147 152 public ConfigurationXMLReader createXMLReader() 153 { 154 return createXMLReader((String ) null); 155 } 156 157 166 public ConfigurationXMLReader createXMLReader(String prefix) 167 { 168 return createXMLReader(configForKey(prefix)); 169 } 170 171 180 public Document getDocument(String prefix, String rootName) 181 throws DocumentException 182 { 183 ConfigurationXMLReader xmlReader = createXMLReader(prefix); 184 if (rootName != null) 185 { 186 xmlReader.setRootName(rootName); 187 } 188 189 SAXReader reader = new SAXReader(xmlReader); 190 return reader.read(getClass().getName()); 191 } 192 193 201 public Document getDocument(String prefix) throws DocumentException 202 { 203 return getDocument(prefix, null); 204 } 205 206 212 public Document getDocument() throws DocumentException 213 { 214 return getDocument(null, null); 215 } 216 217 226 public org.w3c.dom.Document getW3cDocument(String prefix, String rootName) 227 throws DocumentException 228 { 229 return toW3cDocument(getDocument(prefix, rootName)); 230 } 231 232 240 public org.w3c.dom.Document getW3cDocument(String prefix) 241 throws DocumentException 242 { 243 return getW3cDocument(prefix, null); 244 } 245 246 252 public org.w3c.dom.Document getW3cDocument() throws DocumentException 253 { 254 return getW3cDocument(null, null); 255 } 256 257 263 static org.w3c.dom.Document toW3cDocument(Document doc) 264 throws DocumentException 265 { 266 return new DOMWriter().write(doc); 267 } 268 269 276 private Configuration configForKey(String key) 277 { 278 Configuration conf = (key == null) 279 ? getConfiguration() 280 : getConfiguration().subset(key); 281 282 if(conf == null || (conf instanceof CompositeConfiguration 284 && ((CompositeConfiguration) conf).getNumberOfConfigurations() < 2)) 285 { 286 throw new NoSuchElementException ("No subset with key " + key); 287 } 288 289 return conf; 290 } 291 292 319 public Object callDigester(String prefix) throws IOException , SAXException 320 { 321 Digester digester = getDefaultDigester(prefix); 322 return digester.parse(getClass().getName()); 323 } 324 325 332 protected Digester getDefaultDigester(String prefix) 333 { 334 Digester digester = createDefaultDigester(prefix); 335 setupDefaultDigester(digester); 336 337 return digester; 338 } 339 340 347 protected Digester createDefaultDigester(String prefix) 348 { 349 return new Digester(createXMLReader(prefix)); 350 } 351 352 359 protected void setupDefaultDigester(Digester digester) 360 { 361 digester.addObjectCreate(ELEM_CLASS, ATTR_NAME, Object .class); 362 digester.addSetProperty(ELEM_PROPERTY, ATTR_NAME, ATTR_VALUE); 363 } 364 365 376 public void write(Writer out, String prefix, String root, boolean pretty) 377 throws IOException , DocumentException 378 { 379 OutputFormat format = 380 (pretty) 381 ? OutputFormat.createPrettyPrint() 382 : OutputFormat.createCompactFormat(); 383 384 XMLWriter writer = new XMLWriter(out, format); 385 writer.write(getDocument(prefix, root)); 386 } 387 388 399 public void write(Writer out, String prefix, String root) 400 throws IOException , DocumentException 401 { 402 write(out, prefix, root, true); 403 } 404 405 415 public void write(Writer out, String prefix, boolean pretty) 416 throws IOException , DocumentException 417 { 418 write(out, prefix, null, pretty); 419 } 420 421 431 public void write(Writer out, String prefix) 432 throws IOException , DocumentException 433 { 434 write(out, prefix, true); 435 } 436 437 445 public void write(Writer out, boolean pretty) 446 throws IOException , DocumentException 447 { 448 write(out, null, null, pretty); 449 } 450 451 459 public void write(Writer out) throws IOException , DocumentException 460 { 461 write(out, true); 462 } 463 } 464 | Popular Tags |