View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.metadata.gleaner;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import org.codehaus.plexus.component.repository.ComponentDescriptor;
23  import org.codehaus.plexus.configuration.PlexusConfiguration;
24  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
25  
26  /**
27   * Support for component gleaner implementations.
28   *
29   */
30  public abstract class ComponentGleanerSupport {
31      private static final String EMPTY_STRING = "";
32  
33      protected String filterEmptyAsNull(final String value) {
34          if (value == null) {
35              return null;
36          } else if (EMPTY_STRING.equals(value.trim())) {
37              return null;
38          } else {
39              return value;
40          }
41      }
42  
43      protected boolean isRequirementListType(final Class<?> type) {
44          // assert type != null;
45  
46          return Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type);
47      }
48  
49      protected void addChildConfiguration(final ComponentDescriptor<?> component, final PlexusConfiguration config) {
50          // assert component != null;
51          // assert config != null;
52  
53          if (!component.hasConfiguration()) {
54              component.setConfiguration(new XmlPlexusConfiguration("configuration"));
55          }
56  
57          component.getConfiguration().addChild(config);
58      }
59  
60      protected String deHump(final String string) {
61          // assert string != null;
62  
63          StringBuilder buff = new StringBuilder();
64  
65          for (int i = 0; i < string.length(); i++) {
66              if (i != 0 && Character.isUpperCase(string.charAt(i))) {
67                  buff.append('-');
68              }
69  
70              buff.append(string.charAt(i));
71          }
72  
73          return buff.toString().trim().toLowerCase();
74      }
75  }