1 16 17 package org.apache.commons.configuration; 18 19 import java.util.List ; 20 21 import org.apache.commons.configuration.HierarchicalConfiguration.Node; 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.helpers.AttributesImpl ; 24 25 40 public class HierarchicalConfigurationXMLReader extends ConfigurationXMLReader 41 { 42 43 private HierarchicalConfiguration configuration; 44 45 49 public HierarchicalConfigurationXMLReader() 50 { 51 super(); 52 } 53 54 61 public HierarchicalConfigurationXMLReader(HierarchicalConfiguration config) 62 { 63 this(); 64 setConfiguration(config); 65 } 66 67 72 public HierarchicalConfiguration getConfiguration() 73 { 74 return configuration; 75 } 76 77 82 public void setConfiguration(HierarchicalConfiguration config) 83 { 84 configuration = config; 85 } 86 87 92 public Configuration getParsedConfiguration() 93 { 94 return getConfiguration(); 95 } 96 97 100 protected void processKeys() 101 { 102 getConfiguration().getRoot().visit(new SAXVisitor(), null); 103 } 104 105 110 class SAXVisitor extends HierarchicalConfiguration.NodeVisitor 111 { 112 113 private static final String ATTR_TYPE = "CDATA"; 114 115 121 public void visitAfterChildren(Node node, ConfigurationKey key) 122 { 123 if (!isAttributeNode(node)) 124 { 125 fireElementEnd(nodeName(node)); 126 } 127 } 128 129 135 public void visitBeforeChildren(Node node, ConfigurationKey key) 136 { 137 if (!isAttributeNode(node)) 138 { 139 fireElementStart(nodeName(node), fetchAttributes(node)); 140 141 if (node.getValue() != null) 142 { 143 fireCharacters(node.getValue().toString()); 144 } 145 } 146 } 147 148 154 public boolean terminate() 155 { 156 return getException() != null; 157 } 158 159 165 protected Attributes fetchAttributes(Node node) 166 { 167 AttributesImpl attrs = new AttributesImpl (); 168 List children = node.getChildren(); 169 170 for (int i = 0; i < children.size(); i++) 171 { 172 Node child = (Node) children.get(i); 173 if (isAttributeNode(child) && child.getValue() != null) 174 { 175 String attr = ConfigurationKey.attributeName(child.getName()); 176 attrs.addAttribute(NS_URI, attr, attr, ATTR_TYPE, child.getValue().toString()); 177 } 178 } 179 180 return attrs; 181 } 182 183 191 private String nodeName(Node node) 192 { 193 return (node.getName() == null) ? getRootName() : node.getName(); 194 } 195 196 204 private boolean isAttributeNode(Node node) 205 { 206 return ConfigurationKey.isAttributeKey(node.getName()); 207 } 208 } 209 } 210 | Popular Tags |