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 public class AntrunXmlPlexusConfigurationWriter
35 {
36
37
38
39
40
41
42 public void write( PlexusConfiguration configuration, Writer writer )
43 throws IOException
44 {
45 final int depth = 0;
46
47 PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( writer );
48 write( configuration, xmlWriter, depth );
49 }
50
51 private void write( PlexusConfiguration c, XMLWriter w, int depth )
52 throws IOException
53 {
54 int count = c.getChildCount();
55
56 if ( count == 0 )
57 {
58 writeTag( c, w, depth );
59 }
60 else
61 {
62 w.startElement( c.getName() );
63 writeAttributes( c, w );
64
65 for ( int i = 0; i < count; i++ )
66 {
67 PlexusConfiguration child = c.getChild( i );
68
69 write( child, w, depth + 1 );
70 }
71
72 w.endElement();
73 }
74 }
75
76 private void writeTag( PlexusConfiguration c, XMLWriter w, int depth )
77 throws IOException
78 {
79 w.startElement( c.getName() );
80
81 writeAttributes( c, w );
82
83 String value = c.getValue( null );
84 if ( value != null )
85 {
86 w.writeText( value );
87 }
88
89 w.endElement();
90 }
91
92 private void writeAttributes( PlexusConfiguration c, XMLWriter w )
93 throws IOException
94 {
95 String[] names = c.getAttributeNames();
96
97 for ( String name : names )
98 {
99 w.addAttribute( name, c.getAttribute( name, null ) );
100 }
101 }
102
103 }
104