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 *    https://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 */
020package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.api.util.Strings;
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 */
034@SuppressWarnings("serial")
035public final class RegexSyntaxChecker extends SyntaxChecker
036{
037    /** the set of regular expressions */
038    private String[] expressions;
039    
040    /**
041     * A static Builder for this class
042     */
043    public static final class Builder extends SCBuilder<RegexSyntaxChecker>
044    {
045        /** the set of regular expressions */
046        private String[] expressions;
047        
048        /**
049         * The Builder constructor
050         */
051        private Builder()
052        {
053            super( null );
054        }
055
056
057        /**
058         * Add a list of regexp to be applied by this SyntaxChecker
059         * 
060         * @param expressions The regexp list to add
061         * @return the RegexSyntaxChecker Builder instance
062         */
063        public Builder setExpressions( String[] expressions )
064        {
065            if ( ( expressions != null ) && ( expressions.length > 0 ) )
066            {
067                this.expressions = new String[expressions.length];
068                System.arraycopy( expressions, 0, this.expressions, 0, expressions.length );
069            }
070            
071            return this;
072        }
073        
074        
075        /**
076         * Create a new instance of RegexSyntaxChecker
077         * @return A new instance of RegexSyntaxChecker
078         */
079        public RegexSyntaxChecker build()
080        {
081            return new RegexSyntaxChecker( oid, expressions );
082        }
083    }
084
085    
086    /**
087     * Creates a Syntax validator for a specific Syntax using Perl5 matching
088     * rules for validation.
089     * 
090     * @param oid the oid of the Syntax values checked
091     * @param matchExprArray the array of matching expressions
092     */
093    private RegexSyntaxChecker( String oid, String[] matchExprArray )
094    {
095        super( oid );
096
097        this.expressions = matchExprArray;
098    }
099
100    
101    /**
102     * @return An instance of the Builder for this class
103     */
104    public static Builder builder()
105    {
106        return new Builder();
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public boolean isValidSyntax( Object value )
115    {
116        String str;
117
118        if ( value instanceof String )
119        {
120            str = ( String ) value;
121
122            for ( String regexp : expressions )
123            {
124                if ( !str.matches( regexp ) )
125                {
126                    if ( LOG.isDebugEnabled() )
127                    {
128                        LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
129                    }
130                    
131                    return false;
132                }
133            }
134        }
135
136        if ( LOG.isDebugEnabled() )
137        {
138            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
139        }
140
141        return true;
142    }
143
144
145    /**
146     * Get the list of regexp stored into this SyntaxChecker
147     * 
148     * @return AN array containing all the stored regexp
149     */
150    public String[] getExpressions()
151    {
152        if ( expressions == null )
153        {
154            return Strings.EMPTY_STRING_ARRAY;
155        }
156        
157        String[] exprs = new String[expressions.length];
158        System.arraycopy( expressions, 0, exprs, 0, expressions.length );
159        
160        return exprs;
161    }
162}