View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
24  
25  /**
26   * Abstract enforcer rule that give a foundation to validate properties from multiple sources.
27   *
28   * @author Paul Gier
29   * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a>
30   * @version $Id: AbstractPropertyEnforcerRule.java 1802595 2017-07-21 13:39:48Z rfscholte $
31   */
32  public abstract class AbstractPropertyEnforcerRule
33      extends AbstractNonCacheableEnforcerRule
34  {
35  
36      /**
37       * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
38       * 
39       * @see {@link #setRegex(String)}
40       * @see {@link #getRegex()}
41       */
42      private String regex = null;
43  
44      /**
45       * Specify a warning message if the regular expression is not matched.
46       * 
47       * @see {@link #setRegexMessage(String)}
48       * @see {@link #getRegexMessage()}
49       */
50      private String regexMessage = null;
51  
52      public AbstractPropertyEnforcerRule()
53      {
54          super();
55      }
56      
57      /**
58       * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
59       * 
60       * @param regex The regular expression
61       */
62      public final void setRegex( String regex )
63      {
64          this.regex = regex;
65      }
66  
67      /**
68       * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
69       * 
70       * @return the regular expression
71       */
72      public final String getRegex()
73      {
74          return regex;
75      }
76      
77      /**
78       * Set a warning message if the regular expression is not matched.
79       * 
80       * @param regexMessage the regex message
81       */
82      public final void setRegexMessage( String regexMessage )
83      {
84          this.regexMessage = regexMessage;
85      }
86      
87      /**
88       * Get a warning message if the regular expression is not matched.
89       * 
90       * @return the regex message
91       */
92      public final String getRegexMessage()
93      {
94          return regexMessage;
95      }
96  
97      @Override
98      public void execute( EnforcerRuleHelper helper )
99          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 }