001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
024
025/**
026 * Abstract enforcer rule that give a foundation to validate properties from multiple sources.
027 *
028 * @author Paul Gier
029 * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a>
030 */
031public abstract class AbstractPropertyEnforcerRule
032    extends AbstractNonCacheableEnforcerRule
033{
034
035    /**
036     * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
037     * 
038     * @see {@link #setRegex(String)}
039     * @see {@link #getRegex()}
040     */
041    private String regex = null;
042
043    /**
044     * Specify a warning message if the regular expression is not matched.
045     * 
046     * @see {@link #setRegexMessage(String)}
047     * @see {@link #getRegexMessage()}
048     */
049    private String regexMessage = null;
050
051    public AbstractPropertyEnforcerRule()
052    {
053        super();
054    }
055    
056    /**
057     * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
058     * 
059     * @param regex The regular expression
060     */
061    public final void setRegex( String regex )
062    {
063        this.regex = regex;
064    }
065
066    /**
067     * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
068     * 
069     * @return the regular expression
070     */
071    public final String getRegex()
072    {
073        return regex;
074    }
075    
076    /**
077     * Set a warning message if the regular expression is not matched.
078     * 
079     * @param regexMessage the regex message
080     */
081    public final void setRegexMessage( String regexMessage )
082    {
083        this.regexMessage = regexMessage;
084    }
085    
086    /**
087     * Get a warning message if the regular expression is not matched.
088     * 
089     * @return the regex message
090     */
091    public final String getRegexMessage()
092    {
093        return regexMessage;
094    }
095
096    @Override
097    public void execute( EnforcerRuleHelper helper )
098        throws EnforcerRuleException
099    {
100        Object propValue = resolveValue( helper );
101
102        // Check that the property is not null or empty string
103        if ( propValue == null )
104        {
105            String message = getMessage();
106            if ( message == null )
107            {
108                message = getName() + " \"" + getPropertyName() + "\" is required for this build.";
109            }
110            throw new EnforcerRuleException( message );
111        }
112        // If there is a regex, check that the property matches it
113        if ( regex != null && !propValue.toString().matches( regex ) )
114        {
115            if ( regexMessage == null )
116            {
117                regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\".  "
118                    + "This does not match the regular expression \"" + regex + "\"";
119            }
120            throw new EnforcerRuleException( regexMessage );
121        }
122    }
123
124    /**
125     * How the property that is being evaluated is called
126     * @return kind of property
127     */
128    protected abstract String getName();
129
130    /**
131     * The name of the property currently being evaluated, this is used for default message purposes only
132     * 
133     * @return the name of the property
134     */
135    protected abstract String getPropertyName();
136
137    /**
138     * Resolves the property value
139     *
140     * @param helper
141     * @throws EnforcerRuleException
142     */
143    protected abstract Object resolveValue( EnforcerRuleHelper helper )
144        throws EnforcerRuleException;
145
146}