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
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.regex.Pattern;
026 import java.util.regex.PatternSyntaxException;
027
028 import org.apache.directory.shared.ldap.constants.SchemaConstants;
029 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
030 import org.apache.directory.shared.ldap.util.StringTools;
031 import org.slf4j.Logger;
032 import org.slf4j.LoggerFactory;
033
034
035 /**
036 * A SyntaxChecker which verifies that a value is a TelephoneNumber according to ITU
037 * recommendation E.123 (which is quite vague ...).
038 *
039 * A valid Telephone number respect more or less this syntax :
040 *
041 * " *[+]? *((\([0-9- ]+\))|[0-9- ]+)+"
042 *
043 * If needed, and to allow more syntaxes, a list of regexps has been added
044 * which can be initialized to other values
045 *
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 * @version $Rev$
048 */
049 public class TelephoneNumberSyntaxChecker extends SyntaxChecker
050 {
051 /** A logger for this class */
052 private static final Logger LOG = LoggerFactory.getLogger( TelephoneNumberSyntaxChecker.class );
053
054 /** The serialVersionUID */
055 private static final long serialVersionUID = 1L;
056
057 /** Other regexps to extend the initial one */
058 private List<String> regexps;
059
060 /** Other regexp to extend the initial one, compiled */
061 private List<Pattern> compiledREs;
062
063 /** The default pattern used to check a TelephoneNumber */
064 private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ]+\\))|[0-9- ]+)+$";
065
066 /** The compiled default pattern */
067 private Pattern defaultPattern = Pattern.compile( DEFAULT_REGEXP );
068
069 /** A flag set when only the default regexp should be tested */
070 protected boolean defaultMandatory = false;
071
072 /**
073 * Creates a new instance of TelephoneNumberSyntaxChecker.
074 */
075 public TelephoneNumberSyntaxChecker()
076 {
077 super( SchemaConstants.TELEPHONE_NUMBER_SYNTAX );
078 }
079
080
081 /**
082 * Add a new valid regexp for a Telephone number
083 * @param regexp The new regexp to check
084 */
085 public void addRegexp( String regexp )
086 {
087 if ( defaultMandatory )
088 {
089 return;
090 }
091
092 try
093 {
094 Pattern compiledRE = Pattern.compile( regexp );
095
096 if ( regexps == null )
097 {
098 regexps = new ArrayList<String>();
099 compiledREs = new ArrayList<Pattern>();
100 }
101
102 regexps.add( regexp );
103 compiledREs.add( compiledRE );
104 }
105 catch ( PatternSyntaxException pse )
106 {
107 return;
108 }
109 }
110
111
112 /**
113 * Set the defaut regular expression for the Telephone number
114 *
115 * @param regexp the default regular expression.
116 */
117 public void setDefaultRegexp( String regexp )
118 {
119 try
120 {
121 defaultPattern = Pattern.compile( regexp );
122
123 defaultMandatory = true;
124 regexps = null;
125 compiledREs = null;
126 }
127 catch ( PatternSyntaxException pse )
128 {
129 return;
130 }
131 }
132
133 /**
134 * {@inheritDoc}
135 */
136 public boolean isValidSyntax( Object value )
137 {
138 String strValue = null;
139
140 if ( value == null )
141 {
142 LOG.debug( "Syntax invalid for '{}'", value );
143 return false;
144 }
145
146 if ( value instanceof String )
147 {
148 strValue = ( String ) value;
149 }
150 else if ( value instanceof byte[] )
151 {
152 strValue = StringTools.utf8ToString( ( byte[] ) value );
153 }
154 else
155 {
156 strValue = value.toString();
157 }
158
159 if ( strValue.length() == 0 )
160 {
161 LOG.debug( "Syntax invalid for '{}'", value );
162 return false;
163 }
164
165 // We will use a regexp to check the TelephoneNumber.
166 if ( defaultMandatory )
167 {
168 // We have a unique regexp to check, the default one
169 boolean result = defaultPattern.matcher( strValue ).matches();
170
171 if ( result )
172 {
173 LOG.debug( "Syntax valid for '{}'", value );
174 }
175 else
176 {
177 LOG.debug( "Syntax invalid for '{}'", value );
178 }
179
180 return result;
181 }
182 else
183 {
184 if ( defaultPattern.matcher( strValue ).matches() )
185 {
186 LOG.debug( "Syntax valid for '{}'", value );
187 return true;
188 }
189 else
190 {
191 if ( compiledREs == null )
192 {
193 LOG.debug( "Syntax invalid for '{}'", value );
194 return false;
195 }
196
197 // The default is not enough, let's try
198 // the other regexps
199 for ( Pattern pattern:compiledREs )
200 {
201 if ( pattern.matcher( strValue ).matches() )
202 {
203 LOG.debug( "Syntax valid for '{}'", value );
204 return true;
205 }
206 }
207
208 LOG.debug( "Syntax invalid for '{}'", value );
209 return false;
210 }
211 }
212 }
213 }