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 1631135 2014-10-11 23:27:06Z khmarbaise $ 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 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 98 /** 99 * Execute the rule. 100 * 101 * @param helper the helper 102 * @throws EnforcerRuleException the enforcer rule exception 103 */ 104 public void execute( EnforcerRuleHelper helper ) 105 throws EnforcerRuleException 106 { 107 Object propValue = resolveValue( helper ); 108 109 // Check that the property is not null or empty string 110 if ( propValue == null ) 111 { 112 String message = getMessage(); 113 if ( message == null ) 114 { 115 message = getName() + " \"" + getPropertyName() + "\" is required for this build."; 116 } 117 throw new EnforcerRuleException( message ); 118 } 119 // If there is a regex, check that the property matches it 120 if ( regex != null && !propValue.toString().matches( regex ) ) 121 { 122 if ( regexMessage == null ) 123 { 124 regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\". " 125 + "This does not match the regular expression \"" + regex + "\""; 126 } 127 throw new EnforcerRuleException( regexMessage ); 128 } 129 } 130 131 /** 132 * How the property that is being evaluated is called 133 */ 134 public abstract String getName(); 135 136 /** 137 * The name of the property currently being evaluated, this is used for default message pourpouses only 138 */ 139 public abstract String getPropertyName(); 140 141 /** 142 * Resolves the property value 143 * 144 * @param helper 145 * @throws EnforcerRuleException 146 */ 147 public abstract Object resolveValue( EnforcerRuleHelper helper ) 148 throws EnforcerRuleException; 149 150 }