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 static net.sf.oval.Validator.*;
016    
017    import java.util.Collection;
018    import java.util.List;
019    import java.util.Map;
020    import java.util.regex.Pattern;
021    
022    import net.sf.oval.ConstraintTarget;
023    import net.sf.oval.Validator;
024    import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
025    import net.sf.oval.context.OValContext;
026    import net.sf.oval.internal.util.ArrayUtils;
027    
028    /**
029     * @author Sebastian Thomschke
030     */
031    public class NotMatchPatternCheck extends AbstractAnnotationCheck<NotMatchPattern>
032    {
033            private static final long serialVersionUID = 1L;
034    
035            private final List<Pattern> patterns = getCollectionFactory().createList(2);
036    
037            /**
038             * {@inheritDoc}
039             */
040            @Override
041            public void configure(final NotMatchPattern constraintAnnotation)
042            {
043                    super.configure(constraintAnnotation);
044    
045                    synchronized (patterns)
046                    {
047                            patterns.clear();
048                            final String[] stringPatterns = constraintAnnotation.pattern();
049                            final int[] f = constraintAnnotation.flags();
050                            for (int i = 0, l = stringPatterns.length; i < l; i++)
051                            {
052                                    final int flag = f.length > i ? f[i] : 0;
053                                    final Pattern p = Pattern.compile(stringPatterns[i], flag);
054                                    patterns.add(p);
055                            }
056                            requireMessageVariablesRecreation();
057                    }
058            }
059    
060            /**
061             * {@inheritDoc}
062             */
063            @Override
064            protected Map<String, String> createMessageVariables()
065            {
066                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
067                    messageVariables.put("pattern", patterns.size() == 1 ? patterns.get(0).toString() : patterns.toString());
068                    return messageVariables;
069            }
070    
071            /**
072             * {@inheritDoc}
073             */
074            @Override
075            protected ConstraintTarget[] getAppliesToDefault()
076            {
077                    return new ConstraintTarget[]{ConstraintTarget.VALUES};
078            }
079    
080            /**
081             * @return the pattern
082             */
083            public Pattern[] getPatterns()
084            {
085                    synchronized (patterns)
086                    {
087                            return patterns.toArray(new Pattern[patterns.size()]);
088                    }
089            }
090    
091            /**
092             * {@inheritDoc}
093             */
094            public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
095                            final Validator validator)
096            {
097                    if (valueToValidate == null) return true;
098    
099                    for (final Pattern p : patterns)
100                            if (p.matcher(valueToValidate.toString()).matches()) return false;
101                    return true;
102            }
103    
104            /**
105             * @param pattern the pattern to set
106             */
107            public void setPattern(final Pattern pattern)
108            {
109                    synchronized (patterns)
110                    {
111                            patterns.clear();
112                            patterns.add(pattern);
113                    }
114                    requireMessageVariablesRecreation();
115            }
116    
117            /**
118             * @param pattern the pattern to set
119             */
120            public void setPattern(final String pattern, final int flags)
121            {
122                    synchronized (patterns)
123                    {
124                            patterns.clear();
125                            patterns.add(Pattern.compile(pattern, flags));
126                    }
127                    requireMessageVariablesRecreation();
128            }
129    
130            /**
131             * @param patterns the patterns to set
132             */
133            public void setPatterns(final Collection<Pattern> patterns)
134            {
135                    synchronized (this.patterns)
136                    {
137                            this.patterns.clear();
138                            this.patterns.addAll(patterns);
139                    }
140                    requireMessageVariablesRecreation();
141            }
142    
143            /**
144             * @param patterns the patterns to set
145             */
146            public void setPatterns(final Pattern... patterns)
147            {
148                    synchronized (this.patterns)
149                    {
150                            this.patterns.clear();
151                            ArrayUtils.addAll(this.patterns, patterns);
152                    }
153                    requireMessageVariablesRecreation();
154            }
155    }