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 * @version $Id$
031 */
032public abstract class AbstractPropertyEnforcerRule
033    extends AbstractNonCacheableEnforcerRule
034{
035
036    /**
037     * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
038     * 
039     * @see {@link #setRegex(String)}
040     * @see {@link #getRegex()}
041     */
042    private String regex = null;
043
044    /**
045     * Specify a warning message if the regular expression is not matched.
046     * 
047     * @see {@link #setRegexMessage(String)}
048     * @see {@link #getRegexMessage()}
049     */
050    private String regexMessage = null;
051
052    public AbstractPropertyEnforcerRule()
053    {
054        super();
055    }
056    
057    /**
058     * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
059     * 
060     * @param regex The regular expression
061     */
062    public final void setRegex( String regex )
063    {
064        this.regex = regex;
065    }
066
067    /**
068     * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
069     * 
070     * @return the regular expression
071     */
072    public final String getRegex()
073    {
074        return regex;
075    }
076    
077    /**
078     * Set a warning message if the regular expression is not matched.
079     * 
080     * @param regexMessage the regex message
081     */
082    public final void setRegexMessage( String regexMessage )
083    {
084        this.regexMessage = regexMessage;
085    }
086    
087    /**
088     * Get a warning message if the regular expression is not matched.
089     * 
090     * @return the regex message
091     */
092    public final String getRegexMessage()
093    {
094        return regexMessage;
095    }
096
097    @Override
098    public void execute( EnforcerRuleHelper helper )
099        throws EnforcerRuleException
100    {
101        Object propValue = resolveValue( helper );
102
103        // Check that the property is not null or empty string
104        if ( propValue == null )
105        {
106            String message = getMessage();
107            if ( message == null )
108            {
109                message = getName() + " \"" + getPropertyName() + "\" is required for this build.";
110            }
111            throw new EnforcerRuleException( message );
112        }
113        // If there is a regex, check that the property matches it
114        if ( regex != null && !propValue.toString().matches( regex ) )
115        {
116            if ( regexMessage == null )
117            {
118                regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\".  "
119                    + "This does not match the regular expression \"" + regex + "\"";
120            }
121            throw new EnforcerRuleException( regexMessage );
122        }
123    }
124
125    /**
126     * How the property that is being evaluated is called
127     */
128    public abstract String getName();
129
130    /**
131     * The name of the property currently being evaluated, this is used for default message pourpouses only
132     */
133    public abstract String getPropertyName();
134
135    /**
136     * Resolves the property value
137     *
138     * @param helper
139     * @throws EnforcerRuleException
140     */
141    public abstract Object resolveValue( EnforcerRuleHelper helper )
142        throws EnforcerRuleException;
143
144}