001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper.fields;
016
017import static com.google.common.base.Preconditions.checkState;
018import static java.lang.String.format;
019
020import org.basepom.mojo.propertyhelper.Field;
021import org.basepom.mojo.propertyhelper.FieldContext;
022import org.basepom.mojo.propertyhelper.ValueProvider;
023import org.basepom.mojo.propertyhelper.definitions.MacroDefinition;
024import org.basepom.mojo.propertyhelper.macros.MacroType;
025
026import java.util.Optional;
027import java.util.StringJoiner;
028
029import com.google.common.annotations.VisibleForTesting;
030import org.apache.maven.plugin.MojoExecutionException;
031
032public final class MacroField extends Field<String, MacroDefinition> {
033
034    private final ValueProvider valueProvider;
035    private final FieldContext context;
036
037    @VisibleForTesting
038    public static MacroField forTesting(MacroDefinition dateDefinition, ValueProvider valueProvider, FieldContext fieldContext) {
039        return new MacroField(dateDefinition, valueProvider, fieldContext);
040    }
041
042    public MacroField(final MacroDefinition macroDefinition, final ValueProvider valueProvider, final FieldContext context) {
043        super(macroDefinition, context);
044
045        this.valueProvider = valueProvider;
046        this.context = context;
047    }
048
049    @Override
050    public String getFieldName() {
051        return fieldDefinition.getId();
052    }
053
054    @Override
055    public String getValue() throws MojoExecutionException {
056        final Optional<String> type = fieldDefinition.getMacroType();
057        final MacroType macroType;
058
059        try {
060            if (type.isPresent()) {
061                macroType = context.getMacros().get(type.get());
062                checkState(macroType != null, "Could not locate macro '%s'", type.get());
063            } else {
064                final Optional<String> macroClassName = fieldDefinition.getMacroClass();
065                checkState(macroClassName.isPresent(), "No definition for macro '%s' found!", fieldDefinition.getId());
066                final Class<? extends MacroType> macroClass = (Class<? extends MacroType>) Class.forName(macroClassName.get());
067                macroType = macroClass.getDeclaredConstructor().newInstance();
068            }
069
070            return formatResult(macroType.getValue(fieldDefinition, valueProvider, context).orElse(null));
071
072        } catch (ReflectiveOperationException e) {
073            throw new MojoExecutionException(format("Could not instantiate '%s'", fieldDefinition), e);
074        }
075    }
076
077    @Override
078    public String toString() {
079        return new StringJoiner(", ", MacroField.class.getSimpleName() + "[", "]")
080            .add("valueProvider=" + valueProvider)
081            .add("context=" + context)
082            .toString();
083    }
084}