001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.constraint; 014 015 import java.lang.annotation.Documented; 016 import java.lang.annotation.ElementType; 017 import java.lang.annotation.Retention; 018 import java.lang.annotation.RetentionPolicy; 019 import java.lang.annotation.Target; 020 021 import net.sf.oval.ConstraintTarget; 022 import net.sf.oval.ConstraintViolation; 023 import net.sf.oval.configuration.annotation.Constraint; 024 import net.sf.oval.configuration.annotation.Constraints; 025 026 /** 027 * Check if the specified regular expression pattern is matched. 028 * 029 * <br><br> 030 * <b>Note:</b> This constraint is also satisfied when the value to validate is null, therefore you might also need to specified @NotNull 031 * 032 * @author Sebastian Thomschke 033 */ 034 @Documented 035 @Retention(RetentionPolicy.RUNTIME) 036 @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) 037 @Constraint(checkWith = MatchPatternCheck.class) 038 public @interface MatchPattern 039 { 040 @Documented 041 @Retention(RetentionPolicy.RUNTIME) 042 @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) 043 @Constraints 044 public @interface List 045 { 046 /** 047 * The MatchPattern constraints. 048 */ 049 MatchPattern[] value(); 050 051 /** 052 * Formula returning <code>true</code> if this constraint shall be evaluated and 053 * <code>false</code> if it shall be ignored for the current validation. 054 * <p> 055 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used. 056 * E.g. <code>groovy:_this.amount > 10</code> 057 * <p> 058 * Available context variables are:<br> 059 * <b>_this</b> -> the validated bean<br> 060 * <b>_value</b> -> the value to validate (e.g. the field value, parameter value, method return value, 061 * or the validated bean for object level constraints) 062 */ 063 String when() default ""; 064 } 065 066 /** 067 * <p>In case the constraint is declared for an array, collection or map this controls how the constraint is applied to it and it's child objects. 068 * 069 * <p><b>Default:</b> ConstraintTarget.VALUES 070 * 071 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection. 072 */ 073 ConstraintTarget[] appliesTo() default ConstraintTarget.VALUES; 074 075 /** 076 * error code passed to the ConstraintViolation object 077 */ 078 String errorCode() default "net.sf.oval.constraint.MatchPattern"; 079 080 /** 081 * Match flags, a bit mask that may include 082 * Pattern.CASE_INSENSITIVE, Pattern.MULTILINE, Pattern.DOTALL, 083 * Pattern.UNICODE_CASE, Pattern.CANON_EQ 084 * 085 * @see java.util.regex.Pattern 086 */ 087 int[] flags() default 0; 088 089 /** 090 * specifies if all of the declared patterns must match or if one is sufficient 091 */ 092 boolean matchAll() default true; 093 094 /** 095 * message to be used for the ContraintsViolatedException 096 * 097 * @see ConstraintViolation 098 */ 099 String message() default "net.sf.oval.constraint.MatchPattern.violated"; 100 101 /** 102 * The regular expression(s) to match against 103 * <br><br> 104 * Examples:<br> 105 * decimal number: "^-{0,1}(\\d*|(\\d{1,3}([,]\\d{3})*))[.]?\\d*$"<br> 106 * numbers only: "^\\d*$"<br> 107 * e-mail address: "^([a-z0-9]{1,}[\\.\\_\\-]?[a-z0-9]{1,})\\@([a-z0-9]{2,}\\.)([a-z]{2,2}|org|net|com|gov|edu|int|info|biz|museum)$"<br> 108 * 109 * @see java.util.regex.Pattern 110 */ 111 String[] pattern(); 112 113 /** 114 * The associated constraint profiles. 115 */ 116 String[] profiles() default {}; 117 118 /** 119 * severity passed to the ConstraintViolation object 120 */ 121 int severity() default 0; 122 123 /** 124 * An expression to specify where in the object graph relative from this object the expression 125 * should be applied. 126 * <p> 127 * Examples: 128 * <li>"owner" would apply this constraint to the current object's property <code>owner</code> 129 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code> 130 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied. 131 */ 132 String target() default ""; 133 134 /** 135 * Formula returning <code>true</code> if this constraint shall be evaluated and 136 * <code>false</code> if it shall be ignored for the current validation. 137 * <p> 138 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used. 139 * E.g. <code>groovy:_this.amount > 10</code> 140 * <p> 141 * Available context variables are:<br> 142 * <b>_this</b> -> the validated bean<br> 143 * <b>_value</b> -> the value to validate (e.g. the field value, parameter value, method return value, 144 * or the validated bean for object level constraints) 145 */ 146 String when() default ""; 147 }