001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules.utils;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.List;
025import java.util.Objects;
026
027import org.apache.maven.model.Plugin;
028import org.apache.maven.model.ReportPlugin;
029import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
030
031/**
032 * The Class EnforcerRuleUtils.
033 *
034 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
035 */
036@Named
037public class EnforcerRuleUtils {
038
039    private final ExpressionEvaluator evaluator;
040    /**
041     * Instantiates a new enforcer rule utils.
042     *
043     * @param evaluator the expression evaluator
044     */
045    @Inject
046    public EnforcerRuleUtils(ExpressionEvaluator evaluator) {
047        this.evaluator = Objects.requireNonNull(evaluator);
048    }
049
050    private void resolve(Plugin plugin) {
051        try {
052            plugin.setGroupId((String) evaluator.evaluate(plugin.getGroupId()));
053            plugin.setArtifactId((String) evaluator.evaluate(plugin.getArtifactId()));
054            plugin.setVersion((String) evaluator.evaluate(plugin.getVersion()));
055        } catch (ExpressionEvaluationException e) {
056            // this should have gone already before
057        }
058    }
059
060    private void resolve(ReportPlugin plugin) {
061        try {
062            plugin.setGroupId((String) evaluator.evaluate(plugin.getGroupId()));
063            plugin.setArtifactId((String) evaluator.evaluate(plugin.getArtifactId()));
064            plugin.setVersion((String) evaluator.evaluate(plugin.getVersion()));
065        } catch (ExpressionEvaluationException e) {
066            // this should have gone already before
067        }
068    }
069
070    public List<Plugin> resolvePlugins(List<Plugin> plugins) {
071        for (Plugin plugin : plugins) {
072            resolve(plugin);
073        }
074        return plugins;
075    }
076
077    public List<ReportPlugin> resolveReportPlugins(List<ReportPlugin> reportPlugins) {
078        for (ReportPlugin plugin : reportPlugins) {
079            resolve(plugin);
080        }
081        return reportPlugins;
082    }
083}