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   */
31  public abstract class AbstractPropertyEnforcerRule
32      extends AbstractNonCacheableEnforcerRule
33  {
34  
35      /**
36       * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
37       * 
38       * @see {@link #setRegex(String)}
39       * @see {@link #getRegex()}
40       */
41      private String regex = null;
42  
43      /**
44       * Specify a warning message if the regular expression is not matched.
45       * 
46       * @see {@link #setRegexMessage(String)}
47       * @see {@link #getRegexMessage()}
48       */
49      private String regexMessage = null;
50  
51      public AbstractPropertyEnforcerRule()
52      {
53          super();
54      }
55      
56      /**
57       * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
58       * 
59       * @param regex The regular expression
60       */
61      public final void setRegex( String regex )
62      {
63          this.regex = regex;
64      }
65  
66      /**
67       * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
68       * 
69       * @return the regular expression
70       */
71      public final String getRegex()
72      {
73          return regex;
74      }
75      
76      /**
77       * Set a warning message if the regular expression is not matched.
78       * 
79       * @param regexMessage the regex message
80       */
81      public final void setRegexMessage( String regexMessage )
82      {
83          this.regexMessage = regexMessage;
84      }
85      
86      /**
87       * Get a warning message if the regular expression is not matched.
88       * 
89       * @return the regex message
90       */
91      public final String getRegexMessage()
92      {
93          return regexMessage;
94      }
95  
96      @Override
97      public void execute( EnforcerRuleHelper helper )
98          throws EnforcerRuleException
99      {
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      */
127     public abstract String getName();
128 
129     /**
130      * The name of the property currently being evaluated, this is used for default message pourpouses only
131      */
132     public abstract String getPropertyName();
133 
134     /**
135      * Resolves the property value
136      *
137      * @param helper
138      * @throws EnforcerRuleException
139      */
140     public abstract Object resolveValue( EnforcerRuleHelper helper )
141         throws EnforcerRuleException;
142 
143 }