1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.maven.enforcer.rules.property; 20 21 import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 22 import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule; 23 24 /** 25 * Abstract enforcer rule that give a foundation to validate properties from multiple sources. 26 * 27 * @author Paul Gier 28 * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a> 29 */ 30 abstract class AbstractPropertyEnforcerRule extends AbstractStandardEnforcerRule { 31 32 /** 33 * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 34 * 35 * @see {@link #setRegex(String)} 36 * @see {@link #getRegex()} 37 */ 38 private String regex = null; 39 40 /** 41 * Specify a warning message if the regular expression is not matched. 42 * 43 * @see {@link #setRegexMessage(String)} 44 * @see {@link #getRegexMessage()} 45 */ 46 private String regexMessage = null; 47 48 /** 49 * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 50 * 51 * @param regex The regular expression 52 */ 53 public final void setRegex(String regex) { 54 this.regex = regex; 55 } 56 57 /** 58 * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 59 * 60 * @return the regular expression 61 */ 62 public final String getRegex() { 63 return regex; 64 } 65 66 /** 67 * Set a warning message if the regular expression is not matched. 68 * 69 * @param regexMessage the regex message 70 */ 71 public final void setRegexMessage(String regexMessage) { 72 this.regexMessage = regexMessage; 73 } 74 75 /** 76 * Get a warning message if the regular expression is not matched. 77 * 78 * @return the regex message 79 */ 80 public final String getRegexMessage() { 81 return regexMessage; 82 } 83 84 @Override 85 public void execute() throws EnforcerRuleException { 86 Object propValue = resolveValue(); 87 88 // Check that the property is not null or empty string 89 if (propValue == null) { 90 String message = getMessage(); 91 if (message == null) { 92 message = getName() + " \"" + getPropertyName() + "\" is required for this build."; 93 } 94 throw new EnforcerRuleException(message); 95 } 96 // If there is a regex, check that the property matches it 97 if (regex != null && !propValue.toString().matches(regex)) { 98 if (regexMessage == null) { 99 regexMessage = getName() + " \"" + getPropertyName() + "\" evaluates to \"" + propValue + "\". " 100 + "This does not match the regular expression \"" + regex + "\""; 101 } 102 throw new EnforcerRuleException(regexMessage); 103 } 104 } 105 106 /** 107 * How the property that is being evaluated is called 108 * @return kind of property 109 */ 110 protected abstract String getName(); 111 112 /** 113 * The name of the property currently being evaluated, this is used for default message purposes only 114 * 115 * @return the name of the property 116 */ 117 protected abstract String getPropertyName(); 118 119 /** 120 * Resolves the property value 121 * 122 * @return a resolve value 123 * @throws EnforcerRuleException in case of problems 124 */ 125 protected abstract Object resolveValue() throws EnforcerRuleException; 126 }