1 8 package org.codehaus.loom.classman.reader; 9 10 import java.util.ArrayList ; 11 12 import org.codehaus.loom.classman.metadata.ClassLoaderMetaData; 13 import org.codehaus.loom.classman.metadata.ClassLoaderSetMetaData; 14 import org.codehaus.loom.classman.metadata.FileSetMetaData; 15 import org.codehaus.loom.classman.metadata.JoinMetaData; 16 import org.codehaus.loom.extension.Extension; 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.NodeList ; 19 20 27 public class ClassLoaderSetReader 28 { 29 36 public ClassLoaderSetMetaData build( final Element config ) 37 throws Exception 38 { 39 final String defaultClassLoader = 40 config.getAttribute( "default" ); 41 if( isUnspecified( defaultClassLoader ) ) 42 { 43 final String message = "Default classloader not specified."; 44 throw new Exception ( message ); 45 } 46 47 final String version = 48 config.getAttribute( "version" ); 49 if( !"1.0".equals( version ) ) 50 { 51 final String message = "Bad version:" + version; 52 throw new Exception ( message ); 53 } 54 55 final NodeList joinConfigs = 56 config.getElementsByTagName( "join" ); 57 final JoinMetaData[] joins = buildJoins( joinConfigs ); 58 59 final NodeList clConfigs = 60 config.getElementsByTagName( "classloader" ); 61 62 final ClassLoaderMetaData[] classloaders = 63 buildClassLoaders( clConfigs ); 64 65 final NodeList predefinedConfigs = 66 config.getElementsByTagName( "predefined" ); 67 68 final String [] predefined = 69 buildPredefined( predefinedConfigs ); 70 71 return new ClassLoaderSetMetaData( defaultClassLoader, 72 predefined, 73 classloaders, 74 joins ); 75 } 76 77 83 private String [] buildPredefined( final NodeList configs ) 84 { 85 final ArrayList predefines = new ArrayList (); 86 87 final int length = configs.getLength(); 88 for( int i = 0; i < length; i++ ) 89 { 90 final Element element = (Element )configs.item( i ); 91 final String predefined = element.getAttribute( "name" ); 92 predefines.add( predefined ); 93 } 94 95 return (String [])predefines.toArray( new String [ predefines.size() ] ); 96 } 97 98 104 private ClassLoaderMetaData[] buildClassLoaders( final NodeList configs ) 105 throws Exception 106 { 107 final ArrayList loaders = new ArrayList (); 108 final int length = configs.getLength(); 109 110 for( int i = 0; i < length; i++ ) 111 { 112 final Element item = (Element )configs.item( i ); 113 final ClassLoaderMetaData loader = buildLoader( item ); 114 loaders.add( loader ); 115 } 116 117 return (ClassLoaderMetaData[])loaders.toArray( 118 new ClassLoaderMetaData[ loaders.size() ] ); 119 } 120 121 127 private ClassLoaderMetaData buildLoader( final Element config ) 128 throws Exception 129 { 130 final String name = config.getAttribute( "name" ); 131 final String parent = config.getAttribute( "parent" ); 132 133 final String [] entrys = 134 buildEntrys( config.getElementsByTagName( "entry" ) ); 135 final Extension[] extensions = 136 buildExtensions( config.getElementsByTagName( "extension" ) ); 137 final FileSetMetaData[] fileSets = 138 buildFileSets( config.getElementsByTagName( "fileset" ) ); 139 return new ClassLoaderMetaData( name, parent, entrys, 140 extensions, fileSets ); 141 } 142 143 149 private Extension[] buildExtensions( final NodeList configs ) 150 throws Exception 151 { 152 final ArrayList extensions = new ArrayList (); 153 154 final int length = configs.getLength(); 155 for( int i = 0; i < length; i++ ) 156 { 157 final Extension extension = 158 buildExtension( (Element )configs.item( i ) ); 159 extensions.add( extension ); 160 } 161 162 return (Extension[])extensions.toArray( 163 new Extension[ extensions.size() ] ); 164 } 165 166 172 private Extension buildExtension( final Element config ) 173 throws Exception 174 { 175 final String name = config.getAttribute( "name" ); 176 if( isUnspecified( name ) ) 177 { 178 final String message = "Missing name from extension"; 179 throw new Exception ( message ); 180 } 181 final String specVersion = 182 config.getAttribute( "specification-version" ); 183 final String specVendor = 184 config.getAttribute( "specification-vendor" ); 185 final String implVersion = 186 config.getAttribute( "implementation-version" ); 187 final String implVendor = 188 config.getAttribute( "implementation-vendor" ); 189 final String implVendorID = 190 config.getAttribute( "implementation-vendor-id" ); 191 final String implURL = 192 config.getAttribute( "implementation-url" ); 193 194 return new Extension( name, specVersion, specVendor, 195 implVersion, implVendor, implVendorID, 196 implURL ); 197 } 198 199 205 private FileSetMetaData[] buildFileSets( final NodeList configs ) 206 { 207 final ArrayList fileSets = new ArrayList (); 208 209 final int length = configs.getLength(); 210 for( int i = 0; i < length; i++ ) 211 { 212 final FileSetMetaData fileSet = 213 buildFileSet( (Element )configs.item( i ) ); 214 fileSets.add( fileSet ); 215 } 216 217 return (FileSetMetaData[])fileSets.toArray( 218 new FileSetMetaData[ fileSets.size() ] ); 219 } 220 221 227 private FileSetMetaData buildFileSet( final Element config ) 228 { 229 final String dir = config.getAttribute( "dir" ); 230 final String [] includes = 231 buildSelectors( config.getElementsByTagName( "include" ) ); 232 final String [] excludes = 233 buildSelectors( config.getElementsByTagName( "exclude" ) ); 234 return new FileSetMetaData( dir, includes, excludes ); 235 } 236 237 243 private String [] buildSelectors( final NodeList configs ) 244 { 245 final ArrayList selectors = new ArrayList (); 246 247 final int length = configs.getLength(); 248 for( int i = 0; i < length; i++ ) 249 { 250 final Element element = (Element )configs.item( i ); 251 final String name = element.getAttribute( "name" ); 252 selectors.add( name ); 253 } 254 255 return (String [])selectors.toArray( new String [ selectors.size() ] ); 256 } 257 258 264 private String [] buildEntrys( final NodeList configs ) 265 { 266 final ArrayList entrys = new ArrayList (); 267 268 final int length = configs.getLength(); 269 for( int i = 0; i < length; i++ ) 270 { 271 final Element config = (Element )configs.item( i ); 272 final String entry = config.getAttribute( "location" ); 273 entrys.add( entry ); 274 } 275 276 return (String [])entrys.toArray( new String [ entrys.size() ] ); 277 } 278 279 285 private JoinMetaData[] buildJoins( final NodeList configs ) 286 { 287 final ArrayList joins = new ArrayList (); 288 289 final int length = configs.getLength(); 290 for( int i = 0; i < length; i++ ) 291 { 292 final Element config = (Element )configs.item( i ); 293 final JoinMetaData join = buildJoin( config ); 294 joins.add( join ); 295 } 296 297 return (JoinMetaData[])joins.toArray( new JoinMetaData[ joins.size() ] ); 298 } 299 300 306 private JoinMetaData buildJoin( final Element config ) 307 { 308 final String name = config.getAttribute( "name" ); 309 final NodeList children = 310 config.getElementsByTagName( "classloader-ref" ); 311 final String [] classloaders = 312 buildClassLoaderRefs( children ); 313 return new JoinMetaData( name, classloaders ); 314 } 315 316 323 private String [] buildClassLoaderRefs( final NodeList configs ) 324 { 325 final ArrayList refs = new ArrayList (); 326 327 final int length = configs.getLength(); 328 for( int i = 0; i < length; i++ ) 329 { 330 final Element element = (Element )configs.item( i ); 331 final String ref = element.getAttribute( "name" ); 332 refs.add( ref ); 333 } 334 335 return (String [])refs.toArray( new String [ refs.size() ] ); 336 } 337 338 345 private boolean isUnspecified( final String attribute ) 346 { 347 return "".equals( attribute ); 348 } 349 } | Popular Tags |