View Javadoc
1   package org.apache.maven.plugin.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.ConfigurationConverter;
29  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
31  import org.codehaus.plexus.configuration.PlexusConfiguration;
32  import org.codehaus.plexus.configuration.PlexusConfigurationException;
33  
34  /**
35   * Custom Plexus ConfigurationConverter to instantiate <code>ToolchainRequirement</code> from configuration.
36   *
37   * @author mkleint
38   * @see ToolchainsRequirement
39   */
40  public class ToolchainConverter
41      extends AbstractConfigurationConverter
42  {
43  
44      public static final String ROLE = ConfigurationConverter.class.getName();
45  
46      /**
47       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
48       */
49      public boolean canConvert( Class type )
50      {
51          return ToolchainsRequirement.class.isAssignableFrom( type );
52      }
53  
54      /**
55       * @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)
56       */
57      public Object fromConfiguration( ConverterLookup converterLookup,
58                                       PlexusConfiguration configuration,
59                                       Class type, Class baseType,
60                                       ClassLoader classLoader,
61                                       ExpressionEvaluator expressionEvaluator,
62                                       ConfigurationListener listener )
63          throws ComponentConfigurationException
64      {
65          ToolchainsRequirement retValue = new ToolchainsRequirement();
66  
67          processConfiguration( retValue, configuration, expressionEvaluator );
68  
69          return retValue;
70      }
71  
72      private void processConfiguration( ToolchainsRequirement requirement,
73                                         PlexusConfiguration configuration,
74                                         ExpressionEvaluator expressionEvaluator )
75          throws ComponentConfigurationException
76      {
77          Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
78  
79          PlexusConfiguration[] tools = configuration.getChildren();
80          for ( PlexusConfiguration tool : tools )
81          {
82              String type = tool.getName();
83              PlexusConfiguration[] params = tool.getChildren();
84  
85              Map<String, String> parameters = new HashMap<String, String>();
86              for ( PlexusConfiguration param : params )
87              {
88                  try
89                  {
90                      parameters.put( param.getName(), param.getValue() );
91                  }
92                  catch ( PlexusConfigurationException ex )
93                  {
94                      throw new ComponentConfigurationException( ex );
95                  }
96              }
97              map.put( type, parameters );
98          }
99  
100         requirement.toolchains = map;
101     }
102 }