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   * @version $Id$
28   */
29  public class XmlPlexusConfiguration
30      extends DefaultPlexusConfiguration
31  {
32      public XmlPlexusConfiguration()
33      {
34          super();
35      }
36  
37      public XmlPlexusConfiguration( String name )
38      {
39          super( name );
40      }
41      
42      public XmlPlexusConfiguration( String name, String value )
43      {
44          super( name, value );
45      }
46  
47      public XmlPlexusConfiguration( Xpp3Dom dom )
48      {
49          super( dom.getName(), dom.getValue() );
50  
51          // attrs
52          String[] attributes = dom.getAttributeNames();
53  
54          for (String attribute : attributes) {
55              setAttribute(attribute, dom.getAttribute(attribute));
56          }
57  
58          // children
59          int childCount = dom.getChildCount();
60          for ( int i = 0; i < childCount; i++ )
61          {
62              addChild( new XmlPlexusConfiguration( dom.getChild( i ) ) );
63          }
64      }
65  
66      // ----------------------------------------------------------------------
67      //
68      // ----------------------------------------------------------------------
69  
70      public String toString()
71      {
72          StringWriter sw = new StringWriter();
73  
74          XmlPlexusConfigurationWriter xw = new XmlPlexusConfigurationWriter();
75  
76          try
77          {
78              xw.write( sw, this );
79          }
80          catch ( IOException e )
81          {
82              // will not happen with StringWriter
83          }
84  
85          return sw.toString();
86      }
87  }