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