1 package org.apache.maven.plugin.antrun;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.configuration.PlexusConfiguration;
23 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
24 import org.codehaus.plexus.util.xml.XMLWriter;
25
26 import java.io.IOException;
27 import java.io.Writer;
28
29
30
31
32
33
34
35 public class AntrunXmlPlexusConfigurationWriter
36 {
37
38 public void write( PlexusConfiguration configuration, Writer writer )
39 throws IOException
40 {
41 int depth = 0;
42
43 PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( writer );
44 write( configuration, xmlWriter, depth );
45 }
46
47 private void write( PlexusConfiguration c, XMLWriter w, int depth )
48 throws IOException
49 {
50 int count = c.getChildCount();
51
52 if ( count == 0 )
53 {
54 writeTag( c, w, depth );
55 }
56 else
57 {
58 w.startElement( c.getName() );
59 writeAttributes( c, w );
60
61 for ( int i = 0; i < count; i++ )
62 {
63 PlexusConfiguration child = c.getChild( i );
64
65 write( child, w, depth + 1 );
66 }
67
68 w.endElement();
69 }
70 }
71
72 private void writeTag( PlexusConfiguration c, XMLWriter w, int depth )
73 throws IOException
74 {
75 w.startElement( c.getName() );
76
77 writeAttributes( c, w );
78
79 String value = c.getValue( null );
80 if ( value != null )
81 {
82 w.writeText( value );
83 }
84
85 w.endElement();
86 }
87
88 private void writeAttributes( PlexusConfiguration c, XMLWriter w )
89 throws IOException
90 {
91 String[] names = c.getAttributeNames();
92
93 for ( int i = 0; i < names.length; i++ )
94 {
95 w.addAttribute( names[i], c.getAttribute( names[i], null ) );
96 }
97 }
98
99 }
100