001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.schema.syntaxes;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
026    
027    
028    /**
029     * A SyntaxChecker implemented using Perl5 regular expressions to constrain
030     * values.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 736240 $
034     */
035    public class RegexSyntaxChecker extends AbstractSyntaxChecker
036    {
037        /** the set of regular expressions */
038        private List<String> expressions;
039    
040    
041        /**
042         * Creates a Syntax validator for a specific Syntax using Perl5 matching
043         * rules for validation.
044         * 
045         * @param oid
046         *            the oid of the Syntax values checked
047         * @param matchExprArray
048         *            the array of matching expressions
049         */
050        public RegexSyntaxChecker( String oid, String[] matchExprArray )
051        {
052            super( oid );
053            
054            if ( ( matchExprArray != null ) && ( matchExprArray.length != 0 ) )
055            {
056                expressions = new ArrayList<String>( matchExprArray.length );
057                
058                for ( String regexp:matchExprArray )
059                {
060                    expressions.add( regexp );
061                }
062            }
063            else
064            {
065                expressions = new ArrayList<String>();
066            }
067        }
068    
069    
070        /**
071         * 
072         * Creates a new instance of RegexSyntaxChecker.
073         * 
074         * @param oid the oid to associate with this new SyntaxChecker
075         *
076         */
077        protected RegexSyntaxChecker( String oid )
078        {
079            super( oid );
080            expressions = new ArrayList<String>();
081        }
082        
083        /**
084         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
085         * 
086         * @param value the value of some attribute with the syntax
087         * @return true if the value is in the valid syntax, false otherwise
088         */
089        public boolean isValidSyntax( Object value )
090        {
091            String str = null;
092            boolean match = true;
093    
094            if ( value instanceof String )
095            {
096                str = ( String ) value;
097    
098                for ( String regexp:expressions )
099                {
100                    match = match && str.matches( regexp );
101    
102                    if ( !match )
103                    {
104                        break;
105                    }
106                }
107            }
108    
109            return match;
110        }
111    
112        /**
113         * Get the list of regexp stored into this SyntaxChecker
114         * 
115         * @return AN array containing all the stored regexp
116         */
117        public String[] getExpressions()
118        {
119            String[] exprs = new String[ expressions.size() ];
120            return expressions.toArray( exprs );
121        }
122    
123        /**
124         * Add a list of regexp to be applied by this SyntaxChecker
125         * 
126         * @param expressions The regexp list to add
127         */
128        public void setExpressions( String[] expressions )
129        {
130            for ( String regexp:expressions )
131            {
132                this.expressions.add( regexp );
133            }
134    
135        }
136    }