1 16 package org.apache.commons.betwixt.io; 17 18 import java.beans.IntrospectionException ; 19 import java.util.HashSet ; 20 import java.util.Set ; 21 22 import javax.xml.parsers.SAXParser ; 23 24 import org.apache.commons.betwixt.BindingConfiguration; 25 import org.apache.commons.betwixt.ElementDescriptor; 26 import org.apache.commons.betwixt.XMLBeanInfo; 27 import org.apache.commons.betwixt.XMLIntrospector; 28 import org.apache.commons.betwixt.io.read.ReadConfiguration; 29 import org.apache.commons.betwixt.io.read.ReadContext; 30 import org.apache.commons.digester.Digester; 31 import org.apache.commons.digester.ExtendedBaseRules; 32 import org.apache.commons.digester.RuleSet; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.xml.sax.XMLReader ; 36 37 44 public class BeanReader extends Digester { 45 46 47 private XMLIntrospector introspector = new XMLIntrospector(); 48 49 private Log log = LogFactory.getLog( BeanReader.class ); 50 51 private Set registeredClasses = new HashSet (); 52 53 private BindingConfiguration bindingConfiguration = new BindingConfiguration(); 54 55 private ReadConfiguration readConfiguration = new ReadConfiguration(); 56 57 60 public BeanReader() { 61 setRules(new ExtendedBaseRules()); 63 } 64 65 74 public BeanReader(SAXParser parser) { 75 super(parser); 76 setRules(new ExtendedBaseRules()); 77 } 78 79 88 public BeanReader(XMLReader reader) { 89 super(reader); 90 setRules(new ExtendedBaseRules()); 91 } 92 93 94 116 public void registerBeanClass(Class beanClass) throws IntrospectionException { 117 if ( ! registeredClasses.contains( beanClass ) ) { 118 if ( log.isTraceEnabled() ) { 119 log.trace( "Registering class " + beanClass ); 120 } 121 registeredClasses.add( beanClass ); 122 123 XMLBeanInfo xmlInfo = introspector.introspect( beanClass ); 125 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); 126 127 String path = elementDescriptor.getQualifiedName(); 128 if (log.isTraceEnabled()) { 129 log.trace("Added path: " + path + ", mapped to: " + beanClass.getName()); 130 } 131 addBeanCreateRule( path, elementDescriptor, beanClass ); 132 133 } else { 134 if ( log.isWarnEnabled() ) { 135 log.warn("Cannot add class " + beanClass.getName() + " since it already exists"); 136 } 137 } 138 } 139 140 166 public void registerBeanClass(String path, Class beanClass) throws IntrospectionException { 167 if ( ! registeredClasses.contains( beanClass ) ) { 168 registeredClasses.add( beanClass ); 169 170 XMLBeanInfo xmlInfo = introspector.introspect( beanClass ); 172 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); 173 174 addBeanCreateRule( path, elementDescriptor, beanClass ); 175 } else { 176 if ( log.isWarnEnabled() ) { 177 log.warn("Cannot add class " + beanClass.getName() + " since it already exists"); 178 } 179 } 180 } 181 182 191 public void flushRegisteredBeanClasses() { 192 registeredClasses.clear(); 193 } 194 195 208 public void deregisterBeanClass( Class beanClass ) { 209 registeredClasses.remove( beanClass ); 210 } 211 212 215 225 public XMLIntrospector getXMLIntrospector() { 226 return introspector; 227 } 228 229 230 240 public void setXMLIntrospector(XMLIntrospector introspector) { 241 this.introspector = introspector; 242 } 243 244 249 public Log getLog() { 250 return log; 251 } 252 253 258 public void setLog(Log log) { 259 this.log = log; 260 setLogger(log); 261 } 262 263 270 public boolean getMatchIDs() { 271 return getBindingConfiguration().getMapIDs(); 272 } 273 274 280 public void setMatchIDs(boolean matchIDs) { 281 getBindingConfiguration().setMapIDs( matchIDs ); 282 } 283 284 289 public BindingConfiguration getBindingConfiguration() { 290 return bindingConfiguration; 291 } 292 293 298 public void setBindingConfiguration( BindingConfiguration bindingConfiguration ) { 299 this.bindingConfiguration = bindingConfiguration; 300 } 301 302 307 public ReadConfiguration getReadConfiguration() { 308 return readConfiguration; 309 } 310 311 316 public void setReadConfiguration( ReadConfiguration readConfiguration ) { 317 this.readConfiguration = readConfiguration; 318 } 319 320 323 330 protected void addBeanCreateRule( 331 String path, 332 ElementDescriptor elementDescriptor, 333 Class beanClass ) { 334 if (log.isTraceEnabled()) { 335 log.trace("Adding BeanRuleSet for " + beanClass); 336 } 337 RuleSet ruleSet = new BeanRuleSet( 338 introspector, 339 path , 340 elementDescriptor, 341 beanClass, 342 makeContext()); 343 addRuleSet( ruleSet ); 344 } 345 346 351 private ReadContext makeContext() { 352 return new ReadContext( log, bindingConfiguration, readConfiguration ); 353 } 354 } 355 | Popular Tags |