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