KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > classman > reader > ClassLoaderSetReader


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.classman.reader;
9
10 import java.util.ArrayList JavaDoc;
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 JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19
20 /**
21  * This class builds a {@link ClassLoaderSetMetaData} object from specified
22  * configuration.
23  *
24  * @author Peter Donald
25  * @version $Revision: 1.1 $ $Date: 2004/04/19 22:19:25 $
26  */

27 public class ClassLoaderSetReader
28 {
29     /**
30      * Build ClassLoader MetaData from a DOM tree.
31      *
32      * @param config the root element
33      * @return the meta data
34      * @throws Exception if malformed DOM
35      */

36     public ClassLoaderSetMetaData build( final Element JavaDoc config )
37         throws Exception JavaDoc
38     {
39         final String JavaDoc defaultClassLoader =
40             config.getAttribute( "default" );
41         if( isUnspecified( defaultClassLoader ) )
42         {
43             final String JavaDoc message = "Default classloader not specified.";
44             throw new Exception JavaDoc( message );
45         }
46
47         final String JavaDoc version =
48             config.getAttribute( "version" );
49         if( !"1.0".equals( version ) )
50         {
51             final String JavaDoc message = "Bad version:" + version;
52             throw new Exception JavaDoc( message );
53         }
54
55         final NodeList JavaDoc joinConfigs =
56             config.getElementsByTagName( "join" );
57         final JoinMetaData[] joins = buildJoins( joinConfigs );
58
59         final NodeList JavaDoc clConfigs =
60             config.getElementsByTagName( "classloader" );
61
62         final ClassLoaderMetaData[] classloaders =
63             buildClassLoaders( clConfigs );
64
65         final NodeList JavaDoc predefinedConfigs =
66             config.getElementsByTagName( "predefined" );
67
68         final String JavaDoc[] predefined =
69             buildPredefined( predefinedConfigs );
70
71         return new ClassLoaderSetMetaData( defaultClassLoader,
72                                            predefined,
73                                            classloaders,
74                                            joins );
75     }
76
77     /**
78      * Parse out a set of predefined classloaders from specified nodes.
79      *
80      * @param configs the nodes to process
81      * @return the predefined classloaders
82      */

83     private String JavaDoc[] buildPredefined( final NodeList JavaDoc configs )
84     {
85         final ArrayList JavaDoc predefines = new ArrayList JavaDoc();
86
87         final int length = configs.getLength();
88         for( int i = 0; i < length; i++ )
89         {
90             final Element JavaDoc element = (Element JavaDoc)configs.item( i );
91             final String JavaDoc predefined = element.getAttribute( "name" );
92             predefines.add( predefined );
93         }
94
95         return (String JavaDoc[])predefines.toArray( new String JavaDoc[ predefines.size() ] );
96     }
97
98     /**
99      * Build an array of ClassLoader meta datas from node list.
100      *
101      * @param configs the nodes to process
102      * @return the classloaders
103      */

104     private ClassLoaderMetaData[] buildClassLoaders( final NodeList JavaDoc configs )
105         throws Exception JavaDoc
106     {
107         final ArrayList JavaDoc loaders = new ArrayList JavaDoc();
108         final int length = configs.getLength();
109
110         for( int i = 0; i < length; i++ )
111         {
112             final Element JavaDoc item = (Element JavaDoc)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     /**
122      * Build a ClassLoader meta datas from element.
123      *
124      * @param config the nodes to process
125      * @return the classloader
126      */

127     private ClassLoaderMetaData buildLoader( final Element JavaDoc config )
128         throws Exception JavaDoc
129     {
130         final String JavaDoc name = config.getAttribute( "name" );
131         final String JavaDoc parent = config.getAttribute( "parent" );
132
133         final String JavaDoc[] 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     /**
144      * Build and array of extensions
145      *
146      * @param configs the nodes to process
147      * @return an array of extensions
148      */

149     private Extension[] buildExtensions( final NodeList JavaDoc configs )
150         throws Exception JavaDoc
151     {
152         final ArrayList JavaDoc extensions = new ArrayList JavaDoc();
153
154         final int length = configs.getLength();
155         for( int i = 0; i < length; i++ )
156         {
157             final Extension extension =
158                 buildExtension( (Element JavaDoc)configs.item( i ) );
159             extensions.add( extension );
160         }
161
162         return (Extension[])extensions.toArray(
163             new Extension[ extensions.size() ] );
164     }
165
166     /**
167      * Build an extension from a DOM element.
168      *
169      * @param config the node to process
170      * @return an extension
171      */

172     private Extension buildExtension( final Element JavaDoc config )
173         throws Exception JavaDoc
174     {
175         final String JavaDoc name = config.getAttribute( "name" );
176         if( isUnspecified( name ) )
177         {
178             final String JavaDoc message = "Missing name from extension";
179             throw new Exception JavaDoc( message );
180         }
181         final String JavaDoc specVersion =
182             config.getAttribute( "specification-version" );
183         final String JavaDoc specVendor =
184             config.getAttribute( "specification-vendor" );
185         final String JavaDoc implVersion =
186             config.getAttribute( "implementation-version" );
187         final String JavaDoc implVendor =
188             config.getAttribute( "implementation-vendor" );
189         final String JavaDoc implVendorID =
190             config.getAttribute( "implementation-vendor-id" );
191         final String JavaDoc implURL =
192             config.getAttribute( "implementation-url" );
193
194         return new Extension( name, specVersion, specVendor,
195                               implVersion, implVendor, implVendorID,
196                               implURL );
197     }
198
199     /**
200      * Build and array of file sets.
201      *
202      * @param configs the nodes to process
203      * @return an array of file sets
204      */

205     private FileSetMetaData[] buildFileSets( final NodeList JavaDoc configs )
206     {
207         final ArrayList JavaDoc fileSets = new ArrayList JavaDoc();
208
209         final int length = configs.getLength();
210         for( int i = 0; i < length; i++ )
211         {
212             final FileSetMetaData fileSet =
213                 buildFileSet( (Element JavaDoc)configs.item( i ) );
214             fileSets.add( fileSet );
215         }
216
217         return (FileSetMetaData[])fileSets.toArray(
218             new FileSetMetaData[ fileSets.size() ] );
219     }
220
221     /**
222      * Build a fileset from a DOM element.
223      *
224      * @param config the node to process
225      * @return a fileset
226      */

227     private FileSetMetaData buildFileSet( final Element JavaDoc config )
228     {
229         final String JavaDoc dir = config.getAttribute( "dir" );
230         final String JavaDoc[] includes =
231             buildSelectors( config.getElementsByTagName( "include" ) );
232         final String JavaDoc[] excludes =
233             buildSelectors( config.getElementsByTagName( "exclude" ) );
234         return new FileSetMetaData( dir, includes, excludes );
235     }
236
237     /**
238      * Build and array of selectors (ie includes/excludes).
239      *
240      * @param configs the nodes to process
241      * @return an array of selectors
242      */

243     private String JavaDoc[] buildSelectors( final NodeList JavaDoc configs )
244     {
245         final ArrayList JavaDoc selectors = new ArrayList JavaDoc();
246
247         final int length = configs.getLength();
248         for( int i = 0; i < length; i++ )
249         {
250             final Element JavaDoc element = (Element JavaDoc)configs.item( i );
251             final String JavaDoc name = element.getAttribute( "name" );
252             selectors.add( name );
253         }
254
255         return (String JavaDoc[])selectors.toArray( new String JavaDoc[ selectors.size() ] );
256     }
257
258     /**
259      * Build file entrys from a nodelist.
260      *
261      * @param configs the nodes to procexx
262      * @return an array of file entrys
263      */

264     private String JavaDoc[] buildEntrys( final NodeList JavaDoc configs )
265     {
266         final ArrayList JavaDoc entrys = new ArrayList JavaDoc();
267
268         final int length = configs.getLength();
269         for( int i = 0; i < length; i++ )
270         {
271             final Element JavaDoc config = (Element JavaDoc)configs.item( i );
272             final String JavaDoc entry = config.getAttribute( "location" );
273             entrys.add( entry );
274         }
275
276         return (String JavaDoc[])entrys.toArray( new String JavaDoc[ entrys.size() ] );
277     }
278
279     /**
280      * Build an array of Join classloaders out of supplied nodes.
281      *
282      * @param configs the nodes to process
283      * @return the join meta data array
284      */

285     private JoinMetaData[] buildJoins( final NodeList JavaDoc configs )
286     {
287         final ArrayList JavaDoc joins = new ArrayList JavaDoc();
288
289         final int length = configs.getLength();
290         for( int i = 0; i < length; i++ )
291         {
292             final Element JavaDoc config = (Element JavaDoc)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     /**
301      * Build a Join classloader out of supplied Element.
302      *
303      * @param config the config element
304      * @return the join meta data
305      */

306     private JoinMetaData buildJoin( final Element JavaDoc config )
307     {
308         final String JavaDoc name = config.getAttribute( "name" );
309         final NodeList JavaDoc children =
310             config.getElementsByTagName( "classloader-ref" );
311         final String JavaDoc[] classloaders =
312             buildClassLoaderRefs( children );
313         return new JoinMetaData( name, classloaders );
314     }
315
316     /**
317      * Create a set of strings for ClassLoader references in a Join
318      * classloader.
319      *
320      * @param configs the nodes to process
321      * @return the strings for class loader refs
322      */

323     private String JavaDoc[] buildClassLoaderRefs( final NodeList JavaDoc configs )
324     {
325         final ArrayList JavaDoc refs = new ArrayList JavaDoc();
326
327         final int length = configs.getLength();
328         for( int i = 0; i < length; i++ )
329         {
330             final Element JavaDoc element = (Element JavaDoc)configs.item( i );
331             final String JavaDoc ref = element.getAttribute( "name" );
332             refs.add( ref );
333         }
334
335         return (String JavaDoc[])refs.toArray( new String JavaDoc[ refs.size() ] );
336     }
337
338     /**
339      * Utility class to test if attribute is unspecified (by testing if it is
340      * equal to empty string).
341      *
342      * @param attribute the attribute
343      * @return true if equal to empty string
344      */

345     private boolean isUnspecified( final String JavaDoc attribute )
346     {
347         return "".equals( attribute );
348     }
349 }
Popular Tags