View Javadoc
1   package org.codehaus.plexus.configuration.xml;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.StringWriter;
21  
22  import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
23  import org.codehaus.plexus.configuration.io.XmlPlexusConfigurationWriter;
24  import org.codehaus.plexus.util.xml.Xpp3Dom;
25  
26  /**
27   */
28  public class XmlPlexusConfiguration extends DefaultPlexusConfiguration {
29      public XmlPlexusConfiguration() {
30          super();
31      }
32  
33      public XmlPlexusConfiguration(String name) {
34          super(name);
35      }
36  
37      public XmlPlexusConfiguration(String name, String value) {
38          super(name, value);
39      }
40  
41      public XmlPlexusConfiguration(Xpp3Dom dom) {
42          super(dom.getName(), dom.getValue());
43  
44          // attrs
45          String[] attributes = dom.getAttributeNames();
46  
47          for (String attribute : attributes) {
48              setAttribute(attribute, dom.getAttribute(attribute));
49          }
50  
51          // children
52          int childCount = dom.getChildCount();
53          for (int i = 0; i < childCount; i++) {
54              addChild(new XmlPlexusConfiguration(dom.getChild(i)));
55          }
56      }
57  
58      // ----------------------------------------------------------------------
59      //
60      // ----------------------------------------------------------------------
61  
62      public String toString() {
63          StringWriter sw = new StringWriter();
64  
65          XmlPlexusConfigurationWriter xw = new XmlPlexusConfigurationWriter();
66  
67          try {
68              xw.write(sw, this);
69          } catch (IOException e) {
70              // will not happen with StringWriter
71          }
72  
73          return sw.toString();
74      }
75  }