1 50 51 package org.apache.excalibur.configuration.validation; 52 53 import java.io.InputStream ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 import java.util.Map ; 57 58 import org.apache.avalon.framework.activity.Disposable; 59 import org.apache.avalon.framework.activity.Initializable; 60 import org.apache.avalon.framework.configuration.Configurable; 61 import org.apache.avalon.framework.configuration.Configuration; 62 import org.apache.avalon.framework.configuration.ConfigurationException; 63 import org.apache.avalon.framework.container.ContainerUtil; 64 import org.apache.avalon.framework.logger.AbstractLogEnabled; 65 66 71 public class DelegatingConfigurationValidatorFactory 72 extends AbstractLogEnabled 73 implements Configurable, Initializable, Disposable, ConfigurationValidatorFactory 74 { 75 private Map m_delegates = new HashMap (); 76 private String m_supportedTypes; 77 78 public void configure( Configuration configuration ) 79 throws ConfigurationException 80 { 81 final Configuration[] delegates = configuration.getChildren( "delegate" ); 82 final StringBuffer types = new StringBuffer (); 83 84 for( int i = 0; i < delegates.length; i++ ) 85 { 86 final String type = delegates[ i ].getAttribute( "schema-type" ); 87 final DelegateEntry entry = 88 new DelegateEntry( type, 89 delegates[ i ].getAttribute( "class" ), 90 delegates[ i ] ); 91 m_delegates.put( type, entry ); 92 if( i > 0 ) 93 { 94 types.append( "," ); 95 } 96 97 types.append( type ); 98 } 99 100 this.m_supportedTypes = types.toString(); 101 } 102 103 public void initialize() 104 throws Exception 105 { 106 for( Iterator i = m_delegates.values().iterator(); i.hasNext(); ) 107 { 108 final DelegateEntry entry = (DelegateEntry)i.next(); 109 final Class clazz = Class.forName( entry.getClassName() ); 110 final ConfigurationValidatorFactory validator = 111 (ConfigurationValidatorFactory)clazz.newInstance(); 112 113 ContainerUtil.enableLogging( validator, getLogger() ); 114 ContainerUtil.configure( validator, entry.getConfiguration() ); 115 ContainerUtil.initialize( validator ); 116 117 entry.setValidatorFactory( validator ); 118 } 119 } 120 121 public void dispose() 122 { 123 for( Iterator i = m_delegates.values().iterator(); i.hasNext(); ) 124 { 125 ContainerUtil.dispose( ( (DelegateEntry)i.next() ).getValidatorFactory() ); 126 } 127 } 128 129 public ConfigurationValidator createValidator( String schemaType, InputStream schema ) 130 throws ConfigurationException 131 { 132 final DelegateEntry entry = (DelegateEntry)m_delegates.get( schemaType ); 133 if( entry == null ) 134 { 135 final String msg = "Invalid schema type: " + schemaType + 136 ". Validator only supports: " + m_supportedTypes; 137 throw new ConfigurationException( msg ); 138 } 139 140 return entry.getValidatorFactory().createValidator( schemaType, schema ); 141 } 142 } 143 | Popular Tags |