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 1382374 2012-09-08 21:47:58Z olamy $
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 public String regex = null;
40
41 /**
42 * Specify a warning message if the regular expression is not matched.
43 */
44 public String regexMessage = null;
45
46 public AbstractPropertyEnforcerRule()
47 {
48 super();
49 }
50
51 /**
52 * Execute the rule.
53 *
54 * @param helper the helper
55 * @throws EnforcerRuleException the enforcer rule exception
56 */
57 public void execute( EnforcerRuleHelper helper )
58 throws EnforcerRuleException
59 {
60 Object propValue = resolveValue( helper );
61
62 // Check that the property is not null or empty string
63 if ( propValue == null )
64 {
65 if ( message == null )
66 {
67 message = getName() + " \"" + getPropertyName() + "\" is required for this build.";
68 }
69 throw new EnforcerRuleException( message );
70 }
71 // If there is a regex, check that the property matches it
72 if ( regex != null && !propValue.toString().matches( regex ) )
73 {
74 if ( regexMessage == null )
75 {
76 regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\". "
77 + "This does not match the regular expression \"" + regex + "\"";
78 }
79 throw new EnforcerRuleException( regexMessage );
80 }
81 }
82
83 /**
84 * How the property that is being evaluated is called
85 */
86 public abstract String getName();
87
88 /**
89 * The name of the property currently being evaluated, this is used for default message pourpouses only
90 */
91 public abstract String getPropertyName();
92
93 /**
94 * Resolves the property value
95 *
96 * @param helper
97 * @throws EnforcerRuleException
98 */
99 public abstract Object resolveValue( EnforcerRuleHelper helper )
100 throws EnforcerRuleException;
101
102 }