blob: 42cdf9c0acd2a6aea1d140f6f3b978a7af56f4c4 [file] [log] [blame]
package org.apache.maven.plugins.toolchain;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
/**
* Custom Plexus ConfigurationConverter to instantiate <code>ToolchainRequirement</code> from configuration.
*
* @author mkleint
* @see ToolchainsRequirement
*/
public class ToolchainConverter
extends AbstractConfigurationConverter
{
/**
* @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
*/
@Override
public boolean canConvert( Class type )
{
return ToolchainsRequirement.class.isAssignableFrom( type );
}
/**
* @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)
*/
@Override
public Object fromConfiguration( ConverterLookup converterLookup,
PlexusConfiguration configuration,
Class type, Class baseType,
ClassLoader classLoader,
ExpressionEvaluator expressionEvaluator,
ConfigurationListener listener )
throws ComponentConfigurationException
{
ToolchainsRequirement retValue = new ToolchainsRequirement();
processConfiguration( retValue, configuration, expressionEvaluator );
return retValue;
}
private void processConfiguration( ToolchainsRequirement requirement,
PlexusConfiguration configuration,
ExpressionEvaluator expressionEvaluator )
{
Map<String, Map<String, String>> map = new HashMap<>();
PlexusConfiguration[] tools = configuration.getChildren();
for ( PlexusConfiguration tool : tools )
{
String type = tool.getName();
PlexusConfiguration[] params = tool.getChildren();
Map<String, String> parameters =
Arrays.stream( params ).collect( Collectors.toMap( PlexusConfiguration::getName,
PlexusConfiguration::getValue ) );
map.put( type, parameters );
}
Arrays.stream( configuration.getChildren() )
.map( s ->
new AbstractMap.SimpleEntry<>( s.getName(),
Arrays.stream( s.getChildren() ).collect( Collectors.toMap( PlexusConfiguration::getName,
PlexusConfiguration::getValue ) ) )
).collect( Collectors.toMap( k -> k, v -> v ) );
requirement.toolchains = map;
}
}