1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.net.URL ; 58 import java.util.Iterator ; 59 60 import org.dom4j.Attribute; 61 import org.dom4j.Document; 62 import org.dom4j.Element; 63 import org.dom4j.io.SAXReader; 64 65 75 public class HierarchicalDOM4JConfiguration 76 extends HierarchicalConfiguration 77 implements BasePathLoader 78 { 79 80 private String file; 81 82 83 private String basePath; 84 85 88 public HierarchicalDOM4JConfiguration() 89 { 90 super(); 91 } 92 93 98 public HierarchicalDOM4JConfiguration(Configuration defaults) 99 { 100 super(defaults); 101 } 102 103 107 public String getFileName() 108 { 109 return file; 110 } 111 112 116 public void setFileName(String file) 117 { 118 this.file = file; 119 } 120 121 125 public String getBasePath() 126 { 127 return basePath; 128 } 129 130 135 public void setBasePath(String path) 136 { 137 basePath = path; 138 } 139 140 145 public void load() throws Exception 146 { 147 load(ConfigurationUtils.getURL(getBasePath(), getFileName())); 148 } 149 150 155 public void load(URL url) throws Exception 156 { 157 initProperties(new SAXReader().read(url)); 158 } 159 160 164 public void initProperties(Document document) 165 { 166 constructHierarchy(getRoot(), document.getRootElement()); 167 } 168 169 175 private void constructHierarchy(Node node, Element element) 176 { 177 if (element.getTextTrim().length() > 0) 178 { 179 node.setValue(element.getTextTrim()); 180 } 181 processAttributes(node, element); 182 183 for (Iterator it = element.elementIterator(); it.hasNext();) 184 { 185 Element child = (Element) it.next(); 186 Node childNode = new Node(child.getName()); 187 constructHierarchy(childNode, child); 188 node.addChild(childNode); 189 } 190 } 191 192 198 private void processAttributes(Node node, Element element) 199 { 200 for (Iterator it = element.attributeIterator(); it.hasNext();) 201 { 202 Attribute attr = (Attribute) it.next(); 203 Node child = 204 new Node( 205 ConfigurationKey.constructAttributeKey(attr.getName())); 206 child.setValue(attr.getValue()); 207 node.addChild(child); 208 } 209 } 210 } 211 | Popular Tags |