View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.*;
16  
17  import java.util.Collection;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.regex.Pattern;
21  
22  import net.sf.oval.ConstraintTarget;
23  import net.sf.oval.Validator;
24  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
25  import net.sf.oval.context.OValContext;
26  import net.sf.oval.internal.util.ArrayUtils;
27  
28  /**
29   * @author Sebastian Thomschke
30   */
31  public class MatchPatternCheck extends AbstractAnnotationCheck<MatchPattern>
32  {
33  	private static final long serialVersionUID = 1L;
34  
35  	private final List<Pattern> patterns = getCollectionFactory().createList(2);
36  	private boolean matchAll = true;
37  
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	@Override
42  	public void configure(final MatchPattern constraintAnnotation)
43  	{
44  		super.configure(constraintAnnotation);
45  
46  		setMatchAll(constraintAnnotation.matchAll());
47  
48  		synchronized (patterns)
49  		{
50  			patterns.clear();
51  			final String[] stringPatterns = constraintAnnotation.pattern();
52  			final int[] f = constraintAnnotation.flags();
53  			for (int i = 0, l = stringPatterns.length; i < l; i++)
54  			{
55  				final int flag = f.length > i ? f[i] : 0;
56  				final Pattern p = Pattern.compile(stringPatterns[i], flag);
57  				patterns.add(p);
58  			}
59  			requireMessageVariablesRecreation();
60  		}
61  	}
62  
63  	/**
64  	 * {@inheritDoc}
65  	 */
66  	@Override
67  	protected Map<String, String> createMessageVariables()
68  	{
69  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
70  		messageVariables.put("pattern", patterns.size() == 1 ? patterns.get(0).toString() : patterns.toString());
71  		return messageVariables;
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	@Override
78  	protected ConstraintTarget[] getAppliesToDefault()
79  	{
80  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
81  	}
82  
83  	/**
84  	 * @return the pattern
85  	 */
86  	public Pattern[] getPatterns()
87  	{
88  		synchronized (patterns)
89  		{
90  			return patterns.toArray(new Pattern[patterns.size()]);
91  		}
92  	}
93  
94  	/**
95  	 * @return the matchAll
96  	 */
97  	public boolean isMatchAll()
98  	{
99  		return matchAll;
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
106 			final Validator validator)
107 	{
108 		if (valueToValidate == null) return true;
109 
110 		for (final Pattern p : patterns)
111 		{
112 			final boolean matches = p.matcher(valueToValidate.toString()).matches();
113 
114 			if (matches)
115 			{
116 				if (!matchAll) return true;
117 			}
118 			else if (matchAll) return false;
119 		}
120 		return matchAll ? true : false;
121 	}
122 
123 	/**
124 	 * @param matchAll the matchAll to set
125 	 */
126 	public void setMatchAll(final boolean matchAll)
127 	{
128 		this.matchAll = matchAll;
129 		requireMessageVariablesRecreation();
130 	}
131 
132 	/**
133 	 * @param pattern the pattern to set
134 	 */
135 	public void setPattern(final Pattern pattern)
136 	{
137 		synchronized (patterns)
138 		{
139 			patterns.clear();
140 			patterns.add(pattern);
141 		}
142 		requireMessageVariablesRecreation();
143 	}
144 
145 	/**
146 	 * @param pattern the pattern to set
147 	 */
148 	public void setPattern(final String pattern, final int flags)
149 	{
150 		synchronized (patterns)
151 		{
152 			patterns.clear();
153 			patterns.add(Pattern.compile(pattern, flags));
154 		}
155 		requireMessageVariablesRecreation();
156 	}
157 
158 	/**
159 	 * @param patterns the patterns to set
160 	 */
161 	public void setPatterns(final Collection<Pattern> patterns)
162 	{
163 		synchronized (this.patterns)
164 		{
165 			this.patterns.clear();
166 			this.patterns.addAll(patterns);
167 		}
168 		requireMessageVariablesRecreation();
169 	}
170 
171 	/**
172 	 * @param patterns the patterns to set
173 	 */
174 	public void setPatterns(final Pattern... patterns)
175 	{
176 		synchronized (this.patterns)
177 		{
178 			this.patterns.clear();
179 			ArrayUtils.addAll(this.patterns, patterns);
180 		}
181 		requireMessageVariablesRecreation();
182 	}
183 }