KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > info > writer > XMLTypeWriter


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
26     "Apache Software Foundation" must not be used to endorse or promote
27     products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48 */

49
50 package org.apache.avalon.meta.info.writer;
51
52 import java.io.IOException JavaDoc;
53 import java.io.OutputStream JavaDoc;
54 import java.io.OutputStreamWriter JavaDoc;
55 import java.io.Writer JavaDoc;
56
57 import org.apache.avalon.meta.info.ContextDescriptor;
58 import org.apache.avalon.meta.info.DependencyDescriptor;
59 import org.apache.avalon.meta.info.Descriptor;
60 import org.apache.avalon.meta.info.EntryDescriptor;
61 import org.apache.avalon.meta.info.InfoDescriptor;
62 import org.apache.avalon.meta.info.CategoryDescriptor;
63 import org.apache.avalon.meta.info.ServiceDescriptor;
64 import org.apache.avalon.meta.info.StageDescriptor;
65 import org.apache.avalon.meta.info.ExtensionDescriptor;
66 import org.apache.avalon.meta.info.Type;
67
68 /**
69  * Write {@link Type} objects to a stream as xml documents.
70  *
71  * TODO: Address configuration schema support
72  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
73  * @version $Revision: 1.13 $ $Date: 2003/08/11 22:06:38 $
74  */

75 public class XMLTypeWriter
76     implements TypeWriter
77 {
78     private static final String JavaDoc CONTEXT_CLASS =
79       "org.apache.avalon.framework.context.Context";
80
81     /**
82      * Write out type representation to xml.
83      *
84      * @param type the type object
85      * @param outputStream the stream to write to
86      * @throws Exception if unable to write xml
87      */

88     public void writeType( final Type type,
89                                     final OutputStream JavaDoc outputStream )
90         throws Exception JavaDoc
91     {
92         final Writer JavaDoc writer = new OutputStreamWriter JavaDoc( outputStream );
93         writeHeader( writer );
94         writeDoctype( writer, "type" );
95         writer.write( "\n\n<type>" );
96         writeInfo( writer, type.getInfo() );
97         writeLoggers( writer, type.getCategories() );
98         writeContext( writer, type.getContext() );
99         writeServices( writer, type.getServices() );
100         writeDependencies( writer, type.getDependencies() );
101         writeStages( writer, type.getStages() );
102         writeExtensions( writer, type.getExtensions() );
103         writer.write( "\n</type>" );
104         writer.flush();
105     }
106
107    /**
108     * Write the XML header.
109     * @param writer the writer
110      * @throws IOException if unable to write xml
111     */

112     private void writeHeader( final Writer JavaDoc writer )
113         throws IOException JavaDoc
114     {
115         writer.write( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" );
116     }
117
118     /**
119      * Write out DOCType delcaration.
120      *
121      * @param writer the writer
122      * @param root the root name of document
123      * @throws IOException if unable to write xml
124      */

125     private void writeDoctype( final Writer JavaDoc writer,
126                                final String JavaDoc root )
127         throws IOException JavaDoc
128     {
129         final String JavaDoc doctype =
130             "\n<!DOCTYPE " + root
131             + " PUBLIC \"-//AVALON/Type DTD Version 1.0//EN\" "
132             + "\"http://avalon.apache.org/dtds/meta/type_1_1.dtd\" >";
133         writer.write( doctype );
134     }
135
136     /**
137      * Write out xml representation of the info descriptor from a Type.
138      *
139      * @param writer the writer
140      * @param info the type info descriptor
141      * @throws IOException if unable to write xml
142      */

143     private void writeInfo( final Writer JavaDoc writer, final InfoDescriptor info )
144         throws IOException JavaDoc
145     {
146         writer.write( "\n <info>" );
147
148         writer.write( "\n <name>");
149         writer.write( info.getName() );
150         writer.write( "</name>" );
151
152         writer.write( "\n <version>");
153         writer.write( info.getVersion().toString() );
154         writer.write( "</version>");
155
156         if( info.getConfigurationSchema() != null )
157         {
158             writer.write( "\n <schema>");
159             writer.write( info.getConfigurationSchema() );
160             writer.write( "</schema>");
161         }
162
163         writer.write( "\n <lifestyle>");
164         writer.write( info.getLifestyle() );
165         writer.write( "</lifestyle>" );
166
167         if( 0 == info.getAttributeNames().length )
168         {
169             writer.write( "\n </info>" );
170         }
171         else
172         {
173             writeAttributes( writer, info );
174             writer.write( "\n </info>" );
175         }
176     }
177
178     /**
179      * Write out xml representation of a set of loggers.
180      *
181      * @param writer the writer
182      * @param loggers the loggers
183      * @throws IOException if unable to write xml
184      */

185     private void writeLoggers( final Writer JavaDoc writer,
186                                final CategoryDescriptor[] loggers )
187         throws IOException JavaDoc
188     {
189         if( 0 == loggers.length )
190         {
191             return;
192         }
193         writer.write( "\n <loggers>" );
194         for( int i = 0; i < loggers.length; i++ )
195         {
196             writeLogger( writer, loggers[ i ] );
197         }
198
199         writer.write( "\n </loggers>" );
200     }
201
202     /**
203      * Write out xml representation of a logger.
204      *
205      * @param writer the writer
206      * @param logger the logger
207      * @throws IOException if unable to write xml
208      */

209     private void writeLogger( final Writer JavaDoc writer,
210                               final CategoryDescriptor logger )
211         throws IOException JavaDoc
212     {
213         writer.write( "\n <logger name=\"" );
214         writer.write( logger.getName() );
215         if( 0 == logger.getAttributeNames().length )
216         {
217             writer.write( "\"/>" );
218         }
219         else
220         {
221             writer.write( "\">" );
222             writeAttributes( writer, logger );
223             writer.write( "\n </logger>" );
224         }
225     }
226
227     /**
228      * Write out xml representation of a context.
229      *
230      * @param writer the writer
231      * @param context the context
232      * @throws IOException if unable to write xml
233      */

234     private void writeContext( final Writer JavaDoc writer,
235                                final ContextDescriptor context )
236         throws IOException JavaDoc
237     {
238         final String JavaDoc type = context.getContextInterfaceClassname();
239         final EntryDescriptor[] entrys = context.getEntries();
240         final int count = context.getAttributeNames().length;
241
242         if( CONTEXT_CLASS.equals( type )
243             && 0 == count
244             && 0 == entrys.length )
245         {
246             return;
247         }
248
249         writer.write( "\n <context" );
250         if( !CONTEXT_CLASS.equals( type ) )
251         {
252             writer.write( " type=\"" );
253             writer.write( type );
254             writer.write( "\"" );
255         }
256
257         if( 0 == count && 0 == entrys.length )
258         {
259             writer.write( "/>" );
260         }
261         else
262         {
263             writer.write( ">" );
264             for( int i = 0; i < entrys.length; i++ )
265             {
266                 writeEntry( writer, entrys[ i ] );
267             }
268             writeAttributes( writer, context );
269             writer.write( "\n </context>" );
270         }
271     }
272
273     /**
274      * Write out xml representation of an entry.
275      *
276      * @param writer the writer
277      * @param entry the entry
278      * @throws IOException if unable to write xml
279      */

280     private void writeEntry( final Writer JavaDoc writer,
281                              final EntryDescriptor entry )
282         throws IOException JavaDoc
283     {
284         writer.write( "\n <entry key=\"" );
285         writer.write( entry.getKey() + "\" " );
286
287         if( !entry.getClassname().equals( "java.lang.String" ) )
288         {
289             writer.write( "type=\"" );
290             writer.write( entry.getClassname() );
291             writer.write( "\" " );
292         }
293
294         if( entry.getAlias() != null )
295         {
296             if( !entry.getAlias().equals( entry.getKey() ) )
297             {
298                 writer.write( "alias=\"" + entry.getAlias() + "\" " );
299             }
300         }
301         if( entry.isOptional() )
302         {
303             writer.write( "\" optional=\"true\" " );
304         }
305         if( entry.isVolatile() )
306         {
307             writer.write( "\" volatile=\"true\" " );
308         }
309
310         writer.write( "/>" );
311     }
312
313     /**
314      * Write out xml representation of a set of services.
315      *
316      * @param writer the writer
317      * @param services the services
318      * @throws IOException if unable to write xml
319      */

320     private void writeServices( final Writer JavaDoc writer,
321                                 final ServiceDescriptor[] services )
322         throws IOException JavaDoc
323     {
324         if( 0 == services.length )
325         {
326             return;
327         }
328
329         writer.write( "\n <services>" );
330         for( int i = 0; i < services.length; i++ )
331         {
332             final ServiceDescriptor service = services[ i ];
333             writer.write( "\n <service type=\"" );
334             writer.write( service.getReference().getClassname() );
335             if( service.getReference().getVersion().getMajor() > -1 )
336             {
337                 writer.write( "\" version=\"" );
338                 writer.write( service.getReference().getVersion().toString() );
339             }
340             final int count = service.getAttributeNames().length;
341             if( 0 == count )
342             {
343                 writer.write( "\"/>" );
344             }
345             else
346             {
347                 writer.write( "\">" );
348                 writeAttributes( writer, service );
349                 writer.write( "\n </service>" );
350             }
351         }
352         writer.write( "\n </services>" );
353     }
354
355     /**
356      * Write out xml representation of a set of dependencies.
357      *
358      * @param writer the writer
359      * @param dependencies the dependencies
360      * @throws IOException if unable to write xml
361      */

362     private void writeDependencies( final Writer JavaDoc writer,
363                                     final DependencyDescriptor[] dependencies )
364         throws IOException JavaDoc
365     {
366         if( 0 == dependencies.length )
367         {
368             return;
369         }
370
371         writer.write( "\n <dependencies>" );
372         for( int i = 0; i < dependencies.length; i++ )
373         {
374             final DependencyDescriptor dependency = dependencies[ i ];
375             writer.write( "\n <dependency " );
376
377             if( !dependency.getKey().equals( dependency.getReference().getClassname() ) )
378             {
379                 writer.write( "key=\"" );
380                 writer.write( dependency.getKey() );
381                 writer.write( "\" " );
382             }
383
384             writer.write( "type=\"" );
385             writer.write( dependency.getReference().getClassname() );
386             if( dependency.getReference().getVersion().getMajor() > -1 )
387             {
388                 writer.write( "\" version=\"" );
389                 writer.write( dependency.getReference().getVersion().toString() );
390             }
391
392             if( dependency.isOptional() )
393             {
394                 writer.write( "\" optional=\"true" );
395             }
396
397             final int count = dependency.getAttributeNames().length;
398             if( 0 == count )
399             {
400                 writer.write( "\"/>" );
401             }
402             else
403             {
404                 writer.write( "\">" );
405                 writeAttributes( writer, dependency );
406                 writer.write( "\n </dependency>" );
407             }
408         }
409         writer.write( "\n </dependencies>" );
410     }
411
412     /**
413      * Write out xml representation of a set of attributes.
414      *
415      * @param writer the writer
416      * @param descriptor a meta info descriptor holding attributes
417      * @throws IOException if unable to write xml
418      */

419     private void writeAttributes( final Writer JavaDoc writer, final Descriptor descriptor )
420         throws IOException JavaDoc
421     {
422         String JavaDoc[] names = descriptor.getAttributeNames();
423         if( names.length > 0 )
424         {
425             writer.write( "\n <attributes>" );
426             for( int i = 0; i < names.length; i++ )
427             {
428                 writeAttribute( writer, names[i], descriptor.getAttribute( names[i] ) );
429             }
430             writer.write( "\n </attributes>" );
431         }
432     }
433
434     /**
435      * Write out xml representation of a single attribute entry.
436      *
437      * @param writer the writer
438      * @param key the attribute key
439      * @param value the attribute value
440      * @throws IOException if unable to write xml
441      */

442     private void writeAttribute( final Writer JavaDoc writer,
443                                  final String JavaDoc key, final String JavaDoc value )
444         throws IOException JavaDoc
445     {
446         writer.write( "\n <attribute key=\"" );
447         writer.write( key );
448         writer.write( "\" value=\"" );
449         writer.write( value );
450         writer.write( "\"/>" );
451     }
452
453     /**
454      * Write out xml representation of a set of stages.
455      *
456      * @param writer the writer
457      * @param stages the stages
458      * @throws IOException if unable to write xml
459      */

460     private void writeStages( final Writer JavaDoc writer,
461                               final StageDescriptor[] stages )
462         throws IOException JavaDoc
463     {
464         if( 0 == stages.length )
465         {
466             return;
467         }
468
469         writer.write( "\n <stages>" );
470         for( int i = 0; i < stages.length; i++ )
471         {
472             final StageDescriptor stage = stages[ i ];
473             writer.write( "\n <stage " );
474             writer.write( "id=\"" );
475             writer.write( stage.getKey() );
476
477             final int count = stage.getAttributeNames().length;
478             if( 0 == count )
479             {
480                 writer.write( "\"/>" );
481             }
482             else
483             {
484                 writer.write( "\">" );
485                 writeAttributes( writer, stage );
486                 writer.write( "\n </stage>" );
487             }
488         }
489         writer.write( "\n </stages>" );
490     }
491
492     /**
493      * Write out xml representation of a set of extensions.
494      *
495      * @param writer the writer
496      * @param extensions the extensions
497      * @throws IOException if unable to write xml
498      */

499     private void writeExtensions( final Writer JavaDoc writer,
500                                   final ExtensionDescriptor[] extensions )
501         throws IOException JavaDoc
502     {
503         if( 0 == extensions.length )
504         {
505             return;
506         }
507
508         writer.write( "\n <extensions>" );
509         for( int i = 0; i < extensions.length; i++ )
510         {
511             final ExtensionDescriptor extension = extensions[ i ];
512
513             writer.write( "\n <extension " );
514             writer.write( "id=\"" );
515             writer.write( extension.getKey() );
516
517             final int count = extension.getAttributeNames().length;
518             if( 0 == count )
519             {
520                 writer.write( "\"/>" );
521             }
522             else
523             {
524                 writer.write( "\">" );
525                 writeAttributes( writer, extension );
526                 writer.write( "\n </extension>" );
527             }
528         }
529         writer.write( "\n </extensions>" );
530     }
531 }
532
Popular Tags