View Javadoc
1   package org.codehaus.plexus.configuration.io;
2   
3   import org.codehaus.plexus.configuration.PlexusConfiguration;
4   
5   import java.io.IOException;
6   import java.io.OutputStream;
7   import java.io.OutputStreamWriter;
8   import java.io.Writer;
9   
10  public class XmlPlexusConfigurationWriter
11      implements PlexusConfigurationWriter
12  {
13  
14      public void write( OutputStream outputStream, PlexusConfiguration configuration )
15          throws IOException
16      {
17          write( new OutputStreamWriter( outputStream ), configuration );
18      }
19  
20      public void write( Writer writer, PlexusConfiguration configuration )
21          throws IOException
22      {
23          int depth = 0;
24  
25          display( configuration, writer, depth );
26      }
27  
28      private void display( PlexusConfiguration c, Writer w, int depth )
29          throws IOException
30      {
31          int count = c.getChildCount();
32  
33          if ( count == 0 )
34          {
35              displayTag( c, w, depth );
36          }
37          else
38          {
39              indent( depth, w );
40              w.write( '<' );
41              w.write( c.getName() );
42  
43              attributes( c, w );
44  
45              w.write( '>' );
46              w.write( '\n' );
47  
48              for ( int i = 0; i < count; i++ )
49              {
50                  PlexusConfiguration child = c.getChild( i );
51  
52                  display( child, w, depth + 1 );
53              }
54  
55              indent( depth, w );
56              w.write( '<' );
57              w.write( '/' );
58              w.write( c.getName() );
59              w.write( '>' );
60              w.write( '\n' );
61          }
62      }
63  
64      private void displayTag( PlexusConfiguration c, Writer w, int depth )
65          throws IOException
66      {
67          String value = c.getValue( null );
68  
69          if ( value != null )
70          {
71              indent( depth, w );
72              w.write( '<' );
73              w.write( c.getName() );
74  
75              attributes( c, w );
76  
77              w.write( '>' );
78              w.write( c.getValue( null ) );
79              w.write( '<' );
80              w.write( '/' );
81              w.write( c.getName() );
82              w.write( '>' );
83              w.write( '\n' );
84          }
85          else
86          {
87              indent( depth, w );
88              w.write( '<' );
89              w.write( c.getName() );
90  
91              attributes( c, w );
92  
93              w.write( '/' );
94              w.write( '>' );
95              w.write( "\n" );
96          }
97      }
98  
99      private void attributes( PlexusConfiguration c, Writer w )
100         throws IOException
101     {
102         String[] names = c.getAttributeNames();
103 
104         for (String name : names) {
105             w.write(' ');
106             w.write(name);
107             w.write('=');
108             w.write('"');
109             w.write(c.getAttribute(name, null));
110             w.write('"');
111         }
112     }
113 
114     private void indent( int depth, Writer w )
115         throws IOException
116     {
117         for ( int i = 0; i < depth; i++ )
118         {
119             w.write( ' ' );
120         }
121     }
122 
123 }