View Javadoc
1   package org.codehaus.plexus.configuration;
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.util.ArrayList;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * @version $Id$
26   */
27  public class DefaultPlexusConfiguration
28      implements PlexusConfiguration
29  {
30  
31      private String name;
32  
33      private String value;
34  
35      private Map<String, String> attributes;
36  
37      private Map<String, List<PlexusConfiguration>> childMap;
38  
39      private List<PlexusConfiguration> childList;
40  
41      protected DefaultPlexusConfiguration()
42      {
43          this( "configuration" );
44      }
45  
46      protected DefaultPlexusConfiguration( String name )
47      {
48          this( name, null );
49      }
50  
51      protected DefaultPlexusConfiguration( String name, String value )
52      {
53          super();
54  
55          this.name = name;
56  
57          this.value = value;
58  
59          this.attributes = new LinkedHashMap<String, String>();
60  
61          this.childMap = new LinkedHashMap<String, List<PlexusConfiguration>>();
62  
63          this.childList = new ArrayList<PlexusConfiguration>();
64      }
65  
66      // ----------------------------------------------------------------------
67      // Name handling
68      // ----------------------------------------------------------------------
69  
70      public String getName()
71      {
72          return name;
73      }
74  
75      public void setName( String name )
76      {
77          this.name = name;
78      }
79  
80      // ----------------------------------------------------------------------
81      // Value handling
82      // ----------------------------------------------------------------------
83  
84      public String getValue()
85      {
86          return value;
87      }
88  
89      public String getValue( String defaultValue )
90      {
91          String value = getValue();
92  
93          if ( value == null )
94          {
95              value = defaultValue;
96          }
97  
98          return value;
99      }
100 
101     public void setValue( String val )
102     {
103         value = val;
104     }
105 
106     public PlexusConfiguration setValueAndGetSelf( String val )
107     {
108         setValue( val );
109 
110         return this;
111     }
112 
113     // ----------------------------------------------------------------------
114     // Attribute handling
115     // ----------------------------------------------------------------------
116     public void setAttribute( String name, String value )
117     {
118         attributes.put( name, value );
119     }
120 
121     public String getAttribute( String name )
122     {
123         return attributes.get( name );
124     }
125 
126     public String getAttribute( String name, String defaultValue )
127     {
128         String value = getAttribute( name );
129 
130         if ( value == null )
131         {
132             value = defaultValue;
133         }
134 
135         return value;
136     }
137 
138     public String[] getAttributeNames()
139     {
140         return attributes.keySet().toArray( new String[attributes.size()] );
141     }
142 
143     // ----------------------------------------------------------------------
144     // Child handling
145     // ----------------------------------------------------------------------
146 
147     public PlexusConfiguration getChild( String name )
148     {
149         return getChild( name, true );
150     }
151 
152     public PlexusConfiguration getChild( int i )
153     {
154         return childList.get( i );
155     }
156 
157     public PlexusConfiguration getChild( String name, boolean createChild )
158     {
159         List<PlexusConfiguration> childs = childMap.get( name );
160 
161         boolean noneFound = ( childs == null || childs.size() == 0 );
162 
163         if ( noneFound && createChild )
164         {
165             addChild( name );
166 
167             return getChild( name, false );
168         }
169         else if ( noneFound && !createChild )
170         {
171             return null;
172         }
173         else
174         {
175             return childs.get( 0 );
176         }
177     }
178 
179     public PlexusConfiguration[] getChildren()
180     {
181         return childList.toArray( new PlexusConfiguration[childList.size()] );
182     }
183 
184     public PlexusConfiguration[] getChildren( String name )
185     {
186         List<PlexusConfiguration> childs = new ArrayList<PlexusConfiguration>();
187 
188         List<PlexusConfiguration> childList = childMap.get( name );
189 
190         if ( childList != null )
191         {
192             childs.addAll( childList );
193         }
194 
195         return childs.toArray( new PlexusConfiguration[childs.size()] );
196     }
197 
198     public void addChild( PlexusConfiguration child )
199     {
200         childList.add( child );
201 
202         List<PlexusConfiguration> children = childMap.get( child.getName() );
203         if ( children == null )
204         {
205             childMap.put( child.getName(), children = new ArrayList<PlexusConfiguration>() );
206         }
207 
208         children.add( child );
209     }
210 
211     public PlexusConfiguration addChild( String name )
212     {
213         // we are using reflection to try to create same class childs as parent is,
214         // since many Maven and Maven plugins stuff casts the incoming result of this call
215         // to the evil XmlPlexusConfiguration
216         PlexusConfiguration child = null;
217 
218         try
219         {
220             child = getClass().newInstance();
221 
222             child.setName( name );
223         }
224         catch ( Exception e )
225         {
226             // we have a PlexusConfiguration that has no constructor(name)
227             child = new DefaultPlexusConfiguration( name );
228         }
229 
230         addChild( child );
231 
232         return this;
233     }
234 
235     public PlexusConfiguration addChild( String name, String value )
236     {
237         PlexusConfiguration child = new DefaultPlexusConfiguration( name, value );
238 
239         addChild( child );
240 
241         return this;
242     }
243 
244     public int getChildCount()
245     {
246         return this.childList.size();
247     }
248 
249 }