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.syntaxCheckers;
021
022 import java.util.ArrayList;
023 import java.util.List;
024
025 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029
030 /**
031 * A SyntaxChecker implemented using Perl5 regular expressions to constrain
032 * values.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 736240 $
036 */
037 public class RegexSyntaxChecker extends SyntaxChecker
038 {
039 /** A logger for this class */
040 private static final Logger LOG = LoggerFactory.getLogger( RegexSyntaxChecker.class );
041
042 /** The serialVersionUID */
043 private static final long serialVersionUID = 1L;
044
045 /** the set of regular expressions */
046 private List<String> expressions;
047
048 /**
049 * Creates a Syntax validator for a specific Syntax using Perl5 matching
050 * rules for validation.
051 *
052 * @param oid
053 * the oid of the Syntax values checked
054 * @param matchExprArray
055 * the array of matching expressions
056 */
057 public RegexSyntaxChecker( String oid, String[] matchExprArray )
058 {
059 super( oid );
060
061 if ( ( matchExprArray != null ) && ( matchExprArray.length != 0 ) )
062 {
063 expressions = new ArrayList<String>( matchExprArray.length );
064
065 for ( String regexp:matchExprArray )
066 {
067 expressions.add( regexp );
068 }
069 }
070 else
071 {
072 expressions = new ArrayList<String>();
073 }
074 }
075
076
077 /**
078 *
079 * Creates a new instance of RegexSyntaxChecker.
080 *
081 * @param oid the oid to associate with this new SyntaxChecker
082 *
083 */
084 public RegexSyntaxChecker( String oid )
085 {
086 super( oid );
087 expressions = new ArrayList<String>();
088 }
089
090
091 /**
092 * {@inheritDoc}
093 */
094 public boolean isValidSyntax( Object value )
095 {
096 String str = null;
097 boolean match = true;
098
099 if ( value instanceof String )
100 {
101 str = ( String ) value;
102
103 for ( String regexp:expressions )
104 {
105 match = match && str.matches( regexp );
106
107 if ( !match )
108 {
109 break;
110 }
111 }
112 }
113
114 if ( match )
115 {
116 LOG.debug( "Syntax valid for '{}'", value );
117 }
118 else
119 {
120 LOG.debug( "Syntax invalid for '{}'", value );
121 }
122
123 return match;
124 }
125
126 /**
127 * Get the list of regexp stored into this SyntaxChecker
128 *
129 * @return AN array containing all the stored regexp
130 */
131 public String[] getExpressions()
132 {
133 String[] exprs = new String[ expressions.size() ];
134 return expressions.toArray( exprs );
135 }
136
137 /**
138 * Add a list of regexp to be applied by this SyntaxChecker
139 *
140 * @param expressions The regexp list to add
141 */
142 public void setExpressions( String[] expressions )
143 {
144 for ( String regexp:expressions )
145 {
146 this.expressions.add( regexp );
147 }
148 }
149 }