1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.IOException ; 58 59 import net.myvietnam.mvncore.configuration.HierarchicalConfiguration.*; 60 import org.xml.sax.Attributes ; 61 import org.xml.sax.SAXException ; 62 import org.xml.sax.helpers.AttributesImpl ; 63 64 79 public class HierarchicalConfigurationXMLReader 80 extends ConfigurationXMLReader 81 { 82 83 private HierarchicalConfiguration configuration; 84 85 89 public HierarchicalConfigurationXMLReader() 90 { 91 super(); 92 } 93 94 100 public HierarchicalConfigurationXMLReader( 101 HierarchicalConfiguration config) 102 { 103 this(); 104 setConfiguration(config); 105 } 106 107 111 public HierarchicalConfiguration getConfiguration() 112 { 113 return configuration; 114 } 115 116 120 public void setConfiguration(HierarchicalConfiguration config) 121 { 122 configuration = config; 123 } 124 125 129 public Configuration getParsedConfiguration() 130 { 131 return getConfiguration(); 132 } 133 134 140 protected void processKeys() throws IOException , SAXException 141 { 142 getConfiguration().getRoot().visit(new SAXVisitor(), null); 143 } 144 145 151 class SAXVisitor extends HierarchicalConfiguration.NodeVisitor 152 { 153 154 private static final String ATTR_TYPE = "CDATA"; 155 156 161 public void visitAfterChildren(Node node, ConfigurationKey key) 162 { 163 if(!isAttributeNode(node)) 164 { 165 fireElementEnd(nodeName(node)); 166 } 167 } 168 169 174 public void visitBeforeChildren(Node node, ConfigurationKey key) 175 { 176 if(!isAttributeNode(node)) 177 { 178 fireElementStart(nodeName(node), fetchAttributes(node)); 179 180 if(node.getValue() != null) 181 { 182 fireCharacters(node.getValue().toString()); 183 } 184 } 185 } 186 187 192 public boolean terminate() 193 { 194 return getException() != null; 195 } 196 197 202 protected Attributes fetchAttributes(Node node) 203 { 204 AttributesImpl attrs = new AttributesImpl (); 205 AbstractConfiguration.Container children = node.getChildren(); 206 207 for(int i = 0; i < children.size(); i++) 208 { 209 Node child = (Node) children.get(i); 210 if(isAttributeNode(child) && child.getValue() != null) 211 { 212 String attr = ConfigurationKey.attributeName( 213 child.getName()); 214 attrs.addAttribute(NS_URI, attr, attr, ATTR_TYPE, 215 child.getValue().toString()); 216 } 217 } 218 219 return attrs; 220 } 221 222 229 private String nodeName(Node node) 230 { 231 return (node.getName() == null) ? getRootName() : node.getName(); 232 } 233 234 241 private boolean isAttributeNode(Node node) 242 { 243 return ConfigurationKey.isAttributeKey(node.getName()); 244 } 245 } 246 } 247 | Popular Tags |