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 1496229 2013-06-24 21:43:56Z 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       * @deprecated the visibility will be reduced to private with the next major version
40       * @see {@link #setRegex(String)}
41       * @see {@link #getRegex()}
42       */
43      public String regex = null;
44  
45      /**
46       * Specify a warning message if the regular expression is not matched.
47       * 
48       * @deprecated the visibility will be reduced to private with the next major version
49       * @see {@link #setRegexMessage(String)}
50       * @see {@link #getRegexMessage()}
51       */
52      public String regexMessage = null;
53  
54      public AbstractPropertyEnforcerRule()
55      {
56          super();
57      }
58      
59      /**
60       * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
61       * 
62       * @param the regular expression
63       */
64      public final void setRegex( String regex )
65      {
66          this.regex = regex;
67      }
68  
69      /**
70       * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok).
71       * 
72       * @return the regular expression
73       */
74      public final String getRegex()
75      {
76          return regex;
77      }
78      
79      /**
80       * Set a warning message if the regular expression is not matched.
81       * 
82       * @param regexMessage the regex message
83       */
84      public final void setRegexMessage( String regexMessage )
85      {
86          this.regexMessage = regexMessage;
87      }
88      
89      /**
90       * Get a warning message if the regular expression is not matched.
91       * 
92       * @return the regex message
93       */
94      public final String getRegexMessage()
95      {
96          return regexMessage;
97      }
98      
99  
100     /**
101      * Execute the rule.
102      *
103      * @param helper the helper
104      * @throws EnforcerRuleException the enforcer rule exception
105      */
106     public void execute( EnforcerRuleHelper helper )
107         throws EnforcerRuleException
108     {
109         Object propValue = resolveValue( helper );
110 
111         // Check that the property is not null or empty string
112         if ( propValue == null )
113         {
114             String message = getMessage();
115             if ( message == null )
116             {
117                 message = getName() + " \"" + getPropertyName() + "\" is required for this build.";
118             }
119             throw new EnforcerRuleException( message );
120         }
121         // If there is a regex, check that the property matches it
122         if ( regex != null && !propValue.toString().matches( regex ) )
123         {
124             if ( regexMessage == null )
125             {
126                 regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\".  "
127                     + "This does not match the regular expression \"" + regex + "\"";
128             }
129             throw new EnforcerRuleException( regexMessage );
130         }
131     }
132 
133     /**
134      * How the property that is being evaluated is called
135      */
136     public abstract String getName();
137 
138     /**
139      * The name of the property currently being evaluated, this is used for default message pourpouses only
140      */
141     public abstract String getPropertyName();
142 
143     /**
144      * Resolves the property value
145      *
146      * @param helper
147      * @throws EnforcerRuleException
148      */
149     public abstract Object resolveValue( EnforcerRuleHelper helper )
150         throws EnforcerRuleException;
151 
152 }