001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 024 025/** 026 * Abstract enforcer rule that give a foundation to validate properties from multiple sources. 027 * 028 * @author Paul Gier 029 * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a> 030 * @version $Id: AbstractPropertyEnforcerRule.java 1496229 2013-06-24 21:43:56Z rfscholte $ 031 */ 032public abstract class AbstractPropertyEnforcerRule 033 extends AbstractNonCacheableEnforcerRule 034{ 035 036 /** 037 * Match the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 038 * 039 * @deprecated the visibility will be reduced to private with the next major version 040 * @see {@link #setRegex(String)} 041 * @see {@link #getRegex()} 042 */ 043 public String regex = null; 044 045 /** 046 * Specify a warning message if the regular expression is not matched. 047 * 048 * @deprecated the visibility will be reduced to private with the next major version 049 * @see {@link #setRegexMessage(String)} 050 * @see {@link #getRegexMessage()} 051 */ 052 public String regexMessage = null; 053 054 public AbstractPropertyEnforcerRule() 055 { 056 super(); 057 } 058 059 /** 060 * Set the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 061 * 062 * @param the regular expression 063 */ 064 public final void setRegex( String regex ) 065 { 066 this.regex = regex; 067 } 068 069 /** 070 * Get the property value to a given regular expression. Defaults to <code>null</code> (any value is ok). 071 * 072 * @return the regular expression 073 */ 074 public final String getRegex() 075 { 076 return regex; 077 } 078 079 /** 080 * Set a warning message if the regular expression is not matched. 081 * 082 * @param regexMessage the regex message 083 */ 084 public final void setRegexMessage( String regexMessage ) 085 { 086 this.regexMessage = regexMessage; 087 } 088 089 /** 090 * Get a warning message if the regular expression is not matched. 091 * 092 * @return the regex message 093 */ 094 public final String getRegexMessage() 095 { 096 return regexMessage; 097 } 098 099 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}