View Javadoc
1   package org.apache.maven.plugins.toolchain;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
26  import org.codehaus.plexus.component.configurator.ConfigurationListener;
27  import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
28  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
30  import org.codehaus.plexus.configuration.PlexusConfiguration;
31  
32  /**
33   * Custom Plexus ConfigurationConverter to instantiate <code>ToolchainRequirement</code> from configuration.
34   *
35   * @author mkleint
36   * @see ToolchainsRequirement
37   */
38  public class ToolchainConverter
39      extends AbstractConfigurationConverter
40  {
41  
42      /**
43       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
44       */
45      @Override
46      public boolean canConvert( Class type )
47      {
48          return ToolchainsRequirement.class.isAssignableFrom( type );
49      }
50  
51      /**
52       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#fromConfiguration(org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup, org.codehaus.plexus.configuration.PlexusConfiguration, java.lang.Class, java.lang.Class, java.lang.ClassLoader, org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator, org.codehaus.plexus.component.configurator.ConfigurationListener)
53       */
54      @Override
55      public Object fromConfiguration( ConverterLookup converterLookup,
56                                       PlexusConfiguration configuration,
57                                       Class type, Class baseType,
58                                       ClassLoader classLoader,
59                                       ExpressionEvaluator expressionEvaluator,
60                                       ConfigurationListener listener )
61          throws ComponentConfigurationException
62      {
63          ToolchainsRequirement retValue = new ToolchainsRequirement();
64  
65          processConfiguration( retValue, configuration, expressionEvaluator );
66  
67          return retValue;
68      }
69  
70      private void processConfiguration( ToolchainsRequirement requirement,
71                                         PlexusConfiguration configuration,
72                                         ExpressionEvaluator expressionEvaluator )
73          throws ComponentConfigurationException
74      {
75          Map<String, Map<String, String>> map = new HashMap<>();
76  
77          PlexusConfiguration[] tools = configuration.getChildren();
78          for ( PlexusConfiguration tool : tools )
79          {
80              String type = tool.getName();
81              PlexusConfiguration[] params = tool.getChildren();
82  
83              Map<String, String> parameters = new HashMap<>();
84              for ( PlexusConfiguration param : params )
85              {
86                      parameters.put( param.getName(), param.getValue() );
87              }
88              map.put( type, parameters );
89          }
90  
91          requirement.toolchains = map;
92      }
93  }