1 50 51 package org.apache.avalon.meta.info.builder; 52 53 import java.io.InputStream ; 54 import java.util.Properties ; 55 import java.util.ArrayList ; 56 import org.apache.avalon.excalibur.i18n.ResourceManager; 57 import org.apache.avalon.excalibur.i18n.Resources; 58 import org.apache.avalon.framework.Version; 59 import org.apache.avalon.framework.configuration.Configuration; 60 import org.apache.avalon.framework.configuration.ConfigurationException; 61 import org.apache.avalon.meta.ConfigurationBuilder; 62 import org.apache.avalon.meta.info.Service; 63 import org.apache.avalon.meta.info.EntryDescriptor; 64 import org.apache.avalon.meta.info.ReferenceDescriptor; 65 import org.apache.avalon.meta.info.builder.BuildException; 66 import org.apache.excalibur.configuration.ConfigurationUtil; 67 import org.xml.sax.InputSource ; 68 69 76 public class XMLServiceCreator 77 implements ServiceCreator 78 { 79 private static final Resources REZ = 80 ResourceManager.getPackageResources( XMLServiceCreator.class ); 81 82 90 public Service createService( String key, InputStream input ) 91 throws Exception 92 { 93 if( input == null ) 94 { 95 throw new NullPointerException ( "input" ); 96 } 97 98 final InputSource source = new InputSource ( input ); 99 final Configuration xservice = ConfigurationBuilder.build( source ); 100 return build( key, xservice ); 101 } 102 103 112 private Service build( final String classname, final Configuration info ) 113 throws BuildException 114 { 115 final String topLevelName = info.getName(); 116 if( !topLevelName.equals( "service" ) ) 117 { 118 final String message = 119 REZ.getString( "builder.bad-toplevel-service-element.error", 120 classname, 121 topLevelName ); 122 throw new BuildException( message ); 123 } 124 125 final Properties attributes = 126 buildAttributes( info.getChild( "attributes" ) ); 127 final EntryDescriptor[] entries = 128 buildEntries( info.getChild( "entries" ).getChildren("entry") ); 129 final String versionString = info.getChild( "version" ).getValue( "1.0" ); 130 final Version version = buildVersion( versionString ); 131 132 return new Service( new ReferenceDescriptor( classname, version ), entries, attributes ); 133 } 134 135 142 public Properties buildAttributes( final Configuration config ) 143 throws BuildException 144 { 145 final Properties attributes = new Properties (); 146 final Configuration[] children = config.getChildren( "attribute" ); 147 for( int i = 0; i < children.length; i++ ) 148 { 149 Configuration child = children[ i ]; 150 String key = null; 151 try 152 { 153 key = child.getAttribute( "key" ); 154 } 155 catch( ConfigurationException ce ) 156 { 157 final String error = 158 "Missing 'key' attribute in 'attribute' element.\n" 159 + ConfigurationUtil.list( child ); 160 throw new BuildException( error, ce ); 161 } 162 163 String value = null; 164 try 165 { 166 value = child.getAttribute( "value" ); 167 } 168 catch( Throwable e ) 169 { 170 value = child.getValue( "" ); 171 } 172 attributes.setProperty( key, value ); 173 } 174 return attributes; 175 } 176 177 185 protected EntryDescriptor[] buildEntries( final Configuration[] entrySet ) 186 throws BuildException 187 { 188 final ArrayList entrys = new ArrayList (); 189 190 for( int i = 0; i < entrySet.length; i++ ) 191 { 192 final EntryDescriptor service = buildEntry( entrySet[ i ] ); 193 entrys.add( service ); 194 } 195 196 return (EntryDescriptor[])entrys.toArray( new EntryDescriptor[ entrys.size() ] ); 197 } 198 199 206 protected EntryDescriptor buildEntry( final Configuration config ) 207 throws BuildException 208 { 209 try 210 { 211 final String key = config.getAttribute( "key" ); 212 final String type = config.getAttribute( "type", "java.lang.String" ); 213 final boolean isVolatile = config.getAttributeAsBoolean( "volatile", false ); 214 final boolean optional = 215 config.getAttributeAsBoolean( "optional", false ); 216 final String alias = config.getAttribute( "alias", null ); 217 return new EntryDescriptor( key, type, optional, isVolatile, alias ); 218 } 219 catch( Throwable e ) 220 { 221 final String error = 222 "Unable to build entry descriptor." 223 + ConfigurationUtil.list( config ); 224 throw new BuildException( error, e ); 225 } 226 } 227 228 234 protected Version buildVersion( final String version ) 235 throws BuildException 236 { 237 try 238 { 239 return Version.getVersion( version ); 240 } 241 catch( Throwable e ) 242 { 243 final String error = 244 REZ.getString( "builder.bad-version", version ); 245 throw new BuildException( error, e ); 246 } 247 } 248 } 249 | Popular Tags |